Implement Phase 3: Item Attributes & Restrictions

- Add ItemTradeAttribute.java for trade restriction utilities
  - TAG_UNTRADEABLE for items that cannot be traded
  - Check existing TAG_PROFILE_SLOT and TAG_SOULBIND tags
  - canTrade() checks all three restriction types
  - getTradeBlockReason() returns localization key for UI
  - Utility methods for setting/getting each restriction type
  - clearAllRestrictions() and getRestrictionSummary() helpers

- Add AuctionBlacklist.java controller
  - Blacklist items by registry name (e.g., "minecraft:diamond_sword")
  - Blacklist entire mods by mod ID (e.g., "minecraft")
  - Blacklist items with specific NBT tags (supports dot notation)
  - isBlacklisted() and getBlacklistReason() for checking
  - Persistence via auction_blacklist.dat
  - Add/remove methods for each blacklist type

- Initialize AuctionBlacklist in CustomNpcs.java

- Add scripting API methods to ScriptItemStack.java
  - canTrade() - check if item can be traded
  - getTradeBlockReason() - get block reason
  - isUntradeable() / setUntradeable() - manage untradeable flag
  - isProfileSlotBound() / getProfileSlot() / setProfileSlotBound()
  - isSoulbound() / getSoulbindOwner() / removeSoulbind()
  - clearTradeRestrictions() - remove all restrictions
  - getRestrictionSummary() - human-readable summary
This commit is contained in:
Claude committed 2026-01-04 08:34:45 +00:00
1 parent c9f77bed05
commit dc9d15f3e3
4 files changed
+843

No files matched your search

@@ -0,0 +1,308 @@
package kamkeel.npcs.controllers.data.attribute;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.controllers.data.PlayerData;
import java.util.UUID;
/**
* Utility class for managing item trade restrictions.
* Checks and sets attributes that control whether items can be traded/auctioned.
*/
public class ItemTradeAttribute {
// New untradeable attribute - prevents all trading
public static final String TAG_UNTRADEABLE = "CNPC_Untradeable";
// Existing requirement keys (from ProfileSlotRequirement and SoulbindRequirement)
public static final String TAG_PROFILE_SLOT = "cnpc_profile_slot";
public static final String TAG_SOULBIND = "cnpc_soulbind";
// ==================== Trade Check Methods ====================
/**
* Check if item can be traded/auctioned by the given player.
* An item cannot be traded if:
* - It has the Untradeable flag
* - It is bound to a specific profile slot (ProfileSlot Bound)
* - It is soulbound to a different player
*
* @param item The item to check
* @param player The player trying to trade (null to check without player context)
* @return true if the item can be traded
*/
public static boolean canTrade(ItemStack item, EntityPlayer player) {
if (item == null) {
return true;
}
if (!item.hasTagCompound()) {
return true;
}
NBTTagCompound tag = item.getTagCompound();
// Check untradeable flag - always blocks trading
if (tag.getBoolean(TAG_UNTRADEABLE)) {
return false;
}
// Check profile slot bound - blocks trading as it's tied to a slot
if (tag.hasKey(TAG_PROFILE_SLOT)) {
return false;
}
// Check soulbound - blocks trading as it's tied to a player
if (tag.hasKey(TAG_SOULBIND)) {
// If we have a player context, check if they're the owner
if (player != null) {
String soulbindUUID = tag.getString(TAG_SOULBIND);
if (soulbindUUID != null && !soulbindUUID.isEmpty()) {
// Even if bound to this player, still can't trade
return false;
}
}
return false;
}
return true;
}
/**
* Check if item can be traded (without player context)
*/
public static boolean canTrade(ItemStack item) {
return canTrade(item, null);
}
/**
* Get the reason why an item cannot be traded.
* Returns a translation key for localization.
*
* @param item The item to check
* @return Translation key for the block reason, or null if tradeable
*/
public static String getTradeBlockReason(ItemStack item) {
if (item == null || !item.hasTagCompound()) {
return null;
}
NBTTagCompound tag = item.getTagCompound();
if (tag.getBoolean(TAG_UNTRADEABLE)) {
return "gui.auction.untradeable";
}
if (tag.hasKey(TAG_PROFILE_SLOT)) {
return "gui.auction.profileslotbound";
}
if (tag.hasKey(TAG_SOULBIND)) {
return "gui.auction.soulbound";
}
return null;
}
// ==================== Untradeable Methods ====================
/**
* Check if item is marked as untradeable
*/
public static boolean isUntradeable(ItemStack item) {
if (item == null || !item.hasTagCompound()) {
return false;
}
return item.getTagCompound().getBoolean(TAG_UNTRADEABLE);
}
/**
* Set the untradeable flag on an item
*/
public static void setUntradeable(ItemStack item, boolean untradeable) {
if (item == null) {
return;
}
if (!item.hasTagCompound()) {
item.setTagCompound(new NBTTagCompound());
}
if (untradeable) {
item.getTagCompound().setBoolean(TAG_UNTRADEABLE, true);
} else {
item.getTagCompound().removeTag(TAG_UNTRADEABLE);
}
}
// ==================== Profile Slot Bound Methods ====================
/**
* Check if item is bound to a profile slot
*/
public static boolean isProfileSlotBound(ItemStack item) {
if (item == null || !item.hasTagCompound()) {
return false;
}
return item.getTagCompound().hasKey(TAG_PROFILE_SLOT);
}
/**
* Get the profile slot an item is bound to
* @return The profile slot ID, or -1 if not bound
*/
public static int getProfileSlot(ItemStack item) {
if (item == null || !item.hasTagCompound()) {
return -1;
}
if (item.getTagCompound().hasKey(TAG_PROFILE_SLOT)) {
return item.getTagCompound().getInteger(TAG_PROFILE_SLOT);
}
return -1;
}
/**
* Bind an item to a specific profile slot
*/
public static void setProfileSlotBound(ItemStack item, int slot) {
if (item == null) {
return;
}
if (!item.hasTagCompound()) {
item.setTagCompound(new NBTTagCompound());
}
if (slot >= 0) {
item.getTagCompound().setInteger(TAG_PROFILE_SLOT, slot);
} else {
item.getTagCompound().removeTag(TAG_PROFILE_SLOT);
}
}
/**
* Bind an item to the player's current profile slot
*/
public static void bindToCurrentSlot(ItemStack item, EntityPlayer player) {
if (item == null || player == null) {
return;
}
PlayerData data = PlayerData.get(player);
if (data != null) {
setProfileSlotBound(item, data.profileSlot);
}
}
// ==================== Soulbind Methods ====================
/**
* Check if item is soulbound to any player
*/
public static boolean isSoulbound(ItemStack item) {
if (item == null || !item.hasTagCompound()) {
return false;
}
return item.getTagCompound().hasKey(TAG_SOULBIND);
}
/**
* Check if item is soulbound to a specific player
*/
public static boolean isSoulboundTo(ItemStack item, EntityPlayer player) {
if (item == null || player == null || !item.hasTagCompound()) {
return false;
}
if (!item.getTagCompound().hasKey(TAG_SOULBIND)) {
return false;
}
String soulbindUUID = item.getTagCompound().getString(TAG_SOULBIND);
return player.getUniqueID().toString().equals(soulbindUUID);
}
/**
* Get the UUID of the player the item is soulbound to
* @return The UUID string, or null if not soulbound
*/
public static String getSoulbindOwner(ItemStack item) {
if (item == null || !item.hasTagCompound()) {
return null;
}
if (item.getTagCompound().hasKey(TAG_SOULBIND)) {
return item.getTagCompound().getString(TAG_SOULBIND);
}
return null;
}
/**
* Soulbind an item to a player
*/
public static void setSoulbound(ItemStack item, EntityPlayer player) {
if (item == null || player == null) {
return;
}
if (!item.hasTagCompound()) {
item.setTagCompound(new NBTTagCompound());
}
item.getTagCompound().setString(TAG_SOULBIND, player.getUniqueID().toString());
}
/**
* Soulbind an item to a player by UUID
*/
public static void setSoulbound(ItemStack item, UUID playerUUID) {
if (item == null || playerUUID == null) {
return;
}
if (!item.hasTagCompound()) {
item.setTagCompound(new NBTTagCompound());
}
item.getTagCompound().setString(TAG_SOULBIND, playerUUID.toString());
}
/**
* Remove soulbind from an item
*/
public static void removeSoulbind(ItemStack item) {
if (item == null || !item.hasTagCompound()) {
return;
}
item.getTagCompound().removeTag(TAG_SOULBIND);
}
// ==================== Utility Methods ====================
/**
* Clear all trade restriction attributes from an item
*/
public static void clearAllRestrictions(ItemStack item) {
if (item == null || !item.hasTagCompound()) {
return;
}
NBTTagCompound tag = item.getTagCompound();
tag.removeTag(TAG_UNTRADEABLE);
tag.removeTag(TAG_PROFILE_SLOT);
tag.removeTag(TAG_SOULBIND);
}
/**
* Get a display string describing all trade restrictions on an item
* @return Human-readable string of restrictions, or empty if none
*/
public static String getRestrictionSummary(ItemStack item) {
if (item == null || !item.hasTagCompound()) {
return "";
}
StringBuilder summary = new StringBuilder();
NBTTagCompound tag = item.getTagCompound();
if (tag.getBoolean(TAG_UNTRADEABLE)) {
summary.append("Untradeable");
}
if (tag.hasKey(TAG_PROFILE_SLOT)) {
if (summary.length() > 0) summary.append(", ");
summary.append("Profile Slot Bound (Slot ").append(tag.getInteger(TAG_PROFILE_SLOT)).append(")");
}
if (tag.hasKey(TAG_SOULBIND)) {
if (summary.length() > 0) summary.append(", ");
summary.append("Soulbound");
}
return summary.toString();
}
}
@@ -60,6 +60,7 @@ import noppes.npcs.controllers.LinkedNpcController;
import noppes.npcs.controllers.MagicController;
import noppes.npcs.controllers.VaultHelper;
import noppes.npcs.controllers.CurrencyController;
import noppes.npcs.controllers.AuctionBlacklist;
import noppes.npcs.controllers.PlayerDataController;
import noppes.npcs.controllers.QuestController;
import noppes.npcs.controllers.RecipeController;
@@ -308,6 +309,7 @@ public class CustomNpcs {
public void started(FMLServerStartedEvent event) {
new VaultHelper();
new CurrencyController();
new AuctionBlacklist();
RecipeController.Instance.load();
new BankController();
DialogController.Instance.load();
@@ -0,0 +1,419 @@
package noppes.npcs.controllers;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import noppes.npcs.CustomNpcs;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.Set;
/**
* Controller for managing auction house item blacklisting.
* Allows administrators to block specific items, entire mods,
* or items with certain NBT tags from being listed.
*/
public class AuctionBlacklist {
private static final Logger logger = LogManager.getLogger(CustomNpcs.class);
public static AuctionBlacklist Instance;
// Blacklist by item registry name (e.g., "minecraft:diamond_sword")
private Set<String> blacklistedItems = new HashSet<>();
// Blacklist by mod ID (e.g., "minecraft" blocks all vanilla items)
private Set<String> blacklistedMods = new HashSet<>();
// Blacklist by NBT tag presence (e.g., items with specific custom data)
private Set<String> blacklistedNBTTags = new HashSet<>();
public AuctionBlacklist() {
Instance = this;
load();
}
// ==================== Blacklist Checking ====================
/**
* Check if an item is blacklisted from the auction house.
*
* @param item The item to check
* @return true if the item is blacklisted
*/
public boolean isBlacklisted(ItemStack item) {
if (item == null || item.getItem() == null) {
return false;
}
// Check item registry name
String itemName = Item.itemRegistry.getNameForObject(item.getItem());
if (itemName != null) {
// Direct item blacklist
if (blacklistedItems.contains(itemName)) {
return true;
}
// Also check with metadata for specific variants
String itemNameWithMeta = itemName + ":" + item.getItemDamage();
if (blacklistedItems.contains(itemNameWithMeta)) {
return true;
}
// Check mod ID
String modId = getModIdFromName(itemName);
if (modId != null && blacklistedMods.contains(modId)) {
return true;
}
}
// Check NBT tags
if (item.hasTagCompound()) {
NBTTagCompound tag = item.getTagCompound();
for (String nbtTag : blacklistedNBTTags) {
if (hasNestedTag(tag, nbtTag)) {
return true;
}
}
}
return false;
}
/**
* Get the reason why an item is blacklisted
* @return Description of why blacklisted, or null if not blacklisted
*/
public String getBlacklistReason(ItemStack item) {
if (item == null || item.getItem() == null) {
return null;
}
String itemName = Item.itemRegistry.getNameForObject(item.getItem());
if (itemName != null) {
if (blacklistedItems.contains(itemName)) {
return "Item is blacklisted: " + itemName;
}
String itemNameWithMeta = itemName + ":" + item.getItemDamage();
if (blacklistedItems.contains(itemNameWithMeta)) {
return "Item variant is blacklisted: " + itemNameWithMeta;
}
String modId = getModIdFromName(itemName);
if (modId != null && blacklistedMods.contains(modId)) {
return "Mod is blacklisted: " + modId;
}
}
if (item.hasTagCompound()) {
NBTTagCompound tag = item.getTagCompound();
for (String nbtTag : blacklistedNBTTags) {
if (hasNestedTag(tag, nbtTag)) {
return "Item has blacklisted NBT tag: " + nbtTag;
}
}
}
return null;
}
/**
* Extract mod ID from registry name (e.g., "minecraft" from "minecraft:stone")
*/
private String getModIdFromName(String registryName) {
if (registryName == null) {
return null;
}
int colonIndex = registryName.indexOf(':');
if (colonIndex > 0) {
return registryName.substring(0, colonIndex);
}
return null;
}
/**
* Check for nested NBT tag (supports dot notation like "display.Name")
*/
private boolean hasNestedTag(NBTTagCompound tag, String path) {
if (tag == null || path == null || path.isEmpty()) {
return false;
}
String[] parts = path.split("\\.");
NBTTagCompound current = tag;
for (int i = 0; i < parts.length - 1; i++) {
if (!current.hasKey(parts[i])) {
return false;
}
if (current.hasKey(parts[i], 10)) { // 10 = Compound tag
current = current.getCompoundTag(parts[i]);
} else {
return false;
}
}
return current.hasKey(parts[parts.length - 1]);
}
// ==================== Item Blacklist Management ====================
/**
* Add an item to the blacklist by registry name
* @param itemName Registry name (e.g., "minecraft:diamond_sword" or "minecraft:wool:14")
*/
public void addItem(String itemName) {
if (itemName != null && !itemName.isEmpty()) {
blacklistedItems.add(itemName);
save();
logger.info("Added item to auction blacklist: " + itemName);
}
}
/**
* Add an item to the blacklist from an ItemStack
*/
public void addItem(ItemStack item, boolean includeMeta) {
if (item == null || item.getItem() == null) {
return;
}
String itemName = Item.itemRegistry.getNameForObject(item.getItem());
if (itemName != null) {
if (includeMeta && item.getHasSubtypes()) {
addItem(itemName + ":" + item.getItemDamage());
} else {
addItem(itemName);
}
}
}
/**
* Remove an item from the blacklist
*/
public void removeItem(String itemName) {
if (blacklistedItems.remove(itemName)) {
save();
logger.info("Removed item from auction blacklist: " + itemName);
}
}
/**
* Check if a specific item is blacklisted
*/
public boolean isItemBlacklisted(String itemName) {
return blacklistedItems.contains(itemName);
}
/**
* Get all blacklisted items
*/
public Set<String> getBlacklistedItems() {
return new HashSet<>(blacklistedItems);
}
// ==================== Mod Blacklist Management ====================
/**
* Add an entire mod to the blacklist
* @param modId The mod ID (e.g., "minecraft", "customnpcs")
*/
public void addMod(String modId) {
if (modId != null && !modId.isEmpty()) {
blacklistedMods.add(modId);
save();
logger.info("Added mod to auction blacklist: " + modId);
}
}
/**
* Remove a mod from the blacklist
*/
public void removeMod(String modId) {
if (blacklistedMods.remove(modId)) {
save();
logger.info("Removed mod from auction blacklist: " + modId);
}
}
/**
* Check if a mod is blacklisted
*/
public boolean isModBlacklisted(String modId) {
return blacklistedMods.contains(modId);
}
/**
* Get all blacklisted mods
*/
public Set<String> getBlacklistedMods() {
return new HashSet<>(blacklistedMods);
}
// ==================== NBT Tag Blacklist Management ====================
/**
* Add an NBT tag to the blacklist
* Items containing this tag will be blocked
* @param tagPath The tag path (e.g., "display.Name" or "CustomData")
*/
public void addNBTTag(String tagPath) {
if (tagPath != null && !tagPath.isEmpty()) {
blacklistedNBTTags.add(tagPath);
save();
logger.info("Added NBT tag to auction blacklist: " + tagPath);
}
}
/**
* Remove an NBT tag from the blacklist
*/
public void removeNBTTag(String tagPath) {
if (blacklistedNBTTags.remove(tagPath)) {
save();
logger.info("Removed NBT tag from auction blacklist: " + tagPath);
}
}
/**
* Check if an NBT tag is blacklisted
*/
public boolean isNBTTagBlacklisted(String tagPath) {
return blacklistedNBTTags.contains(tagPath);
}
/**
* Get all blacklisted NBT tags
*/
public Set<String> getBlacklistedNBTTags() {
return new HashSet<>(blacklistedNBTTags);
}
// ==================== Persistence ====================
public void save() {
File saveDir = CustomNpcs.getWorldSaveDirectory();
if (saveDir == null) {
return;
}
try {
NBTTagCompound compound = new NBTTagCompound();
// Save blacklisted items
NBTTagList itemList = new NBTTagList();
for (String item : blacklistedItems) {
itemList.appendTag(new NBTTagString(item));
}
compound.setTag("Items", itemList);
// Save blacklisted mods
NBTTagList modList = new NBTTagList();
for (String mod : blacklistedMods) {
modList.appendTag(new NBTTagString(mod));
}
compound.setTag("Mods", modList);
// Save blacklisted NBT tags
NBTTagList nbtList = new NBTTagList();
for (String nbt : blacklistedNBTTags) {
nbtList.appendTag(new NBTTagString(nbt));
}
compound.setTag("NBTTags", nbtList);
// Write to file
File file = new File(saveDir, "auction_blacklist.dat");
File backup = new File(saveDir, "auction_blacklist.dat_old");
if (file.exists()) {
if (backup.exists()) {
backup.delete();
}
file.renameTo(backup);
}
file = new File(saveDir, "auction_blacklist.dat");
FileOutputStream fos = new FileOutputStream(file);
CompressedStreamTools.writeCompressed(compound, fos);
fos.close();
} catch (Exception e) {
logger.error("Error saving auction blacklist", e);
}
}
public void load() {
File saveDir = CustomNpcs.getWorldSaveDirectory();
if (saveDir == null) {
return;
}
blacklistedItems.clear();
blacklistedMods.clear();
blacklistedNBTTags.clear();
try {
File file = new File(saveDir, "auction_blacklist.dat");
if (!file.exists()) {
// Try backup
file = new File(saveDir, "auction_blacklist.dat_old");
if (!file.exists()) {
return;
}
}
FileInputStream fis = new FileInputStream(file);
NBTTagCompound compound = CompressedStreamTools.readCompressed(fis);
fis.close();
// Load blacklisted items
NBTTagList itemList = compound.getTagList("Items", 8); // 8 = String tag
for (int i = 0; i < itemList.tagCount(); i++) {
blacklistedItems.add(itemList.getStringTagAt(i));
}
// Load blacklisted mods
NBTTagList modList = compound.getTagList("Mods", 8);
for (int i = 0; i < modList.tagCount(); i++) {
blacklistedMods.add(modList.getStringTagAt(i));
}
// Load blacklisted NBT tags
NBTTagList nbtList = compound.getTagList("NBTTags", 8);
for (int i = 0; i < nbtList.tagCount(); i++) {
blacklistedNBTTags.add(nbtList.getStringTagAt(i));
}
logger.info("Loaded auction blacklist: {} items, {} mods, {} NBT tags",
blacklistedItems.size(), blacklistedMods.size(), blacklistedNBTTags.size());
} catch (Exception e) {
logger.error("Error loading auction blacklist", e);
}
}
/**
* Clear all blacklist entries
*/
public void clearAll() {
blacklistedItems.clear();
blacklistedMods.clear();
blacklistedNBTTags.clear();
save();
logger.info("Cleared all auction blacklist entries");
}
/**
* Get total number of blacklist entries
*/
public int getTotalEntries() {
return blacklistedItems.size() + blacklistedMods.size() + blacklistedNBTTags.size();
}
}
@@ -1,6 +1,7 @@
package noppes.npcs.scripted.item;
import com.google.common.collect.Multimap;
import kamkeel.npcs.controllers.data.attribute.ItemTradeAttribute;
import kamkeel.npcs.controllers.data.attribute.requirement.IRequirementChecker;
import kamkeel.npcs.controllers.data.attribute.requirement.RequirementCheckerRegistry;
import kamkeel.npcs.util.AttributeItemUtil;
@@ -592,4 +593,117 @@ public class ScriptItemStack implements IItemStack {
Set<String> keys = reqTag.func_150296_c();
return keys.toArray(new String[keys.size()]);
}
// ==================== Trade Restriction Methods ====================
/**
* Check if this item can be traded or auctioned.
* An item cannot be traded if it is:
* - Marked as untradeable
* - Bound to a profile slot
* - Soulbound to a player
* @return true if the item can be traded
* @since 1.11
*/
public boolean canTrade() {
return ItemTradeAttribute.canTrade(item);
}
/**
* Get the reason why this item cannot be traded.
* @return Localization key for the block reason, or null if tradeable
* @since 1.11
*/
public String getTradeBlockReason() {
return ItemTradeAttribute.getTradeBlockReason(item);
}
/**
* Check if this item is marked as untradeable
* @return true if untradeable
* @since 1.11
*/
public boolean isUntradeable() {
return ItemTradeAttribute.isUntradeable(item);
}
/**
* Set whether this item is untradeable
* @param untradeable true to mark as untradeable
* @since 1.11
*/
public void setUntradeable(boolean untradeable) {
ItemTradeAttribute.setUntradeable(item, untradeable);
}
/**
* Check if this item is bound to a profile slot
* @return true if profile slot bound
* @since 1.11
*/
public boolean isProfileSlotBound() {
return ItemTradeAttribute.isProfileSlotBound(item);
}
/**
* Get the profile slot this item is bound to
* @return Profile slot ID, or -1 if not bound
* @since 1.11
*/
public int getProfileSlot() {
return ItemTradeAttribute.getProfileSlot(item);
}
/**
* Bind this item to a specific profile slot
* @param slot The profile slot ID, or -1 to unbind
* @since 1.11
*/
public void setProfileSlotBound(int slot) {
ItemTradeAttribute.setProfileSlotBound(item, slot);
}
/**
* Check if this item is soulbound to any player
* @return true if soulbound
* @since 1.11
*/
public boolean isSoulbound() {
return ItemTradeAttribute.isSoulbound(item);
}
/**
* Get the UUID of the player this item is soulbound to
* @return UUID string, or null if not soulbound
* @since 1.11
*/
public String getSoulbindOwner() {
return ItemTradeAttribute.getSoulbindOwner(item);
}
/**
* Remove the soulbind from this item
* @since 1.11
*/
public void removeSoulbind() {
ItemTradeAttribute.removeSoulbind(item);
}
/**
* Clear all trade restrictions from this item
* (untradeable, profile slot bound, soulbound)
* @since 1.11
*/
public void clearTradeRestrictions() {
ItemTradeAttribute.clearAllRestrictions(item);
}
/**
* Get a summary of all trade restrictions on this item
* @return Human-readable string of restrictions, or empty if none
* @since 1.11
*/
public String getRestrictionSummary() {
return ItemTradeAttribute.getRestrictionSummary(item);
}
}