9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-30 04:19:27 +00:00

Merge pull request #328 from jhqwqmc/patch-1

重构#316的逻辑
This commit is contained in:
XiaoMoMi
2025-08-25 17:21:22 +08:00
committed by GitHub
4 changed files with 50 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ public class BukkitBlockBehaviors extends BlockBehaviors {
public static final Key STAIRS_BLOCK = Key.from("craftengine:stairs_block");
public static final Key PRESSURE_PLATE_BLOCK = Key.from("craftengine:pressure_plate_block");
public static final Key DOUBLE_HIGH_BLOCK = Key.from("craftengine:double_high_block");
public static final Key CHANGE_OVER_TIME_BLOCK = Key.from("craftengine:change_over_time_block");
public static void init() {
register(EMPTY, (block, args) -> EmptyBlockBehavior.INSTANCE);
@@ -52,5 +53,6 @@ public class BukkitBlockBehaviors extends BlockBehaviors {
register(STAIRS_BLOCK, StairsBlockBehavior.FACTORY);
register(PRESSURE_PLATE_BLOCK, PressurePlateBlockBehavior.FACTORY);
register(DOUBLE_HIGH_BLOCK, DoubleHighBlockBehavior.FACTORY);
register(CHANGE_OVER_TIME_BLOCK,ChangeOverTimeBlockBehavior.FACTORY);
}
}

View File

@@ -0,0 +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;
public class ChangeOverTimeBlockBehavior extends BukkitBlockBehavior {
public static final Factory FACTORY = new Factory();
private final float changeSpeed;
private final Key nextBlock;
public ChangeOverTimeBlockBehavior(CustomBlock customBlock, float changeSpeed, Key nextBlock) {
super(customBlock);
this.changeSpeed = changeSpeed;
this.nextBlock = nextBlock;
}
@Override
public void randomTick(Object thisBlock, Object[] args, Callable<Object> superMethod) throws ReflectiveOperationException {
if (RandomUtils.generateRandomFloat(0F, 1F) >= this.changeSpeed) return;
Optional<Object> 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<String, Object> arguments) {
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);
}
}
}