From 2cf856028c1a1292a1b8f4e81815b520c741d179 Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Fri, 23 May 2025 19:10:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=82=E6=95=B0=E5=90=8D?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bukkit/block/BlockEventListener.java | 4 +- .../block/behavior/CropBlockBehavior.java | 2 +- .../item/listener/ItemEventListener.java | 6 +- .../core/block/BlockStateHolder.java | 36 +++++----- .../core/block/ImmutableBlockState.java | 2 +- .../core/block/VanillaBlockState.java | 23 ++++++- .../core/pack/AbstractPackManager.java | 66 +++++-------------- .../MatchBlockPropertyCondition.java | 2 +- .../parameter/BlockParameterProvider.java | 2 +- .../parameter/DirectContextParameters.java | 2 +- 10 files changed, 67 insertions(+), 78 deletions(-) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java index 436a2d9df..042c1d32f 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java @@ -168,7 +168,7 @@ public class BlockEventListener implements Listener { Cancellable cancellable = Cancellable.of(event::isCancelled, event::setCancelled); PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder() .withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block)) - .withParameter(DirectContextParameters.BLOCK_STATE, state) + .withParameter(DirectContextParameters.CUSTOM_BLOCK_STATE, state) .withParameter(DirectContextParameters.EVENT, cancellable) .withParameter(DirectContextParameters.POSITION, position) .withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand) @@ -304,7 +304,7 @@ public class BlockEventListener implements Listener { .withParameter(DirectContextParameters.EVENT, cancellable) .withParameter(DirectContextParameters.POSITION, new WorldPosition(new BukkitWorld(event.getWorld()), LocationUtils.toVec3d(location))) .withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block)) - .withParameter(DirectContextParameters.BLOCK_STATE, state) + .withParameter(DirectContextParameters.CUSTOM_BLOCK_STATE, state) ), EventTrigger.STEP); if (cancellable.isCancelled()) { event.setCancelled(true); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java index 29b2f7e4e..a8d14c48f 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java @@ -149,7 +149,7 @@ public class CropBlockBehavior extends BushBlockBehavior { int i = this.getAge(immutableBlockState) + this.boneMealBonus.getInt( SimpleContext.of( ContextHolder.builder() - .withParameter(DirectContextParameters.BLOCK_STATE, immutableBlockState) + .withParameter(DirectContextParameters.CUSTOM_BLOCK_STATE, immutableBlockState) .withParameter(DirectContextParameters.POSITION, new WorldPosition(new BukkitWorld(world), Vec3d.atCenterOf(new Vec3i(x, y, z)))) .build() ) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/listener/ItemEventListener.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/listener/ItemEventListener.java index 1ac765bcd..50fd9bd38 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/listener/ItemEventListener.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/listener/ItemEventListener.java @@ -101,7 +101,7 @@ public class ItemEventListener implements Listener { CustomBlock customBlock = immutableBlockState.owner().value(); PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder() .withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block)) - .withParameter(DirectContextParameters.BLOCK_STATE, immutableBlockState) + .withParameter(DirectContextParameters.CUSTOM_BLOCK_STATE, immutableBlockState) .withParameter(DirectContextParameters.HAND, hand) .withParameter(DirectContextParameters.EVENT, dummy) .withParameter(DirectContextParameters.POSITION, LocationUtils.toWorldPosition(block.getLocation())) @@ -175,7 +175,7 @@ public class ItemEventListener implements Listener { Cancellable dummy = Cancellable.dummy(); PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder() .withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block)) - .withOptionalParameter(DirectContextParameters.BLOCK_STATE, immutableBlockState) + .withOptionalParameter(DirectContextParameters.CUSTOM_BLOCK_STATE, immutableBlockState) .withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand) .withParameter(DirectContextParameters.POSITION, LocationUtils.toWorldPosition(block.getLocation())) .withParameter(DirectContextParameters.HAND, hand) @@ -219,7 +219,7 @@ public class ItemEventListener implements Listener { Cancellable dummy = Cancellable.dummy(); PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder() .withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block)) - .withOptionalParameter(DirectContextParameters.BLOCK_STATE, immutableBlockState) + .withOptionalParameter(DirectContextParameters.CUSTOM_BLOCK_STATE, immutableBlockState) .withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand) .withParameter(DirectContextParameters.POSITION, LocationUtils.toWorldPosition(block.getLocation())) .withParameter(DirectContextParameters.HAND, hand) diff --git a/core/src/main/java/net/momirealms/craftengine/core/block/BlockStateHolder.java b/core/src/main/java/net/momirealms/craftengine/core/block/BlockStateHolder.java index 7d9c265b3..445f9c518 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/block/BlockStateHolder.java +++ b/core/src/main/java/net/momirealms/craftengine/core/block/BlockStateHolder.java @@ -19,7 +19,7 @@ public class BlockStateHolder { } public Holder owner() { - return owner; + return this.owner; } public > ImmutableBlockState cycle(Property property) { @@ -38,14 +38,14 @@ public class BlockStateHolder { @Override public String toString() { - if (propertyMap.isEmpty()) { - return owner.value().id().toString(); + if (this.propertyMap.isEmpty()) { + return this.owner.value().id().toString(); } - return owner.value().id() + "[" + getPropertiesAsString() + "]"; + return this.owner.value().id() + "[" + getPropertiesAsString() + "]"; } public String getPropertiesAsString() { - return propertyMap.entrySet().stream() + return this.propertyMap.entrySet().stream() .map(entry -> { Property property = entry.getKey(); return property.name() + "=" + Property.formatValue(property, entry.getValue()); @@ -54,17 +54,17 @@ public class BlockStateHolder { } public Collection> getProperties() { - return Collections.unmodifiableSet(propertyMap.keySet()); + return Collections.unmodifiableSet(this.propertyMap.keySet()); } public > boolean contains(Property property) { - return propertyMap.containsKey(property); + return this.propertyMap.containsKey(property); } public > T get(Property property) { T value = getNullable(property); if (value == null) { - throw new IllegalArgumentException("Property " + property + " not found in " + owner.value().id()); + throw new IllegalArgumentException("Property " + property + " not found in " + this.owner.value().id()); } return value; } @@ -75,19 +75,19 @@ public class BlockStateHolder { @Nullable public > T getNullable(Property property) { - Comparable value = propertyMap.get(property); + Comparable value = this.propertyMap.get(property); return value != null ? property.valueClass().cast(value) : null; } public , V extends T> ImmutableBlockState with(Property property, V value) { - if (!propertyMap.containsKey(property)) { - throw new IllegalArgumentException("Property " + property + " not found in " + owner.value().id()); + if (!this.propertyMap.containsKey(property)) { + throw new IllegalArgumentException("Property " + property + " not found in " + this.owner.value().id()); } return withInternal(property, value); } private , V extends T> ImmutableBlockState withInternal(Property property, V newValue) { - if (newValue.equals(propertyMap.get(property))) { + if (newValue.equals(this.propertyMap.get(property))) { return (ImmutableBlockState) this; } @@ -96,20 +96,20 @@ public class BlockStateHolder { throw new IllegalArgumentException("Invalid value " + newValue + " for property " + property); } - return withMap.get(property)[index]; + return this.withMap.get(property)[index]; } public void createWithMap(Map, Comparable>, ImmutableBlockState> states) { - if (withMap != null) { + if (this.withMap != null) { throw new IllegalStateException("WithMap already initialized"); } - Reference2ObjectArrayMap, ImmutableBlockState[]> map = new Reference2ObjectArrayMap<>(propertyMap.size()); + Reference2ObjectArrayMap, ImmutableBlockState[]> map = new Reference2ObjectArrayMap<>(this.propertyMap.size()); - for (Property property : propertyMap.keySet()) { + for (Property property : this.propertyMap.keySet()) { ImmutableBlockState[] statesArray = property.possibleValues().stream() .map(value -> { - Map, Comparable> testMap = new Reference2ObjectArrayMap<>(propertyMap); + Map, Comparable> testMap = new Reference2ObjectArrayMap<>(this.propertyMap); testMap.put(property, value); ImmutableBlockState state = states.get(testMap); if (state == null) { @@ -126,6 +126,6 @@ public class BlockStateHolder { } public Map, Comparable> propertyEntries() { - return Collections.unmodifiableMap(propertyMap); + return Collections.unmodifiableMap(this.propertyMap); } } \ No newline at end of file diff --git a/core/src/main/java/net/momirealms/craftengine/core/block/ImmutableBlockState.java b/core/src/main/java/net/momirealms/craftengine/core/block/ImmutableBlockState.java index 68747718d..0123b54d9 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/block/ImmutableBlockState.java +++ b/core/src/main/java/net/momirealms/craftengine/core/block/ImmutableBlockState.java @@ -144,6 +144,6 @@ public class ImmutableBlockState extends BlockStateHolder { if (block == null) return List.of(); LootTable lootTable = (LootTable) block.lootTable(); if (lootTable == null) return List.of(); - return lootTable.getRandomItems(builder.withParameter(DirectContextParameters.BLOCK_STATE, this).build(), world, player); + return lootTable.getRandomItems(builder.withParameter(DirectContextParameters.CUSTOM_BLOCK_STATE, this).build(), world, player); } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/block/VanillaBlockState.java b/core/src/main/java/net/momirealms/craftengine/core/block/VanillaBlockState.java index 9e811011f..e86fcd003 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/block/VanillaBlockState.java +++ b/core/src/main/java/net/momirealms/craftengine/core/block/VanillaBlockState.java @@ -2,5 +2,26 @@ package net.momirealms.craftengine.core.block; import net.momirealms.craftengine.core.util.Key; -public record VanillaBlockState(Key type, String properties, int registryId) { +public class VanillaBlockState { + private final Key type; + private final String properties; + private final int registryId; + + public VanillaBlockState(Key type, String properties, int registryId) { + this.properties = properties; + this.registryId = registryId; + this.type = type; + } + + public String properties() { + return properties; + } + + public int registryId() { + return registryId; + } + + public Key type() { + return type; + } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java b/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java index a91f3b95d..22cd977ae 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java +++ b/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java @@ -477,29 +477,27 @@ public abstract class AbstractPackManager implements PackManager { for (CachedConfigSection cached : entry.getValue()) { for (Map.Entry configEntry : cached.config().entrySet()) { String key = configEntry.getKey(); + Key id = Key.withDefaultNamespace(key, cached.pack().namespace()); try { - Key id = Key.withDefaultNamespace(key, cached.pack().namespace()); - try { - if (parser.supportsParsingObject()) { - parser.parseObject(cached.pack(), cached.filePath(), id, configEntry.getValue()); - } else if (predicate.test(parser)) { - if (configEntry.getValue() instanceof Map configSection0) { - Map configSection1 = castToMap(configSection0, false); - if ((boolean) configSection1.getOrDefault("enable", true)) { - parser.parseSection(cached.pack(), cached.filePath(), id, plugin.templateManager().applyTemplates(configSection1)); - } - } else { - TranslationManager.instance().log("warning.config.structure.not_section", cached.filePath().toString(), cached.prefix() + "." + key, configEntry.getValue().getClass().getSimpleName()); + if (parser.supportsParsingObject()) { + parser.parseObject(cached.pack(), cached.filePath(), id, configEntry.getValue()); + } else if (predicate.test(parser)) { + if (configEntry.getValue() instanceof Map configSection0) { + Map configSection1 = castToMap(configSection0, false); + if ((boolean) configSection1.getOrDefault("enable", true)) { + parser.parseSection(cached.pack(), cached.filePath(), id, plugin.templateManager().applyTemplates(configSection1)); } + } else { + TranslationManager.instance().log("warning.config.structure.not_section", cached.filePath().toString(), cached.prefix() + "." + key, configEntry.getValue().getClass().getSimpleName()); } - } catch (LocalizedException e) { - if (e instanceof LocalizedResourceConfigException exception) { - exception.setPath(cached.filePath()); - exception.setId(cached.prefix() + "." + key); - } - TranslationManager.instance().log(e.node(), e.arguments()); - this.plugin.debug(e::node); } + } catch (LocalizedException e) { + if (e instanceof LocalizedResourceConfigException exception) { + exception.setPath(cached.filePath()); + exception.setId(cached.prefix() + "." + key); + } + TranslationManager.instance().log(e.node(), e.arguments()); + this.plugin.debug(e::node); } catch (Exception e) { this.plugin.logger().warn("Unexpected error loading file " + cached.filePath() + " - '" + parser.sectionId()[0] + "." + key + "'. Please find the cause according to the stacktrace or seek developer help.", e); } @@ -522,36 +520,6 @@ public abstract class AbstractPackManager implements PackManager { } } -// private static void initFileSystemProvider() { -// String osName = System.getProperty("os.name").toLowerCase(); -// String providerClass = null; -// if (osName.contains("win")) { -// providerClass = "sun.nio.fs.WindowsFileSystemProvider"; -// } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) { -// providerClass = "sun.nio.fs.LinuxFileSystemProvider"; -// } else if (osName.contains("mac")) { -// providerClass = "sun.nio.fs.MacOSXFileSystemProvider"; -// } -// if (providerClass != null) { -// try { -// System.setProperty("java.nio.file.spi.DefaultFileSystemProvider", providerClass); -// } catch (Exception ignored) {} -// } -// } -// -// private static void deleteDirectory(Path folder) throws IOException { -// if (!Files.exists(folder)) return; -// try (Stream walk = Files.walk(folder)) { -// walk.sorted(Comparator.reverseOrder()) -// .parallel() -// .forEach(path -> { -// try { -// Files.delete(path); -// } catch (IOException ignored) {} -// }); -// } -// } - @Override public void generateResourcePack() throws IOException { this.plugin.logger().info("Generating resource pack..."); diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchBlockPropertyCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchBlockPropertyCondition.java index 44ae3625b..b8a213d27 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchBlockPropertyCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchBlockPropertyCondition.java @@ -29,7 +29,7 @@ public class MatchBlockPropertyCondition implements Conditi @Override public boolean test(CTX ctx) { - return ctx.getOptionalParameter(DirectContextParameters.BLOCK_STATE).map(state -> { + return ctx.getOptionalParameter(DirectContextParameters.CUSTOM_BLOCK_STATE).map(state -> { CustomBlock block = state.owner().value(); for (Pair property : this.properties) { Property propertyIns = block.getProperty(property.left()); diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/BlockParameterProvider.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/BlockParameterProvider.java index e707aca80..1aac69f9c 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/BlockParameterProvider.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/BlockParameterProvider.java @@ -19,7 +19,7 @@ public class BlockParameterProvider implements ChainParameterProvider EXPLOSION_RADIUS = ContextKey.direct("explosion_radius"); public static final ContextKey PLAYER = ContextKey.direct("player"); public static final ContextKey ENTITY = ContextKey.direct("entity"); - public static final ContextKey BLOCK_STATE = ContextKey.direct("custom_block_state"); + public static final ContextKey CUSTOM_BLOCK_STATE = ContextKey.direct("custom_block_state"); public static final ContextKey COORDINATE = ContextKey.direct("coordinate"); public static final ContextKey POSITION = ContextKey.direct("position"); public static final ContextKey NAME = ContextKey.direct("name");