Merge remote-tracking branch 'origin/master'

This commit is contained in:
PretzelJohn 2023-06-29 15:06:02 -04:00
commit 7e8baf8a6c
4 changed files with 44 additions and 34 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.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())
));
}
}
}

View File

@ -1,9 +1,11 @@
package com.pretzel.dev.villagertradelimiter.listeners;
import com.google.common.collect.Iterables;
import com.pretzel.dev.villagertradelimiter.VillagerTradeLimiter;
import com.pretzel.dev.villagertradelimiter.data.Cooldown;
import com.pretzel.dev.villagertradelimiter.data.PlayerData;
import com.pretzel.dev.villagertradelimiter.settings.Settings;
import org.bukkit.Material;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -81,8 +83,8 @@ public class VillagerListener implements Listener {
//Get the items involved in the restock
final MerchantRecipe recipe = event.getRecipe();
final ItemStack result = recipe.getResult();
ItemStack ingredient1 = recipe.getIngredients().get(0);
ItemStack ingredient2 = recipe.getIngredients().get(1);
ItemStack ingredient1 = Iterables.get(recipe.getIngredients(), 0, new ItemStack(Material.AIR));
ItemStack ingredient2 = Iterables.get(recipe.getIngredients(), 1, new ItemStack(Material.AIR));
final String type = settings.getType(result, ingredient1, ingredient2);
//Get the villager's data container

View File

@ -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);
}
}

View File

@ -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();
}