Skip to content

Commit

Permalink
feat: add ItemBaseComponent#getLockMode and `ItemBaseComponent#setL…
Browse files Browse the repository at this point in the history
…ockMode` methods to get and set the lock mode of an item.
  • Loading branch information
smartcmd committed Jan 25, 2025
1 parent 38bac79 commit c644e61
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
21 changes: 21 additions & 0 deletions api/src/main/java/org/allaymc/api/item/data/ItemLockMode.java
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;

Expand All @@ -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;
Expand All @@ -90,7 +93,9 @@ public class ItemBaseComponentImpl implements ItemBaseComponent {
@Setter
protected List<String> lore = new ArrayList<>();
protected Map<EnchantmentType, EnchantmentInstance> enchantments = new HashMap<>();
// TODO: item lock type
@Getter
@Setter
protected ItemLockMode lockMode = ItemLockMode.NONE;
// TODO: replace custom nbt content with pdc
@Getter
@Setter
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit c644e61

Please sign in to comment.