mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-29 03:49:15 +00:00
添加覆雪方块行为
This commit is contained in:
@@ -45,6 +45,7 @@ public class BukkitBlockBehaviors extends BlockBehaviors {
|
||||
public static final Key BUDDING_BLOCK = Key.from("craftengine:budding_block");
|
||||
public static final Key SEAT_BLOCK = Key.from("craftengine:seat_block");
|
||||
public static final Key SPREADING_BLOCK = Key.from("craftengine:spreading_block");
|
||||
public static final Key SNOWY_BLOCK = Key.from("craftengine:snowy_block");
|
||||
|
||||
public static void init() {
|
||||
register(EMPTY, (block, args) -> EmptyBlockBehavior.INSTANCE);
|
||||
@@ -88,5 +89,6 @@ public class BukkitBlockBehaviors extends BlockBehaviors {
|
||||
register(BUDDING_BLOCK, BuddingBlockBehavior.FACTORY);
|
||||
register(SEAT_BLOCK, SeatBlockBehavior.FACTORY);
|
||||
register(SPREADING_BLOCK, SpreadingBlockBehavior.FACTORY);
|
||||
register(SNOWY_BLOCK, SnowyBlockBehavior.FACTORY);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package net.momirealms.craftengine.bukkit.block.behavior;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MTagKeys;
|
||||
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.LocationUtils;
|
||||
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.behavior.BlockBehaviorFactory;
|
||||
import net.momirealms.craftengine.core.block.properties.BooleanProperty;
|
||||
import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class SnowyBlockBehavior extends BukkitBlockBehavior {
|
||||
public static final Factory FACTORY = new Factory();
|
||||
private final BooleanProperty snowyProperty;
|
||||
|
||||
public SnowyBlockBehavior(CustomBlock customBlock, BooleanProperty snowyProperty) {
|
||||
super(customBlock);
|
||||
this.snowyProperty = snowyProperty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object updateShape(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
|
||||
if (args[updateShape$direction] != CoreReflections.instance$Direction$UP) return superMethod.call();
|
||||
ImmutableBlockState state = BlockStateUtils.getOptionalCustomBlockState(args[0]).orElse(null);
|
||||
if (state == null || state.isEmpty()) return superMethod.call();
|
||||
return state.with(this.snowyProperty, isSnowySetting(args[updateShape$neighborState]));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImmutableBlockState updateStateForPlacement(BlockPlaceContext context, ImmutableBlockState state) {
|
||||
Object blockState = FastNMS.INSTANCE.method$BlockGetter$getBlockState(context.getLevel().serverWorld(), LocationUtils.toBlockPos(context.getClickedPos().above()));
|
||||
return state.with(this.snowyProperty, isSnowySetting(blockState));
|
||||
}
|
||||
|
||||
private static boolean isSnowySetting(Object state) {
|
||||
return FastNMS.INSTANCE.method$BlockStateBase$is(state, MTagKeys.Block$SNOW);
|
||||
}
|
||||
|
||||
public static class Factory implements BlockBehaviorFactory {
|
||||
|
||||
@Override
|
||||
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
|
||||
BooleanProperty snowyProperty = (BooleanProperty) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("snowy"), "warning.config.block.behavior.snowy.missing_snowy");
|
||||
return new SnowyBlockBehavior(block, snowyProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,11 @@ import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MTagKeys;
|
||||
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.LocationUtils;
|
||||
import net.momirealms.craftengine.core.block.*;
|
||||
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.behavior.BlockBehaviorFactory;
|
||||
import net.momirealms.craftengine.core.block.properties.BooleanProperty;
|
||||
import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
|
||||
import net.momirealms.craftengine.core.util.LazyReference;
|
||||
import net.momirealms.craftengine.core.util.RandomUtils;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
@@ -31,20 +30,6 @@ public class SpreadingBlockBehavior extends BukkitBlockBehavior {
|
||||
this.spreadBlock = LazyReference.lazyReference(() -> Objects.requireNonNull(BukkitBlockManager.instance().createBlockState(spreadBlock)).literalObject());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object updateShape(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
|
||||
if (args[updateShape$direction] != CoreReflections.instance$Direction$UP) return superMethod.call();
|
||||
return BlockStateUtils.toBlockStateWrapper(args[0]).withProperty("snowy", String.valueOf(isSnowySetting(args[updateShape$neighborState]))).literalObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImmutableBlockState updateStateForPlacement(BlockPlaceContext context, ImmutableBlockState state) {
|
||||
BooleanProperty snowy = (BooleanProperty) this.block().getProperty("snowy");
|
||||
if (snowy == null) return state;
|
||||
Object blockState = FastNMS.INSTANCE.method$BlockGetter$getBlockState(context.getLevel().serverWorld(), LocationUtils.toBlockPos(context.getClickedPos().above()));
|
||||
return state.with(snowy, isSnowySetting(blockState));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void randomTick(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
|
||||
Object state = args[0];
|
||||
@@ -88,10 +73,6 @@ public class SpreadingBlockBehavior extends BukkitBlockBehavior {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isSnowySetting(Object state) {
|
||||
return FastNMS.INSTANCE.method$BlockStateBase$is(state, MTagKeys.Block$SNOW);
|
||||
}
|
||||
|
||||
private static boolean canPropagate(Object state, Object level, Object pos) {
|
||||
Object blockPos = FastNMS.INSTANCE.method$BlockPos$relative(pos, CoreReflections.instance$Direction$UP);
|
||||
return canBeGrass(state, level, pos) && !FastNMS.INSTANCE.method$FluidState$is(FastNMS.INSTANCE.method$BlockGetter$getFluidState(level, blockPos), MTagKeys.Fluid$WATER);
|
||||
|
||||
Reference in New Issue
Block a user