9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 09:59:20 +00:00

feat(bukkit): 改进 LampBlockBehavior

This commit is contained in:
jhqwqmc
2025-09-07 08:08:11 +08:00
parent ff4efa13a0
commit 76e1e66308
10 changed files with 94 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
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.util.BlockStateUtils;
import net.momirealms.craftengine.bukkit.util.LocationUtils;
import net.momirealms.craftengine.core.block.BlockBehavior;
@@ -8,7 +9,9 @@ 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.Property;
import net.momirealms.craftengine.core.entity.player.InteractionResult;
import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
import net.momirealms.craftengine.core.item.context.UseOnContext;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
@@ -19,44 +22,89 @@ import java.util.concurrent.Callable;
public class LampBlockBehavior extends BukkitBlockBehavior {
public static final Factory FACTORY = new Factory();
private final Property<Boolean> litProperty;
private final Property<Boolean> poweredProperty;
private final boolean canOpenWithHand;
private final boolean redstoneToggleMode;
public LampBlockBehavior(CustomBlock block, Property<Boolean> litProperty) {
public LampBlockBehavior(CustomBlock block, Property<Boolean> litProperty, Property<Boolean> poweredProperty, boolean canOpenWithHand, boolean redstoneToggleMode) {
super(block);
this.litProperty = litProperty;
this.poweredProperty = poweredProperty;
this.canOpenWithHand = canOpenWithHand;
this.redstoneToggleMode = redstoneToggleMode;
}
@Override
public InteractionResult useWithoutItem(UseOnContext context, ImmutableBlockState state) {
if (!this.canOpenWithHand) {
return InteractionResult.PASS;
}
LampBlockBehavior behavior = state.behavior().getAs(LampBlockBehavior.class).orElse(null);
if (behavior == null) return InteractionResult.PASS;
FastNMS.INSTANCE.method$LevelWriter$setBlock(
context.getLevel().serverWorld(),
LocationUtils.toBlockPos(context.getClickedPos()),
state.cycle(behavior.litProperty).cycle(behavior.poweredProperty).customBlockState().literalObject(),
2
);
Optional.ofNullable(context.getPlayer()).ifPresent(p -> p.swingHand(context.getHand()));
return InteractionResult.SUCCESS_AND_CANCEL;
}
@Override
public ImmutableBlockState updateStateForPlacement(BlockPlaceContext context, ImmutableBlockState state) {
if (this.canOpenWithHand || this.redstoneToggleMode) return state;
Object level = context.getLevel().serverWorld();
state = state.with(this.litProperty, FastNMS.INSTANCE.method$SignalGetter$hasNeighborSignal(level, LocationUtils.toBlockPos(context.getClickedPos())));
state = state.with(this.poweredProperty, FastNMS.INSTANCE.method$SignalGetter$hasNeighborSignal(level, LocationUtils.toBlockPos(context.getClickedPos())));
return state;
}
@Override
public void tick(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
Object blockState = args[0];
Object world = args[1];
if (this.canOpenWithHand || this.redstoneToggleMode || !CoreReflections.clazz$ServerLevel.isInstance(world)) return;
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState);
if (optionalCustomState.isEmpty()) return;
Object world = args[1];
Object blockPos = args[2];
ImmutableBlockState customState = optionalCustomState.get();
if (customState.get(this.litProperty) && !FastNMS.INSTANCE.method$SignalGetter$hasNeighborSignal(world, blockPos)) {
if (FastNMS.INSTANCE.method$CraftEventFactory$callRedstoneChange(world, blockPos, 0, 15).getNewCurrent() != 15) {
return;
}
FastNMS.INSTANCE.method$LevelWriter$setBlock(world, blockPos, customState.cycle(this.litProperty).customBlockState().literalObject(), 2);
FastNMS.INSTANCE.method$LevelWriter$setBlock(world, blockPos, customState.cycle(this.litProperty).cycle(this.poweredProperty).customBlockState().literalObject(), 2);
}
}
@Override
public void onPlace(Object thisBlock, Object[] args, Callable<Object> superMethod) {
if (this.canOpenWithHand || !this.redstoneToggleMode) return;
Object state = args[0];
Object level = args[1];
Object pos = args[2];
Object oldState = args[3];
if (FastNMS.INSTANCE.method$BlockState$getBlock(oldState) != FastNMS.INSTANCE.method$BlockState$getBlock(state) && CoreReflections.clazz$ServerLevel.isInstance(level)) {
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(state);
if (optionalCustomState.isEmpty()) return;
checkAndFlip(optionalCustomState.get(), level, pos);
}
}
@Override
public void neighborChanged(Object thisBlock, Object[] args, Callable<Object> superMethod) {
Object blockState = args[0];
Object world = args[1];
if (this.canOpenWithHand || !CoreReflections.clazz$ServerLevel.isInstance(world)) return;
Optional<ImmutableBlockState> optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState);
if (optionalCustomState.isEmpty()) return;
Object world = args[1];
Object blockPos = args[2];
ImmutableBlockState customState = optionalCustomState.get();
boolean lit = customState.get(this.litProperty);
if (this.redstoneToggleMode) {
checkAndFlip(customState, world, blockPos);
return;
}
if (lit != FastNMS.INSTANCE.method$SignalGetter$hasNeighborSignal(world, blockPos)) {
if (lit) {
FastNMS.INSTANCE.method$ScheduledTickAccess$scheduleBlockTick(world, blockPos, thisBlock, 4);
@@ -64,17 +112,33 @@ public class LampBlockBehavior extends BukkitBlockBehavior {
if (FastNMS.INSTANCE.method$CraftEventFactory$callRedstoneChange(world, blockPos, 0, 15).getNewCurrent() != 15) {
return;
}
FastNMS.INSTANCE.method$LevelWriter$setBlock(world, blockPos, customState.cycle(this.litProperty).customBlockState().literalObject(), 2);
FastNMS.INSTANCE.method$LevelWriter$setBlock(world, blockPos, customState.cycle(this.litProperty).cycle(this.poweredProperty).customBlockState().literalObject(), 2);
}
}
}
private void checkAndFlip(ImmutableBlockState customState, Object level, Object pos) {
boolean hasNeighborSignal = FastNMS.INSTANCE.method$SignalGetter$hasNeighborSignal(level, pos);
boolean isPowered = customState.get(this.poweredProperty);
if (hasNeighborSignal != isPowered) {
ImmutableBlockState blockState = customState;
if (!isPowered) {
blockState = blockState.cycle(this.litProperty);
}
FastNMS.INSTANCE.method$LevelWriter$setBlock(level, pos, blockState.with(this.poweredProperty, hasNeighborSignal).customBlockState().literalObject(), 3);
}
}
@SuppressWarnings("unchecked")
public static class Factory implements BlockBehaviorFactory {
@Override
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
Property<Boolean> lit = (Property<Boolean>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("lit"), "warning.config.block.behavior.lamp.missing_lit");
return new LampBlockBehavior(block, lit);
Property<Boolean> powered = (Property<Boolean>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("powered"), "warning.config.block.behavior.lamp.missing_powered");
boolean canOpenWithHand = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("can-open-with-hand", false), "can-open-with-hand");
boolean redstoneToggleMode = !canOpenWithHand && ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("redstone-toggle-mode", false), "redstone-toggle-mode");
return new LampBlockBehavior(block, lit, powered, canOpenWithHand, redstoneToggleMode);
}
}
}

View File

@@ -83,7 +83,7 @@ public class VerticalCropBlockBehavior extends BukkitBlockBehavior {
@SuppressWarnings("unchecked")
@Override
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
Property<Integer> ageProperty = (Property<Integer>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("age"), "warning.config.block.behavior.sugar_cane.missing_age");
Property<Integer> ageProperty = (Property<Integer>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("age"), "warning.config.block.behavior.vertical_crop.missing_age");
int maxHeight = ResourceConfigUtils.getAsInt(arguments.getOrDefault("max-height", 3), "max-height");
boolean direction = arguments.getOrDefault("direction", "up").toString().equalsIgnoreCase("up");
return new VerticalCropBlockBehavior(block, ageProperty, maxHeight,

View File

@@ -80,4 +80,5 @@ minecraft:mangrove_fence_gate: 16
minecraft:cherry_fence_gate: 16
minecraft:bamboo_fence_gate: 16
minecraft:crimson_fence_gate: 16
minecraft:warped_fence_gate: 16
minecraft:warped_fence_gate: 16
minecraft:cactus: 15

View File

@@ -221,6 +221,9 @@ items#misc:
lit:
type: boolean
default: false
powered:
type: boolean
default: false
appearances:
off:
state: cactus:0
@@ -245,14 +248,22 @@ items#misc:
top: minecraft:block/custom/copper_coil_on
side: minecraft:block/custom/copper_coil_on_side
variants:
lit=false:
lit=false,powered=false:
appearance: 'off'
id: 0
lit=true:
lit=true,powered=false:
appearance: 'on'
id: 1
settings:
luminance: 8
lit=false,powered=true:
appearance: 'off'
id: 2
lit=true,powered=true:
appearance: 'on'
id: 3
settings:
luminance: 8
default:pebble:
material: nether_brick
custom-model-data: 3005

View File

@@ -273,7 +273,7 @@ warning.config.block.behavior.missing_type: "<yellow>Problem in Datei <arg:0> ge
warning.config.block.behavior.invalid_type: "<yellow>Problem in Datei <arg:0> gefunden - Der Block '<arg:1>' verwendet einen ungültigen Block-Behavior-Typ '<arg:2>'.</yellow>"
warning.config.block.behavior.concrete.missing_solid: "<yellow>Problem in Datei <arg:0> gefunden - Beim Block '<arg:1>' fehlt die erforderliche 'solid-block'-Option für das 'concrete_block'-Behavior.</yellow>"
warning.config.block.behavior.crop.missing_age: "<yellow>Problem in Datei <arg:0> gefunden - Beim Block '<arg:1>' fehlt die erforderliche 'age'-Property für das 'crop_block'-Behavior.</yellow>"
warning.config.block.behavior.sugar_cane.missing_age: "<yellow>Problem in Datei <arg:0> gefunden - Beim Block '<arg:1>' fehlt die erforderliche 'age'-Property für das 'sugar_cane_block'-Behavior.</yellow>"
warning.config.block.behavior.vertical_crop.missing_age: "<yellow>Problem in Datei <arg:0> gefunden - Beim Block '<arg:1>' fehlt die erforderliche 'age'-Property für das 'vertical_crop_block'-Behavior.</yellow>"
warning.config.block.behavior.leaves.missing_persistent: "<yellow>Problem in Datei <arg:0> gefunden - Beim Block '<arg:1>' fehlt die erforderliche 'persistent'-Property für das 'leaves_block'-Behavior.</yellow>"
warning.config.block.behavior.leaves.missing_distance: "<yellow>Problem in Datei <arg:0> gefunden - Beim Block '<arg:1>' fehlt die erforderliche 'distance'-Property für das 'leaves_block'-Behavior.</yellow>"
warning.config.block.behavior.lamp.missing_lit: "<yellow>Problem in Datei <arg:0> gefunden - Beim Block '<arg:1>' fehlt die erforderliche 'lit'-Property für das 'lamp_block'-Behavior.</yellow>"

View File

@@ -277,10 +277,11 @@ warning.config.block.behavior.missing_type: "<yellow>Issue found in file <arg:0>
warning.config.block.behavior.invalid_type: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is using an invalid block behavior type '<arg:2>'.</yellow>"
warning.config.block.behavior.concrete.missing_solid: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'solid-block' option for 'concrete_block' behavior.</yellow>"
warning.config.block.behavior.crop.missing_age: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'age' property for 'crop_block' behavior.</yellow>"
warning.config.block.behavior.sugar_cane.missing_age: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'age' property for 'sugar_cane_block' behavior.</yellow>"
warning.config.block.behavior.vertical_crop.missing_age: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'age' property for 'vertical_crop_block' behavior.</yellow>"
warning.config.block.behavior.leaves.missing_persistent: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'persistent' property for 'leaves_block' behavior.</yellow>"
warning.config.block.behavior.leaves.missing_distance: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'distance' property for 'leaves_block' behavior.</yellow>"
warning.config.block.behavior.lamp.missing_lit: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'lit' property for 'lamp_block' behavior.</yellow>"
warning.config.block.behavior.lamp.missing_powered: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'powered' property for 'lamp_block' behavior.</yellow>"
warning.config.block.behavior.sapling.missing_stage: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'stage' property for 'sapling_block' behavior.</yellow>"
warning.config.block.behavior.sapling.missing_feature: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'feature' argument for 'sapling_block' behavior.</yellow>"
warning.config.block.behavior.strippable.missing_stripped: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'stripped' argument for 'strippable_block' behavior.</yellow>"

View File

@@ -195,7 +195,7 @@ warning.config.block.behavior.missing_type: "<yellow>Problema encontrado en el a
warning.config.block.behavior.invalid_type: "<yellow>Problema encontrado en el archivo <arg:0> - El bloque '<arg:1>' está usando un tipo de comportamiento de bloque inválido '<arg:2>'.</yellow>"
warning.config.block.behavior.concrete.missing_solid: "<yellow>Problema encontrado en el archivo <arg:0> - El bloque '<arg:1>' carece de la opción requerida 'solid-block' para el comportamiento 'concrete_block'.</yellow>"
warning.config.block.behavior.crop.missing_age: "<yellow>Problema encontrado en el archivo <arg:0> - El bloque '<arg:1>' carece de la propiedad requerida 'age' para el comportamiento 'crop_block'.</yellow>"
warning.config.block.behavior.sugar_cane.missing_age: "<yellow>Problema encontrado en el archivo <arg:0> - El bloque '<arg:1>' carece de la propiedad requerida 'age' para el comportamiento 'sugar_cane_block'.</yellow>"
warning.config.block.behavior.vertical_crop.missing_age: "<yellow>Problema encontrado en el archivo <arg:0> - El bloque '<arg:1>' carece de la propiedad requerida 'age' para el comportamiento 'vertical_crop_block'.</yellow>"
warning.config.block.behavior.leaves.missing_persistent: "<yellow>Problema encontrado en el archivo <arg:0> - El bloque '<arg:1>' carece de la propiedad requerida 'persistent' para el comportamiento 'leaves_block'.</yellow>"
warning.config.block.behavior.leaves.missing_distance: "<yellow>Problema encontrado en el archivo <arg:0> - El bloque '<arg:1>' carece de la propiedad requerida 'distance' para el comportamiento 'leaves_block'.</yellow>"
warning.config.block.behavior.sapling.missing_stage: "<yellow>Problema encontrado en el archivo <arg:0> - El bloque '<arg:1>' carece de la propiedad requerida 'stage' para el comportamiento 'sapling_block'.</yellow>"

View File

@@ -245,7 +245,7 @@ warning.config.block.behavior.missing_type: "<yellow>Проблема найде
warning.config.block.behavior.invalid_type: "<yellow>Проблема найдена в файле <arg:0> - Блок '<arg:1>' имеет недействительный блочный behavior тип '<arg:2>'.</yellow>"
warning.config.block.behavior.concrete.missing_solid: "<yellow>Проблема найдена в файле <arg:0> - В блоке '<arg:1>' отсутствует необходимый 'solid-block' вариант для 'concrete_block' behavior.</yellow>"
warning.config.block.behavior.crop.missing_age: "<yellow>Проблема найдена в файле <arg:0> - В блоке '<arg:1>' отсутствует необходимый 'age' свойство для 'crop_block' behavior.</yellow>"
warning.config.block.behavior.sugar_cane.missing_age: "<yellow>Проблема найдена в файле <arg:0> - В блоке '<arg:1>' отсутствует необходимый 'age' свойство для 'sugar_cane_block' behavior.</yellow>"
warning.config.block.behavior.vertical_crop.missing_age: "<yellow>Проблема найдена в файле <arg:0> - В блоке '<arg:1>' отсутствует необходимый 'age' свойство для 'vertical_crop_block' behavior.</yellow>"
warning.config.block.behavior.leaves.missing_persistent: "<yellow>Проблема найдена в файле <arg:0> - В блоке '<arg:1>' отсутствует необходимый 'persistent' свойство для 'leaves_block' behavior.</yellow>"
warning.config.block.behavior.leaves.missing_distance: "<yellow>Проблема найдена в файле <arg:0> - В блоке '<arg:1>' отсутствует необходимый 'distance' свойство для 'leaves_block' behavior.</yellow>"
warning.config.block.behavior.lamp.missing_lit: "<yellow>Проблема найдена в файле <arg:0> - В блоке '<arg:1>' отсутствует необходимый 'lit' свойство для 'lamp_block' behavior.</yellow>"

View File

@@ -193,7 +193,7 @@ warning.config.block.behavior.missing_type: "<yellow><arg:0> dosyasında sorun b
warning.config.block.behavior.invalid_type: "<yellow><arg:0> dosyasında sorun bulundu - '<arg:1>' bloğu geçersiz bir blok davranış türü '<arg:2>' kullanıyor.</yellow>"
warning.config.block.behavior.concrete.missing_solid: "<yellow><arg:0> dosyasında sorun bulundu - '<arg:1>' bloğu, 'concrete_block' davranışı için gerekli 'solid-block' seçeneği eksik.</yellow>"
warning.config.block.behavior.crop.missing_age: "<yellow><arg:0> dosyasında sorun bulundu - '<arg:1>' bloğu, 'crop_block' davranışı için gerekli 'age' özelliği eksik.</yellow>"
warning.config.block.behavior.sugar_cane.missing_age: "<yellow><arg:0> dosyasında sorun bulundu - '<arg:1>' bloğu, 'sugar_cane_block' davranışı için gerekli 'age' özelliği eksik.</yellow>"
warning.config.block.behavior.vertical_crop.missing_age: "<yellow><arg:0> dosyasında sorun bulundu - '<arg:1>' bloğu, 'vertical_crop_block' davranışı için gerekli 'age' özelliği eksik.</yellow>"
warning.config.block.behavior.leaves.missing_persistent: "<yellow><arg:0> dosyasında sorun bulundu - '<arg:1>' bloğu, 'leaves_block' davranışı için gerekli 'persistent' özelliği eksik.</yellow>"
warning.config.block.behavior.leaves.missing_distance: "<yellow><arg:0> dosyasında sorun bulundu - '<arg:1>' bloğu, 'leaves_block' davranışı için gerekli 'distance' özelliği eksik.</yellow>"
warning.config.block.behavior.sapling.missing_stage: "<yellow><arg:0> dosyasında sorun bulundu - '<arg:1>' bloğu, 'sapling_block' davranışı için gerekli 'stage' özelliği eksik.</yellow>"

View File

@@ -277,10 +277,11 @@ warning.config.block.behavior.missing_type: "<yellow>在文件 <arg:0> 发现问
warning.config.block.behavior.invalid_type: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 使用了无效的行为类型 '<arg:2>'</yellow>"
warning.config.block.behavior.concrete.missing_solid: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'concrete_block' 行为缺少必需的 'solid-block' 选项</yellow>"
warning.config.block.behavior.crop.missing_age: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'crop_block' 行为缺少必需的 'age' 属性</yellow>"
warning.config.block.behavior.sugar_cane.missing_age: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'sugar_cane_block' 行为缺少必需的 'age' 属性</yellow>"
warning.config.block.behavior.vertical_crop.missing_age: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'vertical_crop_block' 行为缺少必需的 'age' 属性</yellow>"
warning.config.block.behavior.leaves.missing_persistent: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'leaves_block' 行为缺少必需的 'persistent' 属性</yellow>"
warning.config.block.behavior.leaves.missing_distance: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'leaves_block' 行为缺少必需的 'distance' 属性</yellow>"
warning.config.block.behavior.lamp.missing_lit: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'lamp_block' 行为缺少必需的 'lit' 属性</yellow>"
warning.config.block.behavior.lamp.missing_powered: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'lamp_block' 行为缺少必需的 'powered' 属性</yellow>"
warning.config.block.behavior.sapling.missing_stage: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'sapling_block' 行为缺少必需的 'stage' 属性</yellow>"
warning.config.block.behavior.sapling.missing_feature: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'sapling_block' 行为缺少必需的 'feature' 参数</yellow>"
warning.config.block.behavior.strippable.missing_stripped: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'strippable_block' 行为缺少必需的 'stripped' 参数</yellow>"