Skip to content

Commit

Permalink
Change data files to be per villager
Browse files Browse the repository at this point in the history
  • Loading branch information
Reldeam committed Feb 9, 2022
1 parent 775c7c1 commit 2795b18
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 275 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ ADD | MULTIPLY | MULTIPLY_ALL_MODIFIERS

# The trade gives 2 experience to the villager, but none to the player.

"ingotForEmerald":
"Iron Ingot for Emeralds":
result:
material: IRON_INGOT
amount: 10
Expand All @@ -251,7 +251,7 @@ ADD | MULTIPLY | MULTIPLY_ALL_MODIFIERS
# arrows in return, which might be a nice alternative to the vanilla trade
# for spectral arrows

"spectralArrows":
"Better Spectral Arrows":
result:
material: SPECTRAL_ARROW
amount: 5
Expand All @@ -273,7 +273,7 @@ ADD | MULTIPLY | MULTIPLY_ALL_MODIFIERS
# The trade is 20 EMERALDS and a NETHER STAR for 2 NETHER SCRAP. You can make
# this trade 2 times before the villager needs to replenish its stock.

"netheriteScrapforStar":
"Netherite Scrap for Start":
result:
material: NETHERITE_SCRAP
amount: 2
Expand All @@ -300,7 +300,7 @@ ADD | MULTIPLY | MULTIPLY_ALL_MODIFIERS

# The trades give 4 experience to the villager and the player.

"coolAxe":
"Cool Axe With Many Conditions":
result:
material: DIAMOND_AXE
enchantments:
Expand Down Expand Up @@ -330,7 +330,7 @@ ADD | MULTIPLY | MULTIPLY_ALL_MODIFIERS
# attribute modifiers that increase your armor by a large amount when you
# hold it in your hand.

"bulkyPick":
"Bulky Pick With Attributes":
result:
material: DIAMOND_PICKAXE
attributeModifiers:
Expand All @@ -354,7 +354,7 @@ ADD | MULTIPLY | MULTIPLY_ALL_MODIFIERS
# Buy a diamond for 150 units of your Vault Economy currency.
# This trade requires Vault and an Econony plugin in work.

"diamondForMoney":
"Diamond for Money":
result:
material: DIAMOND
ingredients:
Expand All @@ -366,7 +366,7 @@ ADD | MULTIPLY | MULTIPLY_ALL_MODIFIERS
# Sell a diamond for 100 units of your Vault Economy currency.
# This trade requires Vault and an Econony plugin in work.

"moneyForDiamond":
"Money for Diamond":
result:
money: 100
ingredients:
Expand All @@ -381,7 +381,7 @@ ADD | MULTIPLY | MULTIPLY_ALL_MODIFIERS
# USE THIS AT YOUR OWN RISK - If the tag is invalid you won't know about it and
# it could cause unexpected behaviour!

"coolThingy":
"Scary NBT":
result:
material: GOLD_SWORD
amount: 1
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>online.meinkraft</groupId>
<artifactId>customvillagertrades</artifactId>
<packaging>jar</packaging>
<version>1.7-SNAPSHOT</version>
<version>1.8-SNAPSHOT</version>
<name>CustomVillagerTrades</name>
<url>http://maven.apache.org</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void onEnable() {

// load villager manager
villagerManager = new VillagerManager(this);
villagerManager.load("villagers.data");
// villagerManager.load("villagers.data");

// load custom trades
customTradeManager = new CustomTradeManager(this);
Expand Down Expand Up @@ -273,7 +273,7 @@ public void onDisable() {
HandlerList.unregisterAll(inventoryCloseListener);

// save data files
villagerManager.save("villagers.data");
// villagerManager.save("villagers.data");

// allow plugin to be enabled again
this.loaded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void onTradeSelectEvent(TradeSelectEvent event) {

//TODO handle wandering taders
Villager villager = (Villager) inventory.getHolder();
VillagerData data = plugin.getVillagerManager().getData(villager);
VillagerData data = plugin.getVillagerManager().loadVillagerData(villager);

// remove money from inventory before trying to add money to the
// ingredients since this could cause issues if the denominations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ public void onVillagerAcquireTrade(VillagerAcquireTradeEvent event) {
return;
}

VillagerManager villagerManager = plugin.getVillagerManager();
VillagerData data = villagerManager.getData(villager);
data.addVanillaTrade(villager.getVillagerLevel(), event.getRecipe());
int index = data.getVanillaTrades().size() - 1;

CustomTradeManager tradeManager = plugin.getCustomTradeManager();

VillagerManager villagerManager = plugin.getVillagerManager();
VillagerData villagerData = villagerManager.loadVillagerData(villager);
villagerData.addVanillaTrade(villager.getVillagerLevel(), event.getRecipe());
int index = villagerData.getVanillaTrades().size() - 1;

List<CustomTrade> trades;
try {
trades = tradeManager.getValidTrades(villager);
} catch (VillagerNotMerchantException e) { return; }
trades = tradeManager.getValidTrades(villager, villagerData);
} catch (VillagerNotMerchantException e) {
return;
}

if(trades.size() == 0) {
// don't allow villager to acquire vanilla trade if they are disabled
Expand All @@ -64,32 +66,34 @@ public void onVillagerAcquireTrade(VillagerAcquireTradeEvent event) {
) {
event.setCancelled(true);
}
return;
}

CustomTrade trade = tradeManager.chooseRandomTrade(trades);

// it can happen if all of the trades have a zero chance
if(trade == null) return;

// chance of not getting the trade (if vanilla trades aren't disabled)
if(
plugin.isVanillaTradesAllowed() &&
!plugin.isVanillaTradesDisabledForProfession(villager.getProfession()) &&
rand.nextDouble() > trade.getChance()
) {
// keep vanilla trade
event.setRecipe(event.getRecipe());
}
else {
// set custom trade
event.setRecipe(trade.getRecipe());
data.addCustomTradeKey(
index,
trade.getKey()
);
CustomTrade trade = tradeManager.chooseRandomTrade(trades);

// it can happen if all of the trades have a zero chance
if(trade != null) {
// chance of not getting the trade (if vanilla trades aren't disabled)
if(
plugin.isVanillaTradesAllowed() &&
!plugin.isVanillaTradesDisabledForProfession(villager.getProfession()) &&
rand.nextDouble() > trade.getChance()
) {
// keep vanilla trade
event.setRecipe(event.getRecipe());
}
else {
// set custom trade
event.setRecipe(trade.getRecipe());
villagerData.addCustomTradeKey(
index,
trade.getKey()
);
}
}
}

villagerManager.saveVillagerData(villagerData);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CustomTrade getCustomTrade(String key) {

public void forceCustomTrade(Villager villager, CustomTrade customTrade) {

VillagerData data = villagerManager.getData(villager);
VillagerData data = villagerManager.loadVillagerData(villager);
if(data == null) data = villagerManager.addVillager(villager);

data.addVanillaTrade(
Expand All @@ -61,6 +61,8 @@ public void forceCustomTrade(Villager villager, CustomTrade customTrade) {
customTrade.getKey()
);

villagerManager.saveVillagerData(data);

Merchant merchant = (Merchant) villager;
List<MerchantRecipe> recipes = new ArrayList<>();
recipes.addAll(merchant.getRecipes());
Expand All @@ -71,10 +73,11 @@ public void forceCustomTrade(Villager villager, CustomTrade customTrade) {

public void removeCustomTrade(Villager villager, CustomTrade trade) {

VillagerData data = villagerManager.getData(villager);
VillagerData data = villagerManager.loadVillagerData(villager);
if(data == null) return;

data.removeCustomTrade(trade);
villagerManager.saveVillagerData(data);

Merchant merchant = (Merchant) villager;
List<MerchantRecipe> recipes = new ArrayList<>();
Expand All @@ -97,7 +100,7 @@ public void refreshTrades(Villager villager, Player player) {
Merchant merchant = (Merchant) villager;

VillagerManager villagerManager = plugin.getVillagerManager();
VillagerData data = villagerManager.getData((Villager) merchant);
VillagerData villagerData = villagerManager.loadVillagerData(villager);

List<MerchantRecipe> oldRecipes = merchant.getRecipes();
List<MerchantRecipe> newRecipes = new ArrayList<>();
Expand All @@ -107,7 +110,7 @@ public void refreshTrades(Villager villager, Player player) {
List<CustomTrade> validCustomTrades;
try {
if(plugin.forgetInvalidCustomTrades()) {
validCustomTrades = getValidTrades(villager, true);
validCustomTrades = getValidTrades(villager, villagerData, true);
}
else {
validCustomTrades = new ArrayList<>();
Expand All @@ -118,10 +121,10 @@ public void refreshTrades(Villager villager, Player player) {

int customTradeIndex = 0;
for(int index = 0; index < oldRecipes.size(); index++) {
if(data.isCustomTrade(index)) {
if(villagerData.isCustomTrade(index)) {

// get the corresponding customTradeKey for this index
String customTradeKey = data.getCustomTradeKey(customTradeIndex);
String customTradeKey = villagerData.getCustomTradeKey(customTradeIndex);
customTradeIndex++;

CustomTrade customTrade = customTrades.get(customTradeKey);
Expand All @@ -141,8 +144,8 @@ public void refreshTrades(Villager villager, Player player) {
ChatColor.AQUA + customTrade.getKey()
);

newRecipes.add(data.getVanillaTrade(index).getRecipe());
data.removeCustomTrade(customTrade);
newRecipes.add(villagerData.getVanillaTrade(index).getRecipe());
villagerData.removeCustomTrade(customTrade);
continue;

}
Expand Down Expand Up @@ -171,14 +174,15 @@ public void refreshTrades(Villager villager, Player player) {
}

merchant.setRecipes(newRecipes);
villagerManager.saveVillagerData(villagerData);

}

public void refreshTrades(Villager villager) {
refreshTrades(villager, null);
}

public List<CustomTrade> getValidTrades(Villager villager, boolean ingoreDuplicates) throws VillagerNotMerchantException {
public List<CustomTrade> getValidTrades(Villager villager, VillagerData villagerData, boolean ingoreDuplicates) throws VillagerNotMerchantException {

List<CustomTrade> validTrades = new ArrayList<>();

Expand Down Expand Up @@ -237,7 +241,7 @@ public List<CustomTrade> getValidTrades(Villager villager, boolean ingoreDuplica
if(
!ingoreDuplicates &&
!plugin.isDuplicateTradesAllowed() &&
villagerHasCustomTrade(villager, trade)
villagerHasCustomTrade(villagerData, trade)
) {
continue;
}
Expand All @@ -251,15 +255,12 @@ public List<CustomTrade> getValidTrades(Villager villager, boolean ingoreDuplica

}

public List<CustomTrade> getValidTrades(Villager villager) throws VillagerNotMerchantException {
return getValidTrades(villager, false);
public List<CustomTrade> getValidTrades(Villager villager, VillagerData villagerData) throws VillagerNotMerchantException {
return getValidTrades(villager, villagerData, false);
}

public boolean villagerHasCustomTrade(Villager villager, CustomTrade trade) {

VillagerData data = villagerManager.getData(villager);
return data.getCustomTradeKeys().contains(trade.getKey());

public boolean villagerHasCustomTrade(VillagerData villagerData, CustomTrade trade) {
return villagerData.getCustomTradeKeys().contains(trade.getKey());
}

public CustomTrade chooseRandomTrade(List<CustomTrade> trades) {
Expand All @@ -283,9 +284,9 @@ public boolean rerollCustomTrades(Villager villager) throws VillagerNotMerchantE

Random rand = new Random();

VillagerData data = villagerManager.getData(villager);
data.clearCustomTradeKeys();
List<VanillaTrade> vanillaTrades = data.getVanillaTrades();
VillagerData villagerData = villagerManager.loadVillagerData(villager);
villagerData.clearCustomTradeKeys();
List<VanillaTrade> vanillaTrades = villagerData.getVanillaTrades();

int villagerLevel = villager.getVillagerLevel();
List<MerchantRecipe> newRecipes = new ArrayList<>();
Expand All @@ -295,7 +296,7 @@ public boolean rerollCustomTrades(Villager villager) throws VillagerNotMerchantE
vanillaTrade = vanillaTrades.get(index);

villager.setVillagerLevel(vanillaTrade.getVillagerLevel());
List<CustomTrade> validCustomTrades = getValidTrades(villager);
List<CustomTrade> validCustomTrades = getValidTrades(villager, villagerData);

// no trades available - continue
if(validCustomTrades.size() == 0) {
Expand All @@ -316,7 +317,7 @@ public boolean rerollCustomTrades(Villager villager) throws VillagerNotMerchantE
else {
// set custom trade
newRecipes.add(customTrade.getRecipe());
data.addCustomTradeKey(index, customTrade.getKey());
villagerData.addCustomTradeKey(index, customTrade.getKey());
}
}

Expand All @@ -325,7 +326,7 @@ public boolean rerollCustomTrades(Villager villager) throws VillagerNotMerchantE
}

villager.setVillagerLevel(villagerLevel);

villagerManager.saveVillagerData(villagerData);
return true;

}
Expand All @@ -336,7 +337,7 @@ public void restoreVanillaTrades(Villager villager) throws VillagerNotMerchantEx
throw new VillagerNotMerchantException();
}

VillagerData data = villagerManager.getData(villager);
VillagerData data = villagerManager.loadVillagerData(villager);

List<VanillaTrade> vanillaTrades = data.getVanillaTrades();
List<MerchantRecipe> recipes = new ArrayList<>();
Expand All @@ -346,6 +347,7 @@ public void restoreVanillaTrades(Villager villager) throws VillagerNotMerchantEx

villager.setRecipes(recipes);
data.clearCustomTradeKeys();
villagerManager.saveVillagerData(data);

}

Expand Down
Loading

0 comments on commit 2795b18

Please sign in to comment.