mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-25 06:24:42 +00:00
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:
4 files changed
+843
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user