9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 01:49:30 +00:00

Merge pull request #304 from iqtesterrr/dev

Add DoubleHighBlockItemBehavior
This commit is contained in:
XiaoMoMi
2025-07-24 22:51:47 +08:00
committed by GitHub
7 changed files with 70 additions and 2 deletions

View File

@@ -126,7 +126,7 @@ public class BlockItemBehavior extends BlockBoundItemBehavior {
// it's just world + pos
BlockState previousState = bukkitBlock.getState();
// place custom block
CraftEngineBlocks.place(placeLocation, blockStateToPlace, UpdateOption.UPDATE_ALL_IMMEDIATE, false);
placeBlock(placeLocation, blockStateToPlace);
if (player != null) {
// call bukkit event
@@ -215,6 +215,10 @@ public class BlockItemBehavior extends BlockBoundItemBehavior {
}
}
protected boolean placeBlock(Location location, ImmutableBlockState blockState) {
return CraftEngineBlocks.place(location, blockState, UpdateOption.UPDATE_ALL_IMMEDIATE, false);
}
@Override
public Key block() {
return this.blockId;

View File

@@ -11,6 +11,7 @@ public class BukkitItemBehaviors extends ItemBehaviors {
public static final Key FLINT_AND_STEEL_ITEM = Key.from("craftengine:flint_and_steel_item");
public static final Key COMPOSTABLE_ITEM = Key.from("craftengine:compostable_item");
public static final Key AXE_ITEM = Key.from("craftengine:axe_item");
public static final Key DOUBLE_HIGH_BLOCK_ITEM = Key.from("craftengine:double_high_block_item");
public static void init() {
register(EMPTY, EmptyItemBehavior.FACTORY);
@@ -20,5 +21,6 @@ public class BukkitItemBehaviors extends ItemBehaviors {
register(FLINT_AND_STEEL_ITEM, FlintAndSteelItemBehavior.FACTORY);
register(COMPOSTABLE_ITEM, CompostableItemBehavior.FACTORY);
register(AXE_ITEM, AxeItemBehavior.FACTORY);
register(DOUBLE_HIGH_BLOCK_ITEM, DoubleHighBlockItemBehavior.FACTORY);
}
}

View File

@@ -0,0 +1,58 @@
package net.momirealms.craftengine.bukkit.item.behavior;
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MFluids;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.UpdateOption;
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
import net.momirealms.craftengine.core.item.behavior.ItemBehaviorFactory;
import net.momirealms.craftengine.core.pack.Pack;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import org.bukkit.Location;
import java.nio.file.Path;
import java.util.Map;
public class DoubleHighBlockItemBehavior extends BlockItemBehavior {
public static final Factory FACTORY = new Factory();
public DoubleHighBlockItemBehavior(Key blockId) {
super(blockId);
}
@Override
protected boolean placeBlock(Location location, ImmutableBlockState blockState) {
Object level = FastNMS.INSTANCE.field$CraftWorld$ServerLevel(location.getWorld());
Object blockPos = FastNMS.INSTANCE.constructor$BlockPos(location.getBlockX(), location.getBlockY() + 1, location.getBlockZ());
UpdateOption option = UpdateOption.builder().updateNeighbors().updateClients().updateImmediate().updateKnownShape().build();
Object fluidData = FastNMS.INSTANCE.method$BlockGetter$getFluidState(level, blockPos);
Object stateToPlace = fluidData == MFluids.WATER$defaultState ? MFluids.WATER$defaultState : MBlocks.AIR$defaultState;
FastNMS.INSTANCE.method$LevelWriter$setBlock(level, blockPos, stateToPlace, option.flags());
return super.placeBlock(location, blockState);
}
public static class Factory implements ItemBehaviorFactory {
@Override
public ItemBehavior create(Pack pack, Path path, Key key, Map<String, Object> arguments) {
Object id = arguments.get("block");
if (id == null) {
throw new LocalizedResourceConfigException("warning.config.item.behavior.double_high.missing_block", new IllegalArgumentException("Missing required parameter 'block' for double_high_block_item behavior"));
}
if (id instanceof Map<?, ?> map) {
if (map.containsKey(key.toString())) {
// 防呆
BukkitBlockManager.instance().parser().parseSection(pack, path, key, MiscUtils.castToMap(map.get(key.toString()), false));
} else {
BukkitBlockManager.instance().parser().parseSection(pack, path, key, MiscUtils.castToMap(map, false));
}
return new DoubleHighBlockItemBehavior(key);
} else {
return new DoubleHighBlockItemBehavior(Key.of(id.toString()));
}
}
}
}

View File

@@ -7,6 +7,7 @@ public final class MFluids {
private MFluids() {}
public static final Object WATER;
public static final Object WATER$defaultState;
public static final Object FLOWING_WATER;
public static final Object LAVA;
public static final Object FLOWING_LAVA;
@@ -21,6 +22,7 @@ public final class MFluids {
static {
try {
WATER = getById("water");
WATER$defaultState = CoreReflections.method$Fluid$defaultFluidState.invoke(WATER);
FLOWING_WATER = getById("flowing_water");
LAVA = getById("lava");
FLOWING_LAVA = getById("flowing_lava");

View File

@@ -348,7 +348,7 @@ items:
arguments:
path: minecraft:item/custom/palm_door
behavior:
type: block_item
type: double_high_block_item
block:
behavior:
type: door_block

View File

@@ -187,6 +187,7 @@ warning.config.item.behavior.invalid_type: "<yellow>Issue found in file <arg:0>
warning.config.item.behavior.block.missing_block: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'block' argument for 'block_item' behavior.</yellow>"
warning.config.item.behavior.furniture.missing_furniture: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'furniture' argument for 'furniture_item' behavior.</yellow>"
warning.config.item.behavior.liquid_collision.missing_block: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'block' argument for 'liquid_collision_block_item' behavior.</yellow>"
warning.config.item.behavior.double_high.missing_block: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'block' argument for 'double_high_block_item' behavior.</yellow>"
warning.config.item.legacy_model.missing_path: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the require 'path' argument for legacy-model.</yellow>"
warning.config.item.legacy_model.overrides.missing_path: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the require 'path' argument for legacy-model overrides.</yellow>"
warning.config.item.legacy_model.overrides.missing_predicate: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the require 'predicate' argument for legacy-model overrides.</yellow>"

View File

@@ -185,6 +185,7 @@ warning.config.item.behavior.invalid_type: "<yellow>在文件 <arg:0> 发现问
warning.config.item.behavior.block.missing_block: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 的 'block_item' 行为缺少必需的 'block' 参数</yellow>"
warning.config.item.behavior.furniture.missing_furniture: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 的 'furniture_item' 行为缺少必需的 'furniture' 参数</yellow>"
warning.config.item.behavior.liquid_collision.missing_block: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 的 'liquid_collision_block_item' 行为缺少必需的 'block' 参数</yellow>"
warning.config.item.behavior.double_high.missing_block: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 的 'double_high_block_item' 行为缺少必需的 'block' 参数</yellow>"
warning.config.item.legacy_model.missing_path: "<yellow>在文件 <arg:0> 中发现问题 - 物品 '<arg:1>' 的旧版模型(legacy-model)缺少必需的 'path' 参数</yellow>"
warning.config.item.legacy_model.overrides.missing_path: "<yellow>在文件 <arg:0> 中发现问题 - 物品 '<arg:1>' 的旧版模型覆写规则(overrides)缺少必需的 'path' 参数</yellow>"
warning.config.item.legacy_model.overrides.missing_predicate: "<yellow>在文件 <arg:0> 中发现问题 - 物品 '<arg:1>' 的旧版模型覆写规则(overrides)缺少必需的 'predicate' 参数</yellow>"