diff --git a/src/com/pretzel/dev/villagertradelimiter/listeners/PlayerListener.java b/src/com/pretzel/dev/villagertradelimiter/listeners/PlayerListener.java index 665c76a..f71aee3 100644 --- a/src/com/pretzel/dev/villagertradelimiter/listeners/PlayerListener.java +++ b/src/com/pretzel/dev/villagertradelimiter/listeners/PlayerListener.java @@ -5,7 +5,13 @@ import com.pretzel.dev.villagertradelimiter.data.Cooldown; import com.pretzel.dev.villagertradelimiter.data.PlayerData; import com.pretzel.dev.villagertradelimiter.lib.Util; 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.OfflinePlayer; import org.bukkit.configuration.ConfigurationSection; @@ -18,10 +24,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import java.time.Instant; -import java.util.Date; -import java.util.List; - public class PlayerListener implements Listener { private final VillagerTradeLimiter instance; 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 */ private int getBasePrice(final RecipeWrapper recipe) { - int basePrice = recipe.getIngredient1().getAmount(); + int basePrice = recipe.getIngredient1().getItemStack().getAmount(); basePrice = settings.fetchInt(recipe, "Item1.Amount", basePrice); 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) { if(item == null) return; - ingredient.setMaterialId("minecraft:"+item.getString("Material", ingredient.getMaterialId()).toLowerCase().replace("minecraft:","")); - ingredient.setAmount(item.getInt("Amount", ingredient.getAmount())); + ItemStack previous = ingredient.getItemStack(); + Material material = Material.matchMaterial(item.getString("Material", previous.getType().getKey().getKey())); + if (material != null) { + ingredient.setItemStack(new ItemStack( + material, + item.getInt("Amount", previous.getAmount()) + )); + } } } diff --git a/src/com/pretzel/dev/villagertradelimiter/wrappers/IngredientWrapper.java b/src/com/pretzel/dev/villagertradelimiter/wrappers/IngredientWrapper.java index 1b05674..5b72778 100644 --- a/src/com/pretzel/dev/villagertradelimiter/wrappers/IngredientWrapper.java +++ b/src/com/pretzel/dev/villagertradelimiter/wrappers/IngredientWrapper.java @@ -1,35 +1,35 @@ package com.pretzel.dev.villagertradelimiter.wrappers; import de.tr7zw.changeme.nbtapi.NBTCompound; +import org.bukkit.inventory.ItemStack; public class IngredientWrapper { - private final NBTCompound ingredient; - private final String materialId; - private final int amount; + private final NBTCompound recipe; + private final String key; + private final ItemStack itemStack; - /** @param ingredient The NBTCompound that contains the recipe's NBT data of the ingredient */ - public IngredientWrapper(final NBTCompound ingredient) { - this.ingredient = ingredient; - this.materialId = getMaterialId(); - this.amount = getAmount(); + /** + * @param recipe The NBTCompound that contains the recipe's NBT data of the ingredient + * @param key The key under which the recipe is located + */ + 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) */ - public String getMaterialId() { return ingredient.getString("id"); } + /** @return The {@link ItemStack} representing the data in the recipe */ + public ItemStack getItemStack() { + return recipe.getItemStack(key); + } - /** @return The number of items in the ingredient stack, between 1 and 64 */ - public int getAmount() { return ingredient.getByte("Count").intValue(); } - - - /** @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)); } + /** @param itemStack The {@link ItemStack} which will replace the item in the recipe */ + public void setItemStack(final ItemStack itemStack) { + recipe.setItemStack(key, itemStack); + } /** Resets the material ID and the amount of this ingredient to default values */ public void reset() { - setMaterialId(this.materialId); - setAmount(this.amount); + setItemStack(itemStack); } } diff --git a/src/com/pretzel/dev/villagertradelimiter/wrappers/RecipeWrapper.java b/src/com/pretzel/dev/villagertradelimiter/wrappers/RecipeWrapper.java index b796a7f..b89d569 100644 --- a/src/com/pretzel/dev/villagertradelimiter/wrappers/RecipeWrapper.java +++ b/src/com/pretzel/dev/villagertradelimiter/wrappers/RecipeWrapper.java @@ -20,9 +20,9 @@ public class RecipeWrapper { /** @param recipe The NBTCompound that contains the villager's NBT data of the recipe */ public RecipeWrapper(final NBTCompound recipe) { this.recipe = recipe; - this.ingredient1 = new IngredientWrapper(recipe.getCompound("buy")); - this.ingredient2 = new IngredientWrapper(recipe.getCompound("buyB")); - this.result = new IngredientWrapper(recipe.getCompound("sell")); + this.ingredient1 = new IngredientWrapper(recipe, "buy"); + this.ingredient2 = new IngredientWrapper(recipe, "buyB"); + this.result = new IngredientWrapper(recipe, "sell"); this.specialPrice = getSpecialPrice(); }