From c644e617c92e2ee9675e9e3043d0dce328867a9a Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Sun, 26 Jan 2025 01:23:31 +0800 Subject: [PATCH] feat: add `ItemBaseComponent#getLockMode` and `ItemBaseComponent#setLockMode` methods to get and set the lock mode of an item. --- CHANGELOG.md | 1 + .../api/item/component/ItemBaseComponent.java | 15 +++++++++++++ .../allaymc/api/item/data/ItemLockMode.java | 21 +++++++++++++++++++ .../command/defaults/GameTestCommand.java | 11 ++++++++++ .../item/component/ItemBaseComponentImpl.java | 19 ++++++++++++----- 5 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 api/src/main/java/org/allaymc/api/item/data/ItemLockMode.java diff --git a/CHANGELOG.md b/CHANGELOG.md index bdaf5b389..89bd156a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ Unless otherwise specified, any version comparison below is the comparison of se with the close reason when the form is closed. The old `Form#onClose` method is still available that just ignores the reason. - (API) Added `ChunkService#removeUnusedChunksImmediately` method that can remove unused chunks immediately. Also, the `/gc` command will call this method in all dimensions now. +- (API) Added `ItemBaseComponent#getLockMode` and `ItemBaseComponent#setLockMode` methods to get and set the lock mode of an item. ### Changed diff --git a/api/src/main/java/org/allaymc/api/item/component/ItemBaseComponent.java b/api/src/main/java/org/allaymc/api/item/component/ItemBaseComponent.java index 6ca5f44b9..ce5ae61d9 100644 --- a/api/src/main/java/org/allaymc/api/item/component/ItemBaseComponent.java +++ b/api/src/main/java/org/allaymc/api/item/component/ItemBaseComponent.java @@ -8,6 +8,7 @@ import org.allaymc.api.entity.damage.DamageContainer; import org.allaymc.api.entity.interfaces.EntityPlayer; import org.allaymc.api.item.ItemStack; +import org.allaymc.api.item.data.ItemLockMode; import org.allaymc.api.item.enchantment.EnchantmentInstance; import org.allaymc.api.item.enchantment.EnchantmentType; import org.allaymc.api.item.enchantment.type.AbstractEnchantmentProtectionType; @@ -618,4 +619,18 @@ default void clearBlockEntityNBT() { default boolean hasBlockEntityNBT() { return getBlockEntityNBT() != null; } + + /** + * Get the lock mode of the item. + * + * @return The lock mode of the item. + */ + ItemLockMode getLockMode(); + + /** + * Set the lock mode of the item. + * + * @param lockMode The lock mode of the item. + */ + void setLockMode(ItemLockMode lockMode); } diff --git a/api/src/main/java/org/allaymc/api/item/data/ItemLockMode.java b/api/src/main/java/org/allaymc/api/item/data/ItemLockMode.java new file mode 100644 index 000000000..31a6bb835 --- /dev/null +++ b/api/src/main/java/org/allaymc/api/item/data/ItemLockMode.java @@ -0,0 +1,21 @@ +package org.allaymc.api.item.data; + +/** + * Represents the lock mode of an item. + * + * @author daoge_cmd + */ +public enum ItemLockMode { + /** + * The item is not locked. + */ + NONE, + /** + * The item is locked in a slot. + */ + LOCK_IN_SLOT, + /** + * The item is locked in the inventory. + */ + LOCK_IN_INVENTORY +} diff --git a/server/src/main/java/org/allaymc/server/command/defaults/GameTestCommand.java b/server/src/main/java/org/allaymc/server/command/defaults/GameTestCommand.java index 7e6f9613f..36a0cdb7b 100644 --- a/server/src/main/java/org/allaymc/server/command/defaults/GameTestCommand.java +++ b/server/src/main/java/org/allaymc/server/command/defaults/GameTestCommand.java @@ -14,6 +14,7 @@ import org.allaymc.api.i18n.LangCode; import org.allaymc.api.i18n.TrKeys; import org.allaymc.api.item.data.ItemId; +import org.allaymc.api.item.data.ItemLockMode; import org.allaymc.api.math.MathUtils; import org.allaymc.api.registry.Registries; import org.allaymc.api.utils.AllayStringUtils; @@ -315,6 +316,16 @@ public void prepareCommandTree(CommandTree tree) { } explosion.explode(player.getDimension(), pos); return context.success(); + }, SenderType.PLAYER) + .root() + .key("lockitem") + .enumClass("mode", ItemLockMode.class) + .exec((context, player) -> { + var item = player.getItemInHand(); + item.setLockMode(context.getResult(1)); + player.notifyItemInHandChange(); + player.sendText("Item is locked in " + context.getResult(1) + " mode!"); + return context.success(); }, SenderType.PLAYER); } } diff --git a/server/src/main/java/org/allaymc/server/item/component/ItemBaseComponentImpl.java b/server/src/main/java/org/allaymc/server/item/component/ItemBaseComponentImpl.java index 81d0fbd2d..f05f6f378 100644 --- a/server/src/main/java/org/allaymc/server/item/component/ItemBaseComponentImpl.java +++ b/server/src/main/java/org/allaymc/server/item/component/ItemBaseComponentImpl.java @@ -19,6 +19,7 @@ import org.allaymc.api.item.ItemStack; import org.allaymc.api.item.component.ItemBaseComponent; import org.allaymc.api.item.component.data.ItemDataComponent; +import org.allaymc.api.item.data.ItemLockMode; import org.allaymc.api.item.enchantment.EnchantmentHelper; import org.allaymc.api.item.enchantment.EnchantmentInstance; import org.allaymc.api.item.enchantment.EnchantmentType; @@ -43,6 +44,7 @@ import java.util.*; import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicInteger; import static org.allaymc.api.item.ItemHelper.*; @@ -62,9 +64,10 @@ public class ItemBaseComponentImpl implements ItemBaseComponent { protected static final String TAG_LORE = "Lore"; protected static final String TAG_ENCHANTMENT = "ench"; protected static final String TAG_BLOCK_ENTITY = "BlockEntityTag"; + protected static final String TAG_LOCK_MODE = "minecraft:item_lock"; protected static final String TAG_CUSTOM_NBT = "CustomNBT"; - private static int STACK_NETWORK_ID_COUNTER = 1; + private static final AtomicInteger STACK_NETWORK_ID_COUNTER = new AtomicInteger(1); @Dependency protected ItemDataComponent itemDataComponent; @@ -90,7 +93,9 @@ public class ItemBaseComponentImpl implements ItemBaseComponent { @Setter protected List lore = new ArrayList<>(); protected Map enchantments = new HashMap<>(); - // TODO: item lock type + @Getter + @Setter + protected ItemLockMode lockMode = ItemLockMode.NONE; // TODO: replace custom nbt content with pdc @Getter @Setter @@ -111,12 +116,12 @@ public ItemBaseComponentImpl(ItemStackInitInfo initInfo) { Preconditions.checkArgument(specifiedNetworkId > 0, "Specified ItemStack network id must be greater than 0"); this.stackNetworkId = specifiedNetworkId; } else if (initInfo.autoAssignStackNetworkId()) { - this.stackNetworkId = STACK_NETWORK_ID_COUNTER++; + this.stackNetworkId = STACK_NETWORK_ID_COUNTER.getAndIncrement(); } } public static int getCurrentStackNetworkIdCounter() { - return STACK_NETWORK_ID_COUNTER; + return STACK_NETWORK_ID_COUNTER.get(); } @OnInitFinish @@ -139,6 +144,8 @@ public void loadExtraTag(NbtMap extraTag) { extraTag.listenForCompound(TAG_BLOCK_ENTITY, nbt -> this.blockEntityNBT = nbt); + extraTag.listenForByte(TAG_LOCK_MODE, lockMode -> this.lockMode = ItemLockMode.values()[lockMode]); + extraTag.listenForCompound(TAG_CUSTOM_NBT, customNbt -> this.customNBTContent = customNbt); var event = new CItemLoadExtraTagEvent(extraTag); @@ -174,7 +181,9 @@ public NbtMap saveExtraTag() { nbtBuilder.putCompound(TAG_BLOCK_ENTITY, blockEntityNBT); } - // TODO: item lock type + if (lockMode != ItemLockMode.NONE) { + nbtBuilder.putByte(TAG_LOCK_MODE, (byte) lockMode.ordinal()); + } // Custom NBT content if (!customNBTContent.isEmpty()) {