Skip to content

Commit

Permalink
Merge from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchampions committed Feb 7, 2025
2 parents c31ca24 + 66ec3df commit 6eca6f2
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ public SlimefunUniversalBlockData createUniversalBlock(Location l, String sfId)
UUID uuid = UUID.randomUUID();
SlimefunUniversalBlockData uniData = new SlimefunUniversalBlockData(uuid, sfId, l);

uniData.setIsDataLoaded(true);

uniData.initLastPresent();

uniData.setIsDataLoaded(true);

loadedUniversalData.put(uuid, uniData);

UniversalMenuPreset preset = UniversalMenuPreset.getPreset(sfId);
Expand Down Expand Up @@ -507,12 +507,16 @@ public SlimefunUniversalBlockData getUniversalBlockDataFromCache(UUID uuid) {
* @param l Slimefun block location {@link Location}
*/
public Optional<SlimefunUniversalBlockData> getUniversalBlockDataFromCache(Location l) {
return loadedUniversalData.values().stream()
.filter(uniData -> uniData instanceof SlimefunUniversalBlockData ubd
&& ubd.getLastPresent() != null
&& l.equals(ubd.getLastPresent().toLocation()))
.map(data -> (SlimefunUniversalBlockData) data)
.findFirst();
for (SlimefunUniversalData uniData : loadedUniversalData.values()) {
if (uniData instanceof SlimefunUniversalBlockData ubd
&& ubd.isDataLoaded()
&& ubd.getLastPresent() != null
&& l.equals(ubd.getLastPresent().toLocation())) {
return Optional.of(ubd);
}
}

return Optional.empty();
}

/**
Expand Down Expand Up @@ -697,7 +701,7 @@ public void loadUniversalRecord() {
? new SlimefunUniversalBlockData(uuid, sfId)
: new SlimefunUniversalData(uuid, sfId);

traits.forEach(t -> uniData.getTraits().add(t));
traits.forEach(uniData::addTrait);

scheduleReadTask(() -> loadUniversalData(uniData));
});
Expand Down Expand Up @@ -826,10 +830,10 @@ public void loadUniversalData(SlimefunUniversalData uniData) {
DataUtils.blockDataDebase64(recordSet.get(FieldKey.DATA_VALUE)),
false));

uniData.setIsDataLoaded(true);

loadedUniversalData.putIfAbsent(uniData.getUUID(), uniData);

uniData.setIsDataLoaded(true);

if (uniData.hasTrait(UniversalDataTrait.INVENTORY)) {
UniversalMenuPreset menuPreset = UniversalMenuPreset.getPreset(uniData.getSfId());
if (menuPreset != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public void setLastPresent(Location l) {
}

public BlockPosition getLastPresent() {
if (!isDataLoaded()) {
return null;
}

String data = getData(UniversalDataTrait.BLOCK.getReservedKey());

if (lastPresent != null) {
Expand All @@ -62,6 +66,14 @@ public BlockPosition getLastPresent() {
}

// 修复因使用不一致的文本转换导致的位置无法解析
oldLocationFix(data);
}

return lastPresent;
}

private void oldLocationFix(String data) {
try {
String[] lArr = data.split(",");
BlockPosition bp = new BlockPosition(
Bukkit.getWorld(lArr[0].replace("[world=", "")),
Expand All @@ -71,9 +83,9 @@ public BlockPosition getLastPresent() {

setTraitData(UniversalDataTrait.BLOCK, LocationUtils.getLocKey(bp.toLocation()));

return bp;
lastPresent = bp;
} catch (Exception x) {
throw new RuntimeException("Unable to fix location " + data + ", it might be broken", x);
}

return lastPresent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public UUID getUUID() {
return UUID.fromString(getKey());
}

public void addTrait(UniversalDataTrait trait) {
traits.add(trait);
}

public boolean hasTrait(UniversalDataTrait trait) {
return traits.contains(trait);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.Iterator;
import java.util.List;

import io.github.thebusybiscuit.slimefun4.utils.compatibility.VersionedEntityType;
import me.qscbm.slimefun4.utils.VersionEventsUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -45,7 +47,8 @@ public void onEntityExplode(EntityExplodeEvent e) {
so we just ignore it.
*/
if (Slimefun.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_21)
&& e.getEntityType() == EntityType.WIND_CHARGE) {
&& (e.getEntityType() == EntityType.WIND_CHARGE
|| e.getEntityType() == VersionedEntityType.BREEZE_WIND_CHARGE)) {
return;
}

Expand All @@ -54,6 +57,11 @@ public void onEntityExplode(EntityExplodeEvent e) {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockExplode(BlockExplodeEvent e) {
if (Slimefun.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_21)
&& VersionEventsUtils.isTriggerBlock(e)) {
return;
}

removeResistantBlocks(e.blockList().iterator());
}

Expand All @@ -71,7 +79,7 @@ public static void removeResistantBlocks(Iterator<Block> blocks) {

BlockDataController controller = Slimefun.getDatabaseManager().getBlockDataController();
if (!(item instanceof WitherProof)
&& !item.callItemHandler(BlockBreakHandler.class, handler -> {
&& !item.callItemHandler(BlockBreakHandler.class, handler -> {
if (blockData.isDataLoaded()) {
handleExplosion(handler, block);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

import java.lang.reflect.Field;
import javax.annotation.Nullable;

import org.bukkit.entity.EntityType;

// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/legacy/FieldRename.java?until=2a6207fe150b6165722fce94c83cc1f206620ab5&untilPath=src%2Fmain%2Fjava%2Forg%2Fbukkit%2Fcraftbukkit%2Flegacy%2FFieldRename.java#158-193
public class VersionedEntityType {
public static final EntityType BREEZE_WIND_CHARGE;
public static final EntityType MOOSHROOM;
public static final EntityType SNOW_GOLEM;

static {
MinecraftVersion version = Slimefun.getMinecraftVersion();

BREEZE_WIND_CHARGE = !version.isAtLeast(MinecraftVersion.MINECRAFT_1_21) ? null : getKey("BREEZE_WIND_CHARGE");
// MUSHROOM_COW is renamed to MOOSHROOM in 1.20.5
MOOSHROOM =
!version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5) ? EntityType.MUSHROOM_COW : getKey("MOOSHROOM");
Expand All @@ -22,7 +26,8 @@ public class VersionedEntityType {
SNOW_GOLEM = !version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5) ? EntityType.SNOWMAN : getKey("SNOW_GOLEM");
}

@Nullable private static EntityType getKey(String key) {
@Nullable
private static EntityType getKey(String key) {
try {
Field field = EntityType.class.getDeclaredField(key);
return (EntityType) field.get(null);
Expand Down
60 changes: 49 additions & 11 deletions src/main/java/me/qscbm/slimefun4/utils/VersionEventsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@
* 这是个临时适配的工具类
* 如果上游更新相关的工具类会迁移保证适配附属
*/
@SuppressWarnings({"JavaReflectionMemberAccess", "FieldCanBeLocal"})
public class VersionEventsUtils {
public static VersionEventsConstructor versionEventsConstructor;

private static Method TOP_INVENTORY_GETTER;

private static Method GET_TOP_INVENTORY;
private static Method GET_CLICKED_INVENTORY;
private static Method CLICKED_INVENTORY_GETTER;

private static Method EXPLOSION_RESULT_GETTER;

private static Class<?> EXPLOSION_RESULT_CLASS;

private static Enum<?> TRIGGER_BLOCK_ENUM;

static {
if (Slimefun.getMinecraftVersion()
Expand All @@ -36,14 +43,36 @@ public class VersionEventsUtils {
versionEventsConstructor = new VersionEventsConstructor();
}
try {
GET_TOP_INVENTORY =
Class.forName("org.bukkit.inventory.InventoryView").getMethod("getTopInventory");
GET_TOP_INVENTORY.setAccessible(true);

GET_CLICKED_INVENTORY = Class.forName("org.bukkit.event.inventory.InventoryClickEvent")
.getMethod("getClickedInventory");
GET_CLICKED_INVENTORY.setAccessible(true);
TOP_INVENTORY_GETTER =
Class.forName("org.bukkit.inventory.InventoryView")
.getMethod("getTopInventory");
TOP_INVENTORY_GETTER.setAccessible(true);
} catch (NoSuchMethodException | ClassNotFoundException ignored) {}
try {
CLICKED_INVENTORY_GETTER =
Class.forName("org.bukkit.event.inventory.InventoryClickEvent")
.getMethod("getClickedInventory");
CLICKED_INVENTORY_GETTER.setAccessible(true);
} catch (NoSuchMethodException | ClassNotFoundException ignored) {}
try {
EXPLOSION_RESULT_GETTER =
Class.forName("org.bukkit.event.block.BlockExplodeEvent")
.getMethod("getExplosionResult");
EXPLOSION_RESULT_GETTER.setAccessible(true);
} catch (NoSuchMethodException | ClassNotFoundException ignored) {}
try {
EXPLOSION_RESULT_CLASS = Class.forName("org.bukkit.ExplosionResult");
Method method = EXPLOSION_RESULT_CLASS.getMethod("values");
method.setAccessible(true);
Enum<?>[] enums = (Enum<?>[]) method.invoke(null);
for (Enum<?> e : enums) {
if (e.name().equals("TRIGGER_BLOCK")) {
TRIGGER_BLOCK_ENUM = e;
break;
}
}
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException |
IllegalAccessException ignored) {}
}

public static EntityDamageByEntityEvent newEntityDamageByEntityEvent(Entity damager, Entity damagee, EntityDamageEvent.DamageCause cause, String type, double damage) {
Expand All @@ -59,7 +88,7 @@ public static Inventory getTopInventory(InventoryEvent event) {
return event.getView().getTopInventory();
}
try {
return (Inventory) GET_TOP_INVENTORY.invoke(event.getView());
return (Inventory) TOP_INVENTORY_GETTER.invoke(event.getView());
} catch (Exception e) {
return event.getView().getTopInventory();
}
Expand All @@ -70,9 +99,18 @@ public static Inventory getClickedInventory(InventoryClickEvent event) {
return event.getClickedInventory();
}
try {
return (Inventory) GET_CLICKED_INVENTORY.invoke(event);
return (Inventory) CLICKED_INVENTORY_GETTER.invoke(event);
} catch (Exception e) {
return event.getClickedInventory();
}
}

public static boolean isTriggerBlock(BlockExplodeEvent e) {
try {
Object result = EXPLOSION_RESULT_GETTER.invoke(e);
return result == TRIGGER_BLOCK_ENUM;
} catch (IllegalAccessException | InvocationTargetException ex) {
return true;
}
}
}
1 change: 1 addition & 0 deletions src/main/java/me/qscbm/slimefun4/utils/VersionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class VersionUtils {
private static final int MINECRAFT_PATCH_VERSION;
private static final int MINECRAFT_PRE_RELEASE_VERSION;
private static final int MINECRAFT_RELEASE_CANDIDATE_VERSION;

static {
Matcher matcher = VERSION_PATTERN.matcher(BUKKIT_VERSION);
int version = 0;
Expand Down

0 comments on commit 6eca6f2

Please sign in to comment.