Edit Minecraft trade selection clearing again

This commit is contained in:
fullwall 2024-09-16 19:06:58 +08:00
parent 15d31aa357
commit 7f9b4c629c
6 changed files with 19 additions and 77 deletions

View File

@ -662,14 +662,14 @@ public class HologramTrait extends Trait {
NPC npc = registry().createNPCUsingItem(EntityType.ITEM_DISPLAY, "", itemStack);
npc.data().setPersistent(NPC.Metadata.NAMEPLATE_VISIBLE, false);
if (itemMatcher.group(2) != null) {
String modify = itemMatcher.group(2).substring(1);
for (ChatColor color : ChatColor.values()) {
if (itemMatcher.group(2).equalsIgnoreCase(color.name())) {
npc.getOrAddTrait(ScoreboardTrait.class)
.setColor(Util.matchEnum(ChatColor.values(), itemMatcher.group(2)));
if (modify.equalsIgnoreCase(color.name())) {
npc.getOrAddTrait(ScoreboardTrait.class).setColor(color);
return npc;
}
}
Bukkit.getUnsafe().modifyItemStack(itemStack, itemMatcher.group(2));
Bukkit.getUnsafe().modifyItemStack(itemStack, modify);
npc.setItemProvider(() -> itemStack.clone());
}
return npc;
@ -707,17 +707,17 @@ public class HologramTrait extends Trait {
itemNPC = registry().createNPCUsingItem(Util.getFallbackEntityType("ITEM", "DROPPED_ITEM"), "", itemStack);
itemNPC.data().setPersistent(NPC.Metadata.NAMEPLATE_VISIBLE, false);
if (itemMatcher.group(2) != null) {
String modify = itemMatcher.group(2).substring(1);
ChatColor matched = null;
for (ChatColor color : ChatColor.values()) {
if (itemMatcher.group(2).equalsIgnoreCase(color.name())) {
itemNPC.getOrAddTrait(ScoreboardTrait.class)
.setColor(Util.matchEnum(ChatColor.values(), itemMatcher.group(2)));
if (modify.equalsIgnoreCase(color.name())) {
itemNPC.getOrAddTrait(ScoreboardTrait.class).setColor(color);
matched = color;
break;
}
}
if (matched == null) {
Bukkit.getUnsafe().modifyItemStack(itemStack, itemMatcher.group(2));
Bukkit.getUnsafe().modifyItemStack(itemStack, modify);
itemNPC.setItemProvider(() -> itemStack.clone());
}
}
@ -920,7 +920,7 @@ public class HologramTrait extends Trait {
}
}
private static final Pattern ITEM_MATCHER = Pattern.compile("<item:((?:minecraft:)?[a-zA-Z0-9_ ]*?)([:].*?)?>");
private static final Pattern ITEM_MATCHER = Pattern.compile("<item:((?:minecraft:)?[a-zA-Z0-9_ ]*?)(:.*?)?>");
private static boolean SUPPORTS_DISPLAY = false;
static {
try {

View File

@ -2,7 +2,6 @@ package net.citizensnpcs.trait;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;
@ -12,6 +11,7 @@ import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
@ -34,7 +34,6 @@ import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
@ -76,7 +75,6 @@ import net.citizensnpcs.trait.shop.PermissionAction;
import net.citizensnpcs.trait.shop.PermissionAction.PermissionActionGUI;
import net.citizensnpcs.trait.shop.StoredShops;
import net.citizensnpcs.util.InventoryMultiplexer;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
/**
@ -961,7 +959,6 @@ public class ShopTrait extends Trait {
this.shop = shop;
this.player = player;
Map<Integer, NPCShopItem> tradesMap = Maps.newHashMap();
Set<Integer> clearComponentPredicates = Sets.newHashSet();
Merchant merchant = Bukkit.createMerchant(shop.getTitle());
List<MerchantRecipe> recipes = Lists.newArrayList();
for (NPCShopPage page : shop.pages) {
@ -974,11 +971,17 @@ public class ShopTrait extends Trait {
if (!(action instanceof ItemAction))
continue;
ItemAction ia = (ItemAction) action;
if (!ia.compareSimilarity && ia.items.size() > 0) {
clearComponentPredicates.add(recipes.size());
}
for (ItemStack stack : ia.items) {
stack = stack.clone();
if (!ia.compareSimilarity && ia.metaFilter.size() > 0) {
// Minecraft implements its own trade selection logic on the client
// Clear the custom component part of the itemstack since we will check it later anyway
ItemMeta im = stack.getItemMeta();
for (NamespacedKey nk : Lists.newArrayList(im.getPersistentDataContainer().getKeys())) {
im.getPersistentDataContainer().remove(nk);
}
stack.setItemMeta(im);
}
recipe.addIngredient(stack);
if (recipe.getIngredients().size() == 2)
break;
@ -991,7 +994,6 @@ public class ShopTrait extends Trait {
}
}
merchant.setRecipes(recipes);
NMS.clearMerchantComponentPredicates(merchant, clearComponentPredicates);
trades = tradesMap;
view = player.openMerchant(merchant, true);
}

View File

@ -9,7 +9,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;
@ -37,7 +36,6 @@ import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Vector;
@ -155,10 +153,6 @@ public class NMS {
BRIDGE.cancelMoveDestination(entity);
}
public static void clearMerchantComponentPredicates(Merchant merchant, Set<Integer> clearComponentPredicates) {
BRIDGE.clearMerchantComponentPredicates(merchant, clearComponentPredicates);
}
public static Iterable<Object> createBundlePacket(List<Object> packets) {
return BRIDGE.createBundlePacket(packets);
}

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.util;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
@ -24,7 +23,6 @@ import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Vector;
@ -65,10 +63,6 @@ public interface NMSBridge {
public void cancelMoveDestination(Entity entity);
public default void clearMerchantComponentPredicates(Merchant merchant, Set<Integer> clearComponentPredicates) {
// TODO: implement for <=1.19.4
}
public default Iterable<Object> createBundlePacket(List<Object> packets) {
return packets;
}

View File

@ -9,7 +9,6 @@ import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
@ -40,7 +39,6 @@ import org.bukkit.craftbukkit.v1_20_R4.event.CraftEventFactory;
import org.bukkit.craftbukkit.v1_20_R4.event.CraftPortalEvent;
import org.bukkit.craftbukkit.v1_20_R4.inventory.CraftInventoryAnvil;
import org.bukkit.craftbukkit.v1_20_R4.inventory.CraftInventoryView;
import org.bukkit.craftbukkit.v1_20_R4.inventory.CraftMerchant;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Player;
@ -48,7 +46,6 @@ import org.bukkit.entity.Tameable;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Vector;
@ -263,7 +260,6 @@ import net.citizensnpcs.util.PlayerAnimation;
import net.citizensnpcs.util.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponentPredicate;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.ByteArrayTag;
import net.minecraft.nbt.CompoundTag;
@ -360,8 +356,6 @@ import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.trading.ItemCost;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.dimension.end.EndDragonFight;
@ -502,24 +496,6 @@ public class NMSImpl implements NMSBridge {
}
}
@Override
public void clearMerchantComponentPredicates(Merchant merchant, Set<Integer> clearComponentPredicates) {
net.minecraft.world.item.trading.Merchant handle = ((CraftMerchant) merchant).getMerchant();
Iterator<MerchantOffer> itr = handle.getOffers().iterator();
for (int i = 0; itr.hasNext(); i++) {
MerchantOffer offer = itr.next();
if (!clearComponentPredicates.contains(i))
continue;
offer.baseCostA = new ItemCost(offer.baseCostA.item(), offer.baseCostA.count(),
DataComponentPredicate.EMPTY, offer.baseCostA.itemStack());
ItemCost costB = offer.getItemCostB().orElseGet(() -> null);
if (costB != null) {
offer.costB = Optional
.of(new ItemCost(costB.item(), costB.count(), DataComponentPredicate.EMPTY, costB.itemStack()));
}
}
}
@Override
@SuppressWarnings("rawtypes")
public Iterable<Object> createBundlePacket(List source) {

View File

@ -9,7 +9,6 @@ import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
@ -38,7 +37,6 @@ import org.bukkit.craftbukkit.v1_21_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_21_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_21_R1.event.CraftEventFactory;
import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftInventoryAnvil;
import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftMerchant;
import org.bukkit.craftbukkit.v1_21_R1.inventory.view.CraftAnvilView;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.FishHook;
@ -48,7 +46,6 @@ import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.entity.EntityKnockbackEvent.KnockbackCause;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Vector;
@ -263,7 +260,6 @@ import net.citizensnpcs.util.PlayerAnimation;
import net.citizensnpcs.util.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponentPredicate;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.ByteArrayTag;
import net.minecraft.nbt.CompoundTag;
@ -360,8 +356,6 @@ import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.trading.ItemCost;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.dimension.end.EndDragonFight;
@ -482,24 +476,6 @@ public class NMSImpl implements NMSBridge {
}
}
@Override
public void clearMerchantComponentPredicates(Merchant merchant, Set<Integer> clearComponentPredicates) {
net.minecraft.world.item.trading.Merchant handle = ((CraftMerchant) merchant).getMerchant();
Iterator<MerchantOffer> itr = handle.getOffers().iterator();
for (int i = 0; itr.hasNext(); i++) {
MerchantOffer offer = itr.next();
if (!clearComponentPredicates.contains(i))
continue;
offer.baseCostA = new ItemCost(offer.baseCostA.item(), offer.baseCostA.count(),
DataComponentPredicate.EMPTY, offer.baseCostA.itemStack());
ItemCost costB = offer.getItemCostB().orElseGet(() -> null);
if (costB != null) {
offer.costB = Optional
.of(new ItemCost(costB.item(), costB.count(), DataComponentPredicate.EMPTY, costB.itemStack()));
}
}
}
@Override
@SuppressWarnings("rawtypes")
public Iterable<Object> createBundlePacket(List source) {