From 644a9a646684d162503f95aa895e50dec91e549d Mon Sep 17 00:00:00 2001 From: jhqwqmc <2110242767@qq.com> Date: Mon, 11 Aug 2025 09:40:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(block):=20=E9=87=8D=E6=9E=84=20ChangeO?= =?UTF-8?q?verTimeBlockBehavior=20=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../behavior/ChangeOverTimeBlockBehavior.java | 71 ++++++------------- .../src/main/resources/translations/en.yml | 1 + .../src/main/resources/translations/zh_cn.yml | 1 + 3 files changed, 25 insertions(+), 48 deletions(-) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/ChangeOverTimeBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/ChangeOverTimeBlockBehavior.java index d79531deb..78335c893 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/ChangeOverTimeBlockBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/ChangeOverTimeBlockBehavior.java @@ -1,71 +1,46 @@ package net.momirealms.craftengine.bukkit.block.behavior; +import net.momirealms.craftengine.bukkit.block.BukkitBlockManager; +import net.momirealms.craftengine.bukkit.plugin.reflection.bukkit.CraftBukkitReflections; +import net.momirealms.craftengine.core.block.*; +import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory; +import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.RandomUtils; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; + import java.util.Map; import java.util.Optional; import java.util.concurrent.Callable; -import org.bukkit.GameEvent; -import org.bukkit.event.block.BlockFormEvent; -import org.bukkit.util.Vector; - -import net.momirealms.craftengine.bukkit.block.BukkitBlockManager; -import net.momirealms.craftengine.bukkit.nms.FastNMS; -import net.momirealms.craftengine.bukkit.util.BlockStateUtils; -import net.momirealms.craftengine.bukkit.util.LocationUtils; -import net.momirealms.craftengine.bukkit.world.BukkitBlockInWorld; -import net.momirealms.craftengine.core.block.BlockBehavior; -import net.momirealms.craftengine.core.block.CustomBlock; -import net.momirealms.craftengine.core.block.ImmutableBlockState; -import net.momirealms.craftengine.core.block.UpdateOption; -import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory; -import net.momirealms.craftengine.core.util.Key; -import net.momirealms.craftengine.core.util.RandomUtils; -import net.momirealms.craftengine.core.util.ResourceConfigUtils; -import net.momirealms.craftengine.core.world.BlockPos; -import net.momirealms.craftengine.core.world.World; -import net.momirealms.sparrow.nbt.CompoundTag; - public class ChangeOverTimeBlockBehavior extends BukkitBlockBehavior { public static final Factory FACTORY = new Factory(); - private final float delay; + private final float changeSpeed; private final Key nextBlock; - public ChangeOverTimeBlockBehavior(CustomBlock customBlock, float delay, Key nextBlock) { + public ChangeOverTimeBlockBehavior(CustomBlock customBlock, float changeSpeed, Key nextBlock) { super(customBlock); - this.delay = delay; + this.changeSpeed = changeSpeed; this.nextBlock = nextBlock; } @Override - public void randomTick(Object thisBlock, Object[] args, Callable superMethod) throws Exception { - if(!shouldChange(args)) return; - Optional optionalNewCustomBlock = BukkitBlockManager.instance().blockById(nextBlock); - if (!optionalNewCustomBlock.isPresent()) return; - Object blockState = args[0]; - World level = (World) args[1]; - BlockPos blockPos = (BlockPos) args[2]; - Optional optionalCurrentState = BlockStateUtils .getOptionalCustomBlockState(blockState); - if (optionalCurrentState.isEmpty()) return; - CompoundTag compoundTag = optionalCurrentState.get().propertiesNbt(); - ImmutableBlockState newState = optionalNewCustomBlock.get().getBlockState(compoundTag); - BukkitBlockInWorld blockInWorld = (BukkitBlockInWorld) level.getBlockAt(LocationUtils.fromBlockPos(blockPos)); - BlockFormEvent event = new BlockFormEvent(blockInWorld.block(),BlockStateUtils.fromBlockData(newState.customBlockState().handle()).createBlockState()); - if (event.callEvent()) { - FastNMS.INSTANCE.method$LevelWriter$setBlock(level.serverWorld(), blockPos, newState.customBlockState().handle(), UpdateOption.UPDATE_ALL_IMMEDIATE.flags()); - blockInWorld.block().getWorld().sendGameEvent(null, GameEvent.BLOCK_CHANGE, new Vector(blockPos.x(), blockPos.y(), blockPos.z())); - } - } - - private boolean shouldChange(Object[] args) { - return RandomUtils.generateRandomFloat(0F, 1F) < this.delay; + public void randomTick(Object thisBlock, Object[] args, Callable superMethod) throws ReflectiveOperationException { + if (RandomUtils.generateRandomFloat(0F, 1F) >= this.changeSpeed) return; + Optional nextState = BukkitBlockManager.instance().blockById(this.nextBlock) + .map(CustomBlock::defaultState) + .map(ImmutableBlockState::customBlockState) + .map(BlockStateWrapper::handle); + if (nextState.isEmpty()) return; + CraftBukkitReflections.method$CraftEventFactory$handleBlockFormEvent.invoke(null, args[1], args[2], nextState.get(), UpdateOption.UPDATE_ALL.flags()); } public static class Factory implements BlockBehaviorFactory { + @Override public BlockBehavior create(CustomBlock block, Map arguments) { - float delay = Float.valueOf(arguments.getOrDefault("delay", 0.05688889F).toString()); - Key nextBlock = Key.of(ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.getOrDefault("next-block", "minecraft:air"), "warning.config.block.behavior.change_over_time_block_missing_next_block")); - return new ChangeOverTimeBlockBehavior(block, delay, nextBlock); + float changeSpeed = ResourceConfigUtils.getAsFloat(arguments.getOrDefault("change-speed", 0.05688889F), "change-speed"); + Key nextBlock = Key.of(ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.getOrDefault("next-block", "minecraft:air"), "warning.config.block.behavior.change_over_time_block.missing_next_block")); + return new ChangeOverTimeBlockBehavior(block, changeSpeed, nextBlock); } } } diff --git a/common-files/src/main/resources/translations/en.yml b/common-files/src/main/resources/translations/en.yml index 8a428d817..93b03f5c1 100644 --- a/common-files/src/main/resources/translations/en.yml +++ b/common-files/src/main/resources/translations/en.yml @@ -299,6 +299,7 @@ warning.config.block.behavior.stairs.missing_shape: "Issue found in file warning.config.block.behavior.pressure_plate.missing_powered: "Issue found in file - The block '' is missing the required 'powered' property for 'pressure_plate_block' behavior." warning.config.block.behavior.grass.missing_feature: "Issue found in file - The block '' is missing the required 'feature' argument for 'grass_block' behavior." warning.config.block.behavior.double_high.missing_half: "Issue found in file - The block '' is missing the required 'half' property for 'double_block' behavior." +warning.config.block.behavior.change_over_time_block.missing_next_block: "Issue found in file - The block '' is missing the required 'next_block' property for 'change_over_time_block' behavior." warning.config.model.generation.missing_parent: "Issue found in file - The config '' is missing the required 'parent' argument in 'generation' section." warning.config.model.generation.invalid_display_position: "Issue found in file - The config '' is using an invalid display position '' in 'generation.display' section. Allowed display positions: []" warning.config.model.generation.invalid_gui_light: "Issue found in file - The config '' is using an invalid gui-light option '' in 'generation' section. Allowed gui light options: []" diff --git a/common-files/src/main/resources/translations/zh_cn.yml b/common-files/src/main/resources/translations/zh_cn.yml index 3512ddf1b..b24d301a4 100644 --- a/common-files/src/main/resources/translations/zh_cn.yml +++ b/common-files/src/main/resources/translations/zh_cn.yml @@ -299,6 +299,7 @@ warning.config.block.behavior.stairs.missing_shape: "在文件 warning.config.block.behavior.pressure_plate.missing_powered: "在文件 发现问题 - 方块 '' 的 'pressure_plate_block' 行为缺少必需的 'powered' 属性" warning.config.block.behavior.grass.missing_feature: "在文件 发现问题 - 方块 '' 的 'grass_block' 行为缺少必需的 'feature' 参数" warning.config.block.behavior.double_high.missing_half: "在文件 发现问题 - 方块 '' 的 'double_block' 行为缺少必需的 'half' 属性" +warning.config.block.behavior.change_over_time_block.missing_next_block: "在文件 发现问题 - 方块 '' 的 'change_over_time_block' 行为缺少必需的 'next-block' 配置项" warning.config.model.generation.missing_parent: "在文件 发现问题 - 配置项 '' 的 'generation' 段落缺少必需的 'parent' 参数" warning.config.model.generation.conflict: "在文件 发现问题 - 无法为 '' 生成模型 存在多个配置尝试使用相同路径 '' 生成不同的 JSON 模型" warning.config.model.generation.invalid_display_position: "在文件 发现问题 - 配置项 '' 在 'generation.display' 区域使用了无效的 display 位置类型 ''. 可用展示类型: []"