From 322cf1474de01384efbd426699dca45a6a9cb857 Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Tue, 25 Mar 2025 04:29:22 +0800 Subject: [PATCH] added block light --- .../default/configuration/blocks.yml | 4 +-- .../default/configuration/templates.yml | 10 ++++++++ .../bukkit/block/BukkitBlockManager.java | 1 + .../bukkit/block/BukkitCustomBlock.java | 10 ++++++++ .../craftengine/bukkit/util/Reflections.java | 25 +++++++++++++++++++ .../craftengine/core/block/BlockSettings.java | 15 +++++++++++ 6 files changed, 63 insertions(+), 2 deletions(-) diff --git a/bukkit/loader/src/main/resources/resources/default/configuration/blocks.yml b/bukkit/loader/src/main/resources/resources/default/configuration/blocks.yml index 09eae0cf1..4bb73ce97 100644 --- a/bukkit/loader/src/main/resources/resources/default/configuration/blocks.yml +++ b/bukkit/loader/src/main/resources/resources/default/configuration/blocks.yml @@ -1,4 +1,4 @@ -items: +items#misc: default:chinese_lantern: material: paper custom-model-data: 3000 @@ -112,7 +112,7 @@ items: facing_clockwise=south: appearance: axisZ id: 3 -recipes: +recipes#misc: default:chinese_lantern: type: shaped pattern: diff --git a/bukkit/loader/src/main/resources/resources/default/configuration/templates.yml b/bukkit/loader/src/main/resources/resources/default/configuration/templates.yml index bb2ba0d64..f20f99fcc 100644 --- a/bukkit/loader/src/main/resources/resources/default/configuration/templates.yml +++ b/bukkit/loader/src/main/resources/resources/default/configuration/templates.yml @@ -962,6 +962,16 @@ templates#loot_tables: entries: - type: item item: "{item}" + # drop with silk touch + default:loot_table/silk_touch: + pools: + - rolls: 1 + conditions: + - type: enchantment + predicate: minecraft:silk_touch>=1 + entries: + - type: item + item: "{item}" # drop ores default:loot_table/ore: pools: diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java index 64d24eb3b..3eae94443 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java @@ -722,6 +722,7 @@ public class BukkitBlockManager extends AbstractBlockManager { if (plugin.hasMod()) { newRealBlock = Reflections.method$Registry$get.invoke(Reflections.instance$BuiltInRegistries$BLOCK, resourceLocation); newBlockState = getOnlyBlockState(newRealBlock); + @SuppressWarnings("unchecked") Optional optionalHolder = (Optional) Reflections.method$Registry$getHolder0.invoke(Reflections.instance$BuiltInRegistries$BLOCK, resourceLocation); blockHolder = optionalHolder.get(); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitCustomBlock.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitCustomBlock.java index 3c8f16e6b..1b89860b0 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitCustomBlock.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitCustomBlock.java @@ -11,6 +11,7 @@ import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.registry.Holder; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.Tristate; +import net.momirealms.craftengine.core.util.VersionHelper; import net.momirealms.craftengine.shared.ObjectHolder; import net.momirealms.craftengine.shared.block.BlockBehavior; import org.bukkit.inventory.ItemStack; @@ -103,6 +104,15 @@ public class BukkitCustomBlock extends CustomBlock { Reflections.field$BlockBehaviour$soundType.set(mcBlock, SoundUtils.toSoundType(settings.sounds())); // init cache Reflections.method$BlockStateBase$initCache.invoke(mcBlockState); + // set block light + if (settings.blockLight() != -1) { + if (VersionHelper.isVersionNewerThan1_21_2()) { + Reflections.field$BlockStateBase$lightBlock.set(mcBlockState, settings.blockLight()); + } else { + Object cache = Reflections.field$BlockStateBase$cache.get(mcBlockState); + Reflections.field$BlockStateBase$Cache$lightBlock.set(cache, settings.blockLight()); + } + } // set fluid later if (settings.fluidState()) { Reflections.field$BlockStateBase$fluidState.set(mcBlockState, Reflections.method$FlowingFluid$getSource.invoke(Reflections.instance$Fluids$WATER, false)); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java index ac175461c..182629ae5 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java @@ -2225,6 +2225,25 @@ public class Reflections { ) ); + public static final Class clazz$BlockStateBase$Cache = requireNonNull( + ReflectionUtils.getClazz( + BukkitReflectionUtils.assembleMCClass("world.level.block.state.BlockBehaviour$BlockStateBase$Cache"), + BukkitReflectionUtils.assembleMCClass("world.level.block.state.BlockBase$BlockData$Cache") + ) + ); + + public static final Field field$BlockStateBase$cache = requireNonNull( + ReflectionUtils.getDeclaredField( + clazz$BlockStateBase, clazz$BlockStateBase$Cache, 0 + ) + ); + + // 1.20-1.21.1 + public static final Field field$BlockStateBase$Cache$lightBlock = + ReflectionUtils.getInstanceDeclaredField( + clazz$BlockStateBase$Cache, int.class, 0 + ); + public static final Method method$BlockStateBase$initCache = requireNonNull( ReflectionUtils.getMethod( clazz$BlockStateBase, void.class, new String[] { "initCache", "a" } @@ -2309,6 +2328,12 @@ public class Reflections { ) ); + // 1.21.2+ + public static final Field field$BlockStateBase$lightBlock = + ReflectionUtils.getInstanceDeclaredField( + clazz$BlockStateBase, int.class, 1 + ); + public static final Class clazz$AABB = requireNonNull( ReflectionUtils.getClazz( BukkitReflectionUtils.assembleMCClass("world.phys.AABB"), diff --git a/core/src/main/java/net/momirealms/craftengine/core/block/BlockSettings.java b/core/src/main/java/net/momirealms/craftengine/core/block/BlockSettings.java index 183ab46a5..c065dbd95 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/block/BlockSettings.java +++ b/core/src/main/java/net/momirealms/craftengine/core/block/BlockSettings.java @@ -11,6 +11,7 @@ public class BlockSettings { boolean burnable; int burnChance; int fireSpreadChance; + int blockLight = -1; // TODO investigate how starlight works boolean replaceable; float hardness = 2f; float resistance = 2f; @@ -77,6 +78,7 @@ public class BlockSettings { newSettings.isViewBlocking = settings.isViewBlocking; newSettings.correctTools = settings.correctTools; newSettings.fluidState = settings.fluidState; + newSettings.blockLight = settings.blockLight; return newSettings; } @@ -156,6 +158,10 @@ public class BlockSettings { return isViewBlocking; } + public int blockLight() { + return blockLight; + } + public boolean isCorrectTool(Key key) { if (this.correctTools.isEmpty()) return true; return this.correctTools.contains(key); @@ -236,6 +242,11 @@ public class BlockSettings { return this; } + public BlockSettings blockLight(int intValue) { + this.blockLight = intValue; + return this; + } + public BlockSettings isRedstoneConductor(boolean isRedstoneConductor) { this.isRedstoneConductor = isRedstoneConductor ? Tristate.TRUE : Tristate.FALSE; return this; @@ -279,6 +290,10 @@ public class BlockSettings { int intValue = MiscUtils.getAsInt(value); return settings -> settings.luminance(intValue); })); + registerFactory("block-light", (value -> { + int intValue = MiscUtils.getAsInt(value); + return settings -> settings.blockLight(intValue); + })); registerFactory("hardness", (value -> { float floatValue = MiscUtils.getAsFloat(value); return settings -> settings.hardness(floatValue);