9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-26 02:19:23 +00:00

feat(bukkit): 实现#157

This commit is contained in:
jhqwqmc
2025-06-18 03:28:08 +08:00
parent 4d05a46db5
commit 062b69b4ae
5 changed files with 86 additions and 1 deletions

View File

@@ -116,7 +116,7 @@ public final class CraftEngineBlocks {
if (success) {
FastNMS.INSTANCE.method$BlockStateBase$onPlace(blockState, worldServer, blockPos, oldBlockState, false);
if (playSound) {
location.getWorld().playSound(location, block.sounds().placeSound().toString(), SoundCategory.BLOCKS, block.sounds().placeSound().volume(), block.sounds().placeSound().pitch());
location.getWorld().playSound(location, block.sounds().placeSound().id().toString(), SoundCategory.BLOCKS, block.sounds().placeSound().volume(), block.sounds().placeSound().pitch());
}
}
return success;

View File

@@ -21,6 +21,7 @@ public class BukkitBlockBehaviors extends BlockBehaviors {
public static final Key LAMP_BLOCK = Key.from("craftengine:lamp_block");
public static final Key TRAPDOOR_BLOCK = Key.from("craftengine:trapdoor_block");
public static final Key DOOR_BLOCK = Key.from("craftengine:door_block");
public static final Key STACKABLE_BLOCK = Key.from("craftengine:stackable_block");
public static void init() {
register(EMPTY, (block, args) -> EmptyBlockBehavior.INSTANCE);
@@ -40,5 +41,6 @@ public class BukkitBlockBehaviors extends BlockBehaviors {
register(LAMP_BLOCK, LampBlockBehavior.FACTORY);
register(TRAPDOOR_BLOCK, TrapDoorBlockBehavior.FACTORY);
register(DOOR_BLOCK, DoorBlockBehavior.FACTORY);
register(STACKABLE_BLOCK, StackableBlockBehavior.FACTORY);
}
}

View File

@@ -0,0 +1,81 @@
package net.momirealms.craftengine.bukkit.block.behavior;
import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks;
import net.momirealms.craftengine.bukkit.item.behavior.BlockItemBehavior;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
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.block.properties.Property;
import net.momirealms.craftengine.core.entity.player.InteractionResult;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.CustomItem;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
import net.momirealms.craftengine.core.item.context.UseOnContext;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.world.BlockPos;
import net.momirealms.craftengine.core.world.World;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import java.util.Map;
import java.util.Optional;
public class StackableBlockBehavior extends BukkitBlockBehavior {
public static final Factory FACTORY = new Factory();
private final Property<Integer> amountProperty;
public StackableBlockBehavior(CustomBlock block,Property<Integer> amountProperty) {
super(block);
this.amountProperty = amountProperty;
}
@Override
@SuppressWarnings("unchecked")
public InteractionResult useOnBlock(UseOnContext context, ImmutableBlockState state) {
Player player = context.getPlayer();
if (player.isSneaking()) {
return InteractionResult.PASS;
}
Item<ItemStack> item = (Item<ItemStack>) context.getItem();
if (item == null) {
return InteractionResult.PASS;
}
Optional<CustomItem<ItemStack>> optionalCustomItem = item.getCustomItem();
if (optionalCustomItem.isEmpty()) {
return InteractionResult.PASS;
}
CustomItem<ItemStack> customItem = optionalCustomItem.get();
for (ItemBehavior behavior : customItem.behaviors()) {
if (behavior instanceof BlockItemBehavior blockItemBehavior && blockItemBehavior.blockId().equals(customBlock.id())) {
int amount = state.get(this.amountProperty);
if (amount < this.amountProperty.possibleValues().getLast()) {
ImmutableBlockState nextStage = state.cycle(this.amountProperty);
World world = context.getLevel();
BlockPos pos = context.getClickedPos();
Location location = new Location((org.bukkit.World) world.platformWorld(), pos.x(), pos.y(), pos.z());
if (CraftEngineBlocks.place(location, nextStage, UpdateOption.UPDATE_NONE, true)) {
FastNMS.INSTANCE.method$ItemStack$consume(item.getLiteralObject(), 1, player.serverPlayer());
player.swingHand(context.getHand());
}
return InteractionResult.FAIL;
}
return InteractionResult.PASS;
}
}
return InteractionResult.PASS;
}
public static class Factory implements BlockBehaviorFactory {
@Override
@SuppressWarnings("unchecked")
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
Property<Integer> amount = (Property<Integer>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("amount"), "warning.config.block.behavior.stackable.missing_amount");
return new StackableBlockBehavior(block, amount);
}
}
}

View File

@@ -256,6 +256,7 @@ warning.config.block.behavior.trapdoor.missing_half: "<yellow>Issue found in fil
warning.config.block.behavior.trapdoor.missing_facing: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'facing' property for 'trapdoor_block' behavior.</yellow>"
warning.config.block.behavior.trapdoor.missing_open: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'open' property for 'trapdoor_block' behavior.</yellow>"
warning.config.block.behavior.trapdoor.missing_powered: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'powered' property for 'trapdoor_block' behavior.</yellow>"
warning.config.block.behavior.stackable.missing_amount: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'amount' property for 'stackable_block' behavior.</yellow>"
warning.config.model.generation.missing_parent: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'parent' argument in 'generation' section.</yellow>"
warning.config.model.generation.invalid_display_position: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid display position '<arg:2>' in 'generation.display' section. Allowed display positions: [<arg:3>]</yellow>"
warning.config.model.generation.invalid_gui_light: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid gui-light option '<arg:2>' in 'generation' section. Allowed gui light options: [<arg:3>]</yellow>"

View File

@@ -256,6 +256,7 @@ warning.config.block.behavior.trapdoor.missing_half: "<yellow>在文件 <arg:0>
warning.config.block.behavior.trapdoor.missing_facing: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'trapdoor_block' 行为缺少必需的 'facing' 属性</yellow>"
warning.config.block.behavior.trapdoor.missing_open: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'trapdoor_block' 行为缺少必需的 'open' 属性</yellow>"
warning.config.block.behavior.trapdoor.missing_powered: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'trapdoor_block' 行为缺少必需的 'powered' 属性</yellow>"
warning.config.block.behavior.stackable.missing_amount: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'stackable_block' 行为缺少必需的 'amount' 属性</yellow>"
warning.config.model.generation.missing_parent: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 的 'generation' 段落缺少必需的 'parent' 参数</yellow>"
warning.config.model.generation.conflict: "<yellow>在文件 <arg:0> 发现问题 - 无法为 '<arg:1>' 生成模型 存在多个配置尝试使用相同路径 '<arg:2>' 生成不同的 JSON 模型</yellow>"
warning.config.model.generation.invalid_display_position: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 在 'generation.display' 区域使用了无效的 display 位置类型 '<arg:2>'. 可用展示类型: [<arg:3>]</yellow>"