Skip to content

Commit

Permalink
Add config feature - allowDuplicateTrades
Browse files Browse the repository at this point in the history
  • Loading branch information
Reldeam committed Jan 27, 2022
1 parent 1b176e4 commit 8f8b0fc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -41,6 +42,9 @@ public void onEnable() {
saveDefaultConfig();
createTradesConfig();

// set config values
this.allowDuplicateTrades = getConfig().getBoolean("allowDuplicateTrades");

// build custom trade list
List<CustomTrade> customTrades = CustomTradeLoader.loadTrades(this);
villagerAcquireTradeListener.setCustomTrades(customTrades);
Expand Down Expand Up @@ -101,5 +105,9 @@ public FileConfiguration getTradesConfig() {
public boolean isLoaded() {
return this.loaded;
}

public boolean allowDuplicateTrades() {
return this.allowDuplicateTrades;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,8 +19,13 @@

public class VillagerAcquireTradeListener implements Listener {

private final CustomVillagerTrades plugin;
private List<CustomTrade> customTrades;

public VillagerAcquireTradeListener(CustomVillagerTrades plugin) {
this.plugin = plugin;
}

@EventHandler
public void onVillagerAcquireTrade(VillagerAcquireTradeEvent event) throws FileNotFoundException{

Expand Down Expand Up @@ -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());

Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
allowDuplicateTrades: false
# 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).
36 changes: 27 additions & 9 deletions src/main/resources/trades.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -16,23 +27,30 @@ 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
ingredients:
- material: IRON_INGOT
amount: 10
maxUses: 4
experience: 2
giveExperienceToPlayer: false
chance: 0.5
professions: [MASON]

professions: [MASON]

0 comments on commit 8f8b0fc

Please sign in to comment.