From 8f8b0fcb613936fce04abde06552c3f527cc182c Mon Sep 17 00:00:00 2001 From: Reldeam Date: Thu, 27 Jan 2022 11:01:03 +1100 Subject: [PATCH] Add config feature - allowDuplicateTrades --- .../CustomVillagerTrades.java | 10 +++++- .../VillagerAcquireTradeListener.java | 22 ++++++++---- src/main/resources/config.yml | 10 +++++- src/main/resources/trades.yml | 36 ++++++++++++++----- 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/main/java/online/meinkraft/customvillagertrades/CustomVillagerTrades.java b/src/main/java/online/meinkraft/customvillagertrades/CustomVillagerTrades.java index ff2dfc4..282f298 100644 --- a/src/main/java/online/meinkraft/customvillagertrades/CustomVillagerTrades.java +++ b/src/main/java/online/meinkraft/customvillagertrades/CustomVillagerTrades.java @@ -25,8 +25,9 @@ public final class CustomVillagerTrades extends JavaPlugin { private FileConfiguration tradesConfig; private boolean loaded = false; + private boolean allowDuplicateTrades = false; - private final VillagerAcquireTradeListener villagerAcquireTradeListener = new VillagerAcquireTradeListener(); + private final VillagerAcquireTradeListener villagerAcquireTradeListener = new VillagerAcquireTradeListener(this); @Override public void onEnable() { @@ -41,6 +42,9 @@ public void onEnable() { saveDefaultConfig(); createTradesConfig(); + // set config values + this.allowDuplicateTrades = getConfig().getBoolean("allowDuplicateTrades"); + // build custom trade list List customTrades = CustomTradeLoader.loadTrades(this); villagerAcquireTradeListener.setCustomTrades(customTrades); @@ -101,5 +105,9 @@ public FileConfiguration getTradesConfig() { public boolean isLoaded() { return this.loaded; } + + public boolean allowDuplicateTrades() { + return this.allowDuplicateTrades; + } } \ No newline at end of file diff --git a/src/main/java/online/meinkraft/customvillagertrades/listener/VillagerAcquireTradeListener.java b/src/main/java/online/meinkraft/customvillagertrades/listener/VillagerAcquireTradeListener.java index 14c3c04..a21a171 100644 --- a/src/main/java/online/meinkraft/customvillagertrades/listener/VillagerAcquireTradeListener.java +++ b/src/main/java/online/meinkraft/customvillagertrades/listener/VillagerAcquireTradeListener.java @@ -10,6 +10,7 @@ import org.bukkit.inventory.Merchant; import org.bukkit.inventory.MerchantRecipe; +import online.meinkraft.customvillagertrades.CustomVillagerTrades; import online.meinkraft.customvillagertrades.util.CustomTrade; import org.bukkit.event.EventHandler; @@ -18,8 +19,13 @@ public class VillagerAcquireTradeListener implements Listener { + private final CustomVillagerTrades plugin; private List customTrades; + public VillagerAcquireTradeListener(CustomVillagerTrades plugin) { + this.plugin = plugin; + } + @EventHandler public void onVillagerAcquireTrade(VillagerAcquireTradeEvent event) throws FileNotFoundException{ @@ -63,15 +69,17 @@ public void onVillagerAcquireTrade(VillagerAcquireTradeEvent event) throws FileN } // tader can't sell the same type of thing more than once - boolean resultExists = false; - for(MerchantRecipe recipe : merchant.getRecipes()) { - if(recipe.getResult().getType() == trade.getResult().getType()) { - resultExists = true; - break; + if(!this.plugin.allowDuplicateTrades()) { + boolean resultExists = false; + for(MerchantRecipe recipe : merchant.getRecipes()) { + if(recipe.getResult().getType() == trade.getResult().getType()) { + resultExists = true; + break; + } } + if(resultExists) continue; } - if(resultExists) continue; - + // replace recipe with custom recipe event.setRecipe(trade.getRecipe()); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 62f418e..28e3f3e 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1 +1,9 @@ -allowDuplicateTrades: false \ No newline at end of file +# Whether a villager can acquire the same trade more than once. +allowDuplicateTrades: false + +# true: The villager can acquire the same trade multiple times. + +# false: The villager cannot acquire multiple trades that have an item result +# of the same type (e.g. A villager cannot have two seperate trades for a +# DIAMOND_AXE even if they have different costs or different enchantments on +# the axe). \ No newline at end of file diff --git a/src/main/resources/trades.yml b/src/main/resources/trades.yml index 0608df0..0be14a5 100644 --- a/src/main/resources/trades.yml +++ b/src/main/resources/trades.yml @@ -1,5 +1,16 @@ trades: + # This trade can be acquired by a desert type villager that is in a desert or + # badlands biome, has a weaponsmith profession, and has just become either an + # apprentice (level 2) or a journeyman (level 3). There is a 10% chance they + # will acquire this trade if all of those conditions are met. + + # The trade is 16 emeralds and 4 tropical fish for a diamond axe that is + # enchanted with silk touch and has a custom name and lore. The trade can be + # made up to two times before the villager will need to refresh their stock. + + # The trades give 4 experience to the villager and the player. + - result: material: DIAMOND_AXE enchantments: @@ -16,15 +27,22 @@ trades: amount: 16 - material: TROPICAL_FISH amount: 4 - maxUses: 4 + maxUses: 2 experience: 4 - giveExperienceToPlayer: true - chance: 0.8 - professions: [MASON, BUTCHER] + chance: 0.1 + professions: [WEAPONSMITH] levels: [2, 3] - villagerTypes: [DESERT, SNOW, TAIGA] - biomes: [DESERT] - + villagerTypes: [DESERT] + biomes: [DESERT, BADLANDS] + + # This trade can be acquired by a mason with a 50% chance each time the + # villager acquires a new trade. + + # The trade is 24 emeralds for 10 iron ingots, it can be made up to 4 times + # before the villager will need to refresh their stock. + + # The trade gives 2 experience to the villager, but none to the player. + - result: material: EMERALD amount: 24 @@ -32,7 +50,7 @@ trades: - material: IRON_INGOT amount: 10 maxUses: 4 + experience: 2 giveExperienceToPlayer: false chance: 0.5 - professions: [MASON] - \ No newline at end of file + professions: [MASON] \ No newline at end of file