From 3d6096f611c4c3ec0ac1295b20230b6feb55be33 Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Sat, 15 Jun 2024 15:48:12 +0800 Subject: [PATCH] feat: implement silk_touch --- .../component/common/BlockBaseComponent.java | 1 - .../allaymc/api/command/tree/CommandNode.java | 17 ++++++ .../api/command/tree/CommandNodeFactory.java | 5 ++ .../api/entity/interfaces/EntityPlayer.java | 4 ++ .../component/common/ItemBaseComponent.java | 23 +++++--- .../api/item/enchantment/EnchantmentType.java | 1 + .../common/BlockBaseComponentImpl.java | 12 ++++- .../server/block/type/AllayBlockType.java | 2 +- .../server/command/AllayCommandRegistry.java | 1 + .../command/defaults/EnchantCommand.java | 54 +++++++++++++++++++ .../command/tree/AllayCommandNodeFactory.java | 11 ++++ .../command/tree/node/EnchantmentNode.java | 31 +++++++++++ .../server/command/tree/node/ShortNode.java | 40 ++++++++++++++ .../common/ItemBaseComponentImpl.java | 5 ++ .../InventoryTransactionPacketProcessor.java | 7 ++- .../PlayerAuthInputPacketProcessor.java | 6 +-- 16 files changed, 203 insertions(+), 17 deletions(-) create mode 100644 Allay-Server/src/main/java/org/allaymc/server/command/defaults/EnchantCommand.java create mode 100644 Allay-Server/src/main/java/org/allaymc/server/command/tree/node/EnchantmentNode.java create mode 100644 Allay-Server/src/main/java/org/allaymc/server/command/tree/node/ShortNode.java diff --git a/Allay-API/src/main/java/org/allaymc/api/block/component/common/BlockBaseComponent.java b/Allay-API/src/main/java/org/allaymc/api/block/component/common/BlockBaseComponent.java index b064f68f5..88da33777 100644 --- a/Allay-API/src/main/java/org/allaymc/api/block/component/common/BlockBaseComponent.java +++ b/Allay-API/src/main/java/org/allaymc/api/block/component/common/BlockBaseComponent.java @@ -130,7 +130,6 @@ boolean place( default ItemStack[] getDrops(BlockState blockState, ItemStack usedItem) { // TODO: 时运 - // TODO: silk touch (精准采集) if (getBlockType().getItemType() != null) { return new ItemStack[] {getBlockType().getItemType().createItemStack()}; } diff --git a/Allay-API/src/main/java/org/allaymc/api/command/tree/CommandNode.java b/Allay-API/src/main/java/org/allaymc/api/command/tree/CommandNode.java index 5c488e47d..d5bb0799f 100644 --- a/Allay-API/src/main/java/org/allaymc/api/command/tree/CommandNode.java +++ b/Allay-API/src/main/java/org/allaymc/api/command/tree/CommandNode.java @@ -6,6 +6,7 @@ import org.allaymc.api.command.SenderType; import org.allaymc.api.entity.Entity; import org.allaymc.api.entity.interfaces.EntityPlayer; +import org.allaymc.api.item.enchantment.EnchantmentType; import org.cloudburstmc.protocol.bedrock.data.GameType; import org.cloudburstmc.protocol.bedrock.data.command.CommandParamData; import org.cloudburstmc.protocol.bedrock.data.command.CommandParamOption; @@ -130,6 +131,14 @@ default CommandNode str(String name) { return str(name, ""); } + default CommandNode shortNum(String name, short defaultValue) { + return addLeaf(getFactory().shortNum(name, this, defaultValue)); + } + + default CommandNode shortNum(String name) { + return shortNum(name, (short) 0); + } + default CommandNode intNum(String name, int defaultValue) { return addLeaf(getFactory().intNum(name, this, defaultValue)); } @@ -259,4 +268,12 @@ default CommandNode wildcardTarget(String name) { default CommandNode wildcardTarget(String name, String defaultValue) { return addLeaf(getFactory().wildcardTarget(name, this, defaultValue)); } + + default CommandNode enchantmentNode(String name) { + return enchantmentNode(name, null); + } + + default CommandNode enchantmentNode(String name, EnchantmentType defaultValue) { + return addLeaf(getFactory().enchantmentNode(name, this, defaultValue)); + } } diff --git a/Allay-API/src/main/java/org/allaymc/api/command/tree/CommandNodeFactory.java b/Allay-API/src/main/java/org/allaymc/api/command/tree/CommandNodeFactory.java index e59fe2fd5..048b444c7 100644 --- a/Allay-API/src/main/java/org/allaymc/api/command/tree/CommandNodeFactory.java +++ b/Allay-API/src/main/java/org/allaymc/api/command/tree/CommandNodeFactory.java @@ -3,6 +3,7 @@ import org.allaymc.api.ApiInstanceHolder; import org.allaymc.api.entity.Entity; import org.allaymc.api.entity.interfaces.EntityPlayer; +import org.allaymc.api.item.enchantment.EnchantmentType; import org.cloudburstmc.protocol.bedrock.data.GameType; import org.joml.Vector3fc; @@ -25,6 +26,8 @@ static CommandNodeFactory getFactory() { CommandNode str(String name, CommandNode parent, String defaultValue); + CommandNode shortNum(String name, CommandNode parent, short defaultValue); + CommandNode intNum(String name, CommandNode parent, int defaultValue); CommandNode longNum(String name, CommandNode parent, long defaultValue); @@ -56,4 +59,6 @@ static CommandNodeFactory getFactory() { CommandNode pos(String name, CommandNode parent, Vector3fc defaultValue); CommandNode wildcardTarget(String name, CommandNode parent, String defaultValue); + + CommandNode enchantmentNode(String name, CommandNode parent, EnchantmentType defaultValue); } diff --git a/Allay-API/src/main/java/org/allaymc/api/entity/interfaces/EntityPlayer.java b/Allay-API/src/main/java/org/allaymc/api/entity/interfaces/EntityPlayer.java index 275921eb8..8d87172c2 100644 --- a/Allay-API/src/main/java/org/allaymc/api/entity/interfaces/EntityPlayer.java +++ b/Allay-API/src/main/java/org/allaymc/api/entity/interfaces/EntityPlayer.java @@ -90,6 +90,10 @@ default void dropItemInPlayerPos(ItemStack itemStack) { dimension.dropItem(itemStack, playerLoc.add(0, + this.getEyeHeight() - 0.25f, 0, new Vector3f()), MathUtils.getDirectionVector(playerLoc.yaw(), playerLoc.pitch()).mul(0.5f), 40); } + default ItemStack getItemInHand() { + return getContainer(FullContainerType.PLAYER_INVENTORY).getItemInHand(); + } + default void sendItemInHandUpdate() { var inv = getContainer(FullContainerType.PLAYER_INVENTORY); var itemStack = inv.getItemInHand(); diff --git a/Allay-API/src/main/java/org/allaymc/api/item/component/common/ItemBaseComponent.java b/Allay-API/src/main/java/org/allaymc/api/item/component/common/ItemBaseComponent.java index 546e09a93..e9ed6f449 100644 --- a/Allay-API/src/main/java/org/allaymc/api/item/component/common/ItemBaseComponent.java +++ b/Allay-API/src/main/java/org/allaymc/api/item/component/common/ItemBaseComponent.java @@ -3,12 +3,10 @@ import org.allaymc.api.block.data.BlockFace; import org.allaymc.api.block.type.BlockState; import org.allaymc.api.block.type.BlockTypes; -import org.allaymc.api.data.VanillaBlockId; -import org.allaymc.api.data.VanillaItemId; import org.allaymc.api.entity.interfaces.EntityPlayer; -import org.allaymc.api.item.ItemHelper; import org.allaymc.api.item.ItemStack; import org.allaymc.api.item.component.ItemComponent; +import org.allaymc.api.item.enchantment.EnchantmentInstance; import org.allaymc.api.item.enchantment.EnchantmentType; import org.allaymc.api.item.type.ItemType; import org.allaymc.api.world.Dimension; @@ -17,14 +15,12 @@ import org.joml.Vector3fc; import org.joml.Vector3ic; -import java.util.EnumMap; +import java.util.Collection; import java.util.List; -import static org.allaymc.api.block.type.BlockTypes.BAMBOO_BLOCK_TYPE; import static org.allaymc.api.data.VanillaItemTags.*; import static org.allaymc.api.data.VanillaItemTags.WOODEN_TIER; import static org.allaymc.api.data.VanillaMaterialTypes.*; -import static org.allaymc.api.item.type.ItemTypes.BAMBOO_TYPE; import static org.allaymc.api.item.type.ItemTypes.SHEARS_TYPE; /** @@ -148,6 +144,8 @@ default NbtMap saveNBT() { short getEnchantmentLevel(EnchantmentType enchantmentType); + Collection getEnchantments(); + void addEnchantment(EnchantmentType enchantmentType, short level); /** @@ -199,4 +197,17 @@ default boolean canInstantBreak(BlockState blockState) { } return false; } + + default boolean checkEnchantmentCompatibility(EnchantmentType type) { + return getIncompatibleEnchantmentType(type) == null; + } + + default EnchantmentType getIncompatibleEnchantmentType(EnchantmentType type) { + for (var enchantmentInstance : getEnchantments()) { + if (!enchantmentInstance.getType().checkCompatibility(type)) { + return enchantmentInstance.getType(); + } + } + return null; + } } diff --git a/Allay-API/src/main/java/org/allaymc/api/item/enchantment/EnchantmentType.java b/Allay-API/src/main/java/org/allaymc/api/item/enchantment/EnchantmentType.java index dcdcd9d2b..f678e2e66 100644 --- a/Allay-API/src/main/java/org/allaymc/api/item/enchantment/EnchantmentType.java +++ b/Allay-API/src/main/java/org/allaymc/api/item/enchantment/EnchantmentType.java @@ -20,6 +20,7 @@ public interface EnchantmentType { EnchantmentInstance createInstance(short level); default boolean checkCompatibility(EnchantmentType other) { + // TODO return true; } } diff --git a/Allay-Server/src/main/java/org/allaymc/server/block/component/common/BlockBaseComponentImpl.java b/Allay-Server/src/main/java/org/allaymc/server/block/component/common/BlockBaseComponentImpl.java index 6f07ca3d4..44b463ea8 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/block/component/common/BlockBaseComponentImpl.java +++ b/Allay-Server/src/main/java/org/allaymc/server/block/component/common/BlockBaseComponentImpl.java @@ -10,6 +10,7 @@ import org.allaymc.api.block.data.BlockStateWithPos; import org.allaymc.api.block.type.BlockState; import org.allaymc.api.block.type.BlockType; +import org.allaymc.api.item.enchantment.type.EnchantmentSilkTouchType; import org.allaymc.api.utils.Identifier; import org.allaymc.api.component.annotation.ComponentIdentifier; import org.allaymc.api.component.annotation.Manager; @@ -21,6 +22,8 @@ import org.joml.Vector3fc; import org.joml.Vector3ic; +import java.util.Arrays; + /** * Allay Project 2023/4/8 * @@ -79,10 +82,15 @@ public void onReplace(BlockStateWithPos currentBlockState, BlockState newBlockSt @Override public void onBreak(BlockStateWithPos blockState, ItemStack usedItem, EntityPlayer player) { if (!blockState.blockState().getBlockType().getMaterial().isAlwaysDestroyable() && !usedItem.isCorrectToolFor(blockState.blockState())) return; + var dropPos = new Vector3f(blockState.pos()).add(0.5f, 0.5f, 0.5f); + if (usedItem.hasEnchantment(EnchantmentSilkTouchType.SILK_TOUCH_TYPE)) { + // 精准采集, 直接掉落方块本身 + player.getDimension().dropItem(blockState.blockState().toItemStack(), dropPos); + return; + } var drops = getDrops(blockState.blockState(), usedItem); - if (drops.length == 0) return; for (var drop : drops) { - player.getDimension().dropItem(drop, new Vector3f(blockState.pos()).add(0.5f, 0.5f, 0.5f)); + player.getDimension().dropItem(drop, dropPos); } } diff --git a/Allay-Server/src/main/java/org/allaymc/server/block/type/AllayBlockType.java b/Allay-Server/src/main/java/org/allaymc/server/block/type/AllayBlockType.java index 75facd268..96e6b4f74 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/block/type/AllayBlockType.java +++ b/Allay-Server/src/main/java/org/allaymc/server/block/type/AllayBlockType.java @@ -91,11 +91,11 @@ public static long computeSpecialValue(byte specialValueBits, BlockPropertyType. private final Set blockTags; @Getter private final Material material; + private final ItemType blockItemType; private Class injectedClass; @Getter private BlockState defaultState; - private ItemType blockItemType; private Map hashToMeta; @Getter private T blockBehavior; diff --git a/Allay-Server/src/main/java/org/allaymc/server/command/AllayCommandRegistry.java b/Allay-Server/src/main/java/org/allaymc/server/command/AllayCommandRegistry.java index 8df78c221..e4fa62a7a 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/command/AllayCommandRegistry.java +++ b/Allay-Server/src/main/java/org/allaymc/server/command/AllayCommandRegistry.java @@ -57,6 +57,7 @@ public void registerDefaultCommands() { register(new WhitelistCommand()); register(new ScoreboardCommand()); register(new TimeCommand()); + register(new EnchantCommand()); } @Override diff --git a/Allay-Server/src/main/java/org/allaymc/server/command/defaults/EnchantCommand.java b/Allay-Server/src/main/java/org/allaymc/server/command/defaults/EnchantCommand.java new file mode 100644 index 000000000..39ce72320 --- /dev/null +++ b/Allay-Server/src/main/java/org/allaymc/server/command/defaults/EnchantCommand.java @@ -0,0 +1,54 @@ +package org.allaymc.server.command.defaults; + +import org.allaymc.api.command.SimpleCommand; +import org.allaymc.api.command.tree.CommandTree; +import org.allaymc.api.container.Container; +import org.allaymc.api.container.FullContainerType; +import org.allaymc.api.entity.interfaces.EntityPlayer; +import org.allaymc.api.i18n.TrKeys; +import org.allaymc.api.item.enchantment.EnchantmentType; + +import java.util.Collection; + +/** + * Allay Project 2024/6/15 + * + * @author daoge_cmd + */ +public class EnchantCommand extends SimpleCommand { + + public EnchantCommand() { + super("enchant", TrKeys.M_COMMANDS_ENCHANT_DESCRIPTION); + } + + @Override + public void prepareCommandTree(CommandTree tree) { + tree.getRoot() + .playerTarget("player") + .enchantmentNode("enchantmentName") + .shortNum("level") + .exec(ctx -> { + Collection players = ctx.getFirstResult(); + EnchantmentType enchantmentType = ctx.getSecondResult(); + short level = ctx.getThirdResult(); + for (var player : players) { + var item = player.getContainer(FullContainerType.PLAYER_INVENTORY).getItemInHand(); + if (item == Container.EMPTY_SLOT_PLACE_HOLDER) { + // 手上没东西 + ctx.addOutput(TrKeys.M_COMMANDS_ENCHANT_NOITEM); + return ctx.fail(); + } + var incompatibleEnchantmentType = item.getIncompatibleEnchantmentType(enchantmentType); + if (incompatibleEnchantmentType != null) { + // 新附魔和已有附魔有冲突 + ctx.addOutput(TrKeys.M_COMMANDS_ENCHANT_CANTCOMBINE, incompatibleEnchantmentType.getIdentifier(), enchantmentType.getIdentifier()); + return ctx.fail(); + } + item.addEnchantment(enchantmentType, level); + player.sendItemInHandUpdate(); + ctx.addOutput(TrKeys.M_COMMANDS_ENCHANT_SUCCESS, enchantmentType.getIdentifier()); + } + return ctx.success(); + }); + } +} diff --git a/Allay-Server/src/main/java/org/allaymc/server/command/tree/AllayCommandNodeFactory.java b/Allay-Server/src/main/java/org/allaymc/server/command/tree/AllayCommandNodeFactory.java index 8dd459a98..02fe058dc 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/command/tree/AllayCommandNodeFactory.java +++ b/Allay-Server/src/main/java/org/allaymc/server/command/tree/AllayCommandNodeFactory.java @@ -4,6 +4,7 @@ import org.allaymc.api.command.tree.CommandNodeFactory; import org.allaymc.api.entity.Entity; import org.allaymc.api.entity.interfaces.EntityPlayer; +import org.allaymc.api.item.enchantment.EnchantmentType; import org.allaymc.server.command.tree.node.*; import org.cloudburstmc.protocol.bedrock.data.GameType; import org.joml.Vector3fc; @@ -26,6 +27,11 @@ public CommandNode str(String name, CommandNode parent, String defaultValue) { return new StringNode(name, parent, defaultValue); } + @Override + public CommandNode shortNum(String name, CommandNode parent, short defaultValue) { + return new ShortNode(name, parent, defaultValue); + } + @Override public CommandNode intNum(String name, CommandNode parent, int defaultValue) { return new IntNode(name, parent, defaultValue); @@ -105,4 +111,9 @@ public CommandNode pos(String name, CommandNode parent, Vector3fc defaultValue) public CommandNode wildcardTarget(String name, CommandNode parent, String defaultValue) { return new WildcardTargetNode(name, parent, defaultValue); } + + @Override + public CommandNode enchantmentNode(String name, CommandNode parent, EnchantmentType defaultValue) { + return new EnchantmentNode(name, parent, defaultValue); + } } diff --git a/Allay-Server/src/main/java/org/allaymc/server/command/tree/node/EnchantmentNode.java b/Allay-Server/src/main/java/org/allaymc/server/command/tree/node/EnchantmentNode.java new file mode 100644 index 000000000..aa496e83b --- /dev/null +++ b/Allay-Server/src/main/java/org/allaymc/server/command/tree/node/EnchantmentNode.java @@ -0,0 +1,31 @@ +package org.allaymc.server.command.tree.node; + +import org.allaymc.api.command.tree.CommandNode; +import org.allaymc.api.item.enchantment.EnchantmentRegistry; +import org.allaymc.api.item.enchantment.EnchantmentType; +import org.allaymc.api.utils.Identifier; + +import static org.allaymc.api.utils.Identifier.DEFAULT_NAMESPACE; + +/** + * Allay Project 2024/6/15 + * + * @author daoge_cmd + */ +public class EnchantmentNode extends EnumNode { + + protected static final String[] ENCHANTMENT_STR_ARRAY = + EnchantmentRegistry.getRegistry().getContent().m1().values() + .stream() + .map(type -> type.getIdentifier().path()) + .toArray(String[]::new); + + public EnchantmentNode(String name, CommandNode parent, EnchantmentType defaultValue) { + super(name, parent, defaultValue, ENCHANTMENT_STR_ARRAY); + } + + @Override + protected Object argToResult(String arg) { + return EnchantmentRegistry.getRegistry().getByK2(new Identifier(DEFAULT_NAMESPACE, arg)); + } +} diff --git a/Allay-Server/src/main/java/org/allaymc/server/command/tree/node/ShortNode.java b/Allay-Server/src/main/java/org/allaymc/server/command/tree/node/ShortNode.java new file mode 100644 index 000000000..ce2a660cf --- /dev/null +++ b/Allay-Server/src/main/java/org/allaymc/server/command/tree/node/ShortNode.java @@ -0,0 +1,40 @@ +package org.allaymc.server.command.tree.node; + +import org.allaymc.api.command.tree.BaseNode; +import org.allaymc.api.command.tree.CommandContext; +import org.allaymc.api.command.tree.CommandNode; +import org.cloudburstmc.protocol.bedrock.data.command.CommandParam; +import org.cloudburstmc.protocol.bedrock.data.command.CommandParamData; + +/** + * Allay Project 2024/6/15 + * + * @author daoge_cmd + */ +public class ShortNode extends BaseNode { + + public ShortNode(String name, CommandNode parent, short defaultValue) { + super(name, parent, defaultValue); + } + + @Override + public boolean match(CommandContext context) { + var arg = context.queryArg(); + short number; + try { + number = Short.parseShort(arg); + } catch (NumberFormatException e) { + return false; + } + context.putResult(number); + context.popArg(); + return true; + } + + @Override + public CommandParamData toNetworkData() { + var data = super.toNetworkData(); + data.setType(CommandParam.INT); + return data; + } +} diff --git a/Allay-Server/src/main/java/org/allaymc/server/item/component/common/ItemBaseComponentImpl.java b/Allay-Server/src/main/java/org/allaymc/server/item/component/common/ItemBaseComponentImpl.java index 499fc43d9..5c3627a5f 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/item/component/common/ItemBaseComponentImpl.java +++ b/Allay-Server/src/main/java/org/allaymc/server/item/component/common/ItemBaseComponentImpl.java @@ -338,6 +338,11 @@ public short getEnchantmentLevel(EnchantmentType enchantmentType) { return instance == null ? 0 : instance.getLevel(); } + @Override + public Collection getEnchantments() { + return enchantments.values(); + } + @Override public void addEnchantment(EnchantmentType enchantmentType, short level) { enchantments.put(enchantmentType, new SimpleEnchantmentInstance(enchantmentType, level)); diff --git a/Allay-Server/src/main/java/org/allaymc/server/network/processor/InventoryTransactionPacketProcessor.java b/Allay-Server/src/main/java/org/allaymc/server/network/processor/InventoryTransactionPacketProcessor.java index c13aa7b90..03bd581a9 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/network/processor/InventoryTransactionPacketProcessor.java +++ b/Allay-Server/src/main/java/org/allaymc/server/network/processor/InventoryTransactionPacketProcessor.java @@ -38,8 +38,7 @@ public void handleSync(EntityPlayer player, InventoryTransactionPacket packet) { Vector3ic blockPos = MathUtils.CBVecToJOMLVec(packet.getBlockPosition()); Vector3fc clickPos = MathUtils.CBVecToJOMLVec(packet.getClickPosition()); BlockFace blockFace = BlockFace.fromId(packet.getBlockFace()); - var inv = player.getContainer(FullContainerType.PLAYER_INVENTORY); - var itemStack = inv.getItemInHand(); + var itemStack = player.getItemInHand(); var world = player.getLocation().dimension(); switch (packet.getActionType()) { case ITEM_USE_CLICK_BLOCK -> { @@ -78,7 +77,7 @@ public void handleSync(EntityPlayer player, InventoryTransactionPacket packet) { var target = player.getDimension().getEntityByRuntimeId(packet.getRuntimeEntityId()); Preconditions.checkNotNull(target, "Player " + player.getOriginName() + " try to attack a entity which doesn't exist! Entity id: " + packet.getRuntimeEntityId()); switch (packet.getActionType()) { - case ITEM_USE_ON_ENTITY_INTERACT -> target.onInteract(player, player.getContainer(FullContainerType.PLAYER_INVENTORY).getItemInHand()); + case ITEM_USE_ON_ENTITY_INTERACT -> target.onInteract(player, player.getItemInHand()); case ITEM_USE_ON_ENTITY_ATTACK -> { EntityDamageComponent damageable; if (target instanceof EntityDamageComponent cast) { @@ -88,7 +87,7 @@ public void handleSync(EntityPlayer player, InventoryTransactionPacket packet) { return; } // TODO: Check whether the player can touch the target entity or not (to prevent cheater) - var itemInHand = player.getContainer(FullContainerType.PLAYER_INVENTORY).getItemInHand(); + var itemInHand = player.getItemInHand(); var damage = itemInHand.calculateAttackDamage(); if (damage == 0) damage = 1; var damageContainer = DamageContainer.entityAttack(player, damage); diff --git a/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerAuthInputPacketProcessor.java b/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerAuthInputPacketProcessor.java index 0a3a56bce..ff6ad04fd 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerAuthInputPacketProcessor.java +++ b/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerAuthInputPacketProcessor.java @@ -114,7 +114,7 @@ protected void startBreak(EntityPlayer player, int x, int y, int z, int blockFac breakFaceId = blockFaceId; breakBlock = player.getDimension().getBlockState(x, y, z); startBreakingTime = player.getWorld().getTick(); - needBreakingTime = breakBlock.getBlockType().getBlockBehavior().calculateBreakTime(breakBlock, player.getContainer(FullContainerType.PLAYER_INVENTORY).getItemInHand(), player); + needBreakingTime = breakBlock.getBlockType().getBlockBehavior().calculateBreakTime(breakBlock, player.getItemInHand(), player); stopBreakingTime = startBreakingTime + needBreakingTime * 20.0d; var pk = new LevelEventPacket(); pk.setType(BLOCK_START_BREAK); @@ -164,7 +164,7 @@ protected void completeBreak(EntityPlayer player, int x, int y, int z) { player.getCurrentChunk().addChunkPacket(pk); breakBlock.getBehavior().onBreak( new BlockStateWithPos(breakBlock, new Position3i(breakBlockX, breakBlockY, breakBlockZ, player.getDimension()), 0), - player.getContainer(FullContainerType.PLAYER_INVENTORY).getItemInHand(), + player.getItemInHand(), player ); world.setBlockState(breakBlockX, breakBlockY, breakBlockZ, AIR_TYPE.getDefaultState()); @@ -200,7 +200,7 @@ protected void checkInteractDistance(EntityPlayer player) { } protected void updateBreakingTime(EntityPlayer player) { - var newBreakingTime = breakBlock.getBehavior().calculateBreakTime(breakBlock, player.getContainer(FullContainerType.PLAYER_INVENTORY).getItemInHand(), player); + var newBreakingTime = breakBlock.getBehavior().calculateBreakTime(breakBlock, player.getItemInHand(), player); if (needBreakingTime == newBreakingTime) return; // 破坏时间有变化,进行修正 var currentTime = player.getWorld().getTick();