Compare commits

...

2 Commits

Author SHA1 Message Date
PretzelJohn e804329777
Merge pull request #7 from HarvelsX/master
Fix replacing items in NBT data:
2023-05-10 13:02:31 -04:00
HarvelsX 1fe04c1b5e
Fix replacing items in NBT data:
It is not enough to change the material and quantity, apparently there are preset NBT data;
2023-05-09 03:04:45 +03:00
3 changed files with 40 additions and 32 deletions

View File

@ -5,7 +5,13 @@ import com.pretzel.dev.villagertradelimiter.data.Cooldown;
import com.pretzel.dev.villagertradelimiter.data.PlayerData; import com.pretzel.dev.villagertradelimiter.data.PlayerData;
import com.pretzel.dev.villagertradelimiter.lib.Util; import com.pretzel.dev.villagertradelimiter.lib.Util;
import com.pretzel.dev.villagertradelimiter.settings.Settings; import com.pretzel.dev.villagertradelimiter.settings.Settings;
import com.pretzel.dev.villagertradelimiter.wrappers.*; import com.pretzel.dev.villagertradelimiter.wrappers.IngredientWrapper;
import com.pretzel.dev.villagertradelimiter.wrappers.PlayerWrapper;
import com.pretzel.dev.villagertradelimiter.wrappers.RecipeWrapper;
import com.pretzel.dev.villagertradelimiter.wrappers.VillagerWrapper;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -18,10 +24,6 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import java.time.Instant;
import java.util.Date;
import java.util.List;
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
private final VillagerTradeLimiter instance; private final VillagerTradeLimiter instance;
private final Settings settings; private final Settings settings;
@ -146,7 +148,7 @@ public class PlayerListener implements Listener {
* @return The initial price of a recipe/trade, before any discounts are applied * @return The initial price of a recipe/trade, before any discounts are applied
*/ */
private int getBasePrice(final RecipeWrapper recipe) { private int getBasePrice(final RecipeWrapper recipe) {
int basePrice = recipe.getIngredient1().getAmount(); int basePrice = recipe.getIngredient1().getItemStack().getAmount();
basePrice = settings.fetchInt(recipe, "Item1.Amount", basePrice); basePrice = settings.fetchInt(recipe, "Item1.Amount", basePrice);
return Math.min(Math.max(basePrice, 1), 64); return Math.min(Math.max(basePrice, 1), 64);
} }
@ -260,7 +262,13 @@ public class PlayerListener implements Listener {
*/ */
private void setIngredient(final ConfigurationSection item, final IngredientWrapper ingredient) { private void setIngredient(final ConfigurationSection item, final IngredientWrapper ingredient) {
if(item == null) return; if(item == null) return;
ingredient.setMaterialId("minecraft:"+item.getString("Material", ingredient.getMaterialId()).toLowerCase().replace("minecraft:","")); ItemStack previous = ingredient.getItemStack();
ingredient.setAmount(item.getInt("Amount", ingredient.getAmount())); Material material = Material.matchMaterial(item.getString("Material", previous.getType().getKey().getKey()));
if (material != null) {
ingredient.setItemStack(new ItemStack(
material,
item.getInt("Amount", previous.getAmount())
));
}
} }
} }

View File

@ -1,35 +1,35 @@
package com.pretzel.dev.villagertradelimiter.wrappers; package com.pretzel.dev.villagertradelimiter.wrappers;
import de.tr7zw.changeme.nbtapi.NBTCompound; import de.tr7zw.changeme.nbtapi.NBTCompound;
import org.bukkit.inventory.ItemStack;
public class IngredientWrapper { public class IngredientWrapper {
private final NBTCompound ingredient; private final NBTCompound recipe;
private final String materialId; private final String key;
private final int amount; private final ItemStack itemStack;
/** @param ingredient The NBTCompound that contains the recipe's NBT data of the ingredient */ /**
public IngredientWrapper(final NBTCompound ingredient) { * @param recipe The NBTCompound that contains the recipe's NBT data of the ingredient
this.ingredient = ingredient; * @param key The key under which the recipe is located
this.materialId = getMaterialId(); */
this.amount = getAmount(); public IngredientWrapper(final NBTCompound recipe, final String key) {
this.recipe = recipe;
this.key = key;
this.itemStack = getItemStack();
} }
/** @return The ingredient's material id (e.g, minecraft:enchanted_book) */ /** @return The {@link ItemStack} representing the data in the recipe */
public String getMaterialId() { return ingredient.getString("id"); } public ItemStack getItemStack() {
return recipe.getItemStack(key);
}
/** @return The number of items in the ingredient stack, between 1 and 64 */ /** @param itemStack The {@link ItemStack} which will replace the item in the recipe */
public int getAmount() { return ingredient.getByte("Count").intValue(); } public void setItemStack(final ItemStack itemStack) {
recipe.setItemStack(key, itemStack);
}
/** @param id The ingredient's material id (e.g, minecraft:enchanted_book) */
public void setMaterialId(final String id) { this.ingredient.setString("id", id); }
/** @param amount The number of items in the ingredient stack, which is clamped between 1 and 64 by this function */
public void setAmount(int amount) { this.ingredient.setByte("Count", (byte)Math.max(Math.min(amount, 64), 1)); }
/** Resets the material ID and the amount of this ingredient to default values */ /** Resets the material ID and the amount of this ingredient to default values */
public void reset() { public void reset() {
setMaterialId(this.materialId); setItemStack(itemStack);
setAmount(this.amount);
} }
} }

View File

@ -20,9 +20,9 @@ public class RecipeWrapper {
/** @param recipe The NBTCompound that contains the villager's NBT data of the recipe */ /** @param recipe The NBTCompound that contains the villager's NBT data of the recipe */
public RecipeWrapper(final NBTCompound recipe) { public RecipeWrapper(final NBTCompound recipe) {
this.recipe = recipe; this.recipe = recipe;
this.ingredient1 = new IngredientWrapper(recipe.getCompound("buy")); this.ingredient1 = new IngredientWrapper(recipe, "buy");
this.ingredient2 = new IngredientWrapper(recipe.getCompound("buyB")); this.ingredient2 = new IngredientWrapper(recipe, "buyB");
this.result = new IngredientWrapper(recipe.getCompound("sell")); this.result = new IngredientWrapper(recipe, "sell");
this.specialPrice = getSpecialPrice(); this.specialPrice = getSpecialPrice();
} }