Skip to content

Commit

Permalink
Add worlds trade property
Browse files Browse the repository at this point in the history
  • Loading branch information
Reldeam committed Feb 7, 2022
1 parent e40d265 commit 0f72250
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ villagerTypes: [<villager type>] # (optional) (default: all types) The villager
biomes: [<biome>] # (optional) (default: all biomes) The biome the villager
# must be in to acquire this trade

worlds: [<world>] # (optional) (default: all worlds) The worlds in which a
# villager can acquire this trade

editor: <editor data> # (autogenerated) DO NOT ADD/EDIT - This is generated by
# the in-game editor. It contains ingredient and result data modified via the
# in-game editor. If this field is present, then the result and ingredients
Expand Down Expand Up @@ -219,6 +222,7 @@ ADD | MULTIPLY | MULTIPLY_ALL_MODIFIERS

- [_&lt;attribute&gt;_](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/attribute/Attribute.html) - A list of valid attributes
- [_&lt;equipment slot&gt;_](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/inventory/EquipmentSlot.html) - A list of valid equipment slots
- _&lt;world&gt;_ - A string representation of world name (i.e. WORLD, WORLD_THE_END, etc.)

**trades.yml example**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public boolean isModified() {
isProfessionsModified() ||
isLevelsModified() ||
isVillagerTypesModified() ||
isBiomesModified()
isBiomesModified() ||
isWorldsModified()
) {
return true;
}
Expand Down Expand Up @@ -162,6 +163,10 @@ public boolean isBiomesModified() {
return !trade.getBiomes().equals(updates.getBiomes());
}

public boolean isWorldsModified() {
return !trade.getWorlds().equals(updates.getWorlds());
}

public void delete() {
isDeleted = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public TradeListPage addNewCustomTradeEntry(String key) {
null,
null,
null,
null,
null
);

Expand Down Expand Up @@ -257,11 +258,11 @@ public void save() {
List<String> biomes = newTrade.getBiomes().stream()
.map(Biome::name).collect(Collectors.toList());

//TODO Check if this saves correctly
config.set(tradeKey + ".professions", professions);
config.set(tradeKey + ".levels", newTrade.getLevels());
config.set(tradeKey + ".villagerTypes", villagerTypes);
config.set(tradeKey + ".biomes", biomes);
config.set(tradeKey + ".worlds", newTrade.getWorlds());

if(entry.isItemsModified()) {
String editorKey = tradeKey + ".editor";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public TradeConfigPage(GUI gui, String title) {
icons.put("biomes", new PropertyIcon(
"biomes",
Arrays.asList(new String[]{
"The biome(S) the villager must be in to be able",
"to acquire this trade. No biome means that the",
"The biome(s) the villager must be in to be able",
"to acquire this trade. No biome(s) means that the",
"villager could acquire the trade in any biome."
}),
Arrays.asList(new String[]{
Expand All @@ -160,6 +160,18 @@ public TradeConfigPage(GUI gui, String title) {
})
));

icons.put("worlds", new PropertyIcon(
"worlds",
Arrays.asList(new String[]{
"The worlds(s) the villager must be in to be able",
"to acquire this trade (CASE SENSITIVE). No world(s) means that",
"the villager could acquire the trade in any world."
}),
Arrays.asList(new String[]{
"e.g. world, world_nether, world_the_end, etc."
})
));

// create text prompts

TextInputButton maxUsesPrompt = new TextInputButton(
Expand Down Expand Up @@ -518,6 +530,58 @@ public TradeConfigPage(GUI gui, String title) {
gui.openPage(this, gui.getPlayer());
});

TextInputButton addWorldPrompt = new TextInputButton(
Material.AXOLOTL_BUCKET,
"Add world",
new PlayerPrompt(gui.getPlugin(), "Enter a world to add")
);
addWorldPrompt.onResponse(response -> {
try {
this.tradeEntry.getUpdates().addWorld(response);

icons.get("worlds").setCurrentValue(this.tradeEntry.getUpdates().getWorlds().toString());
setIcon(Slot.WORLDS_ICON.index(), icons.get("worlds"));

if(this.tradeEntry.isWorldsModified())
setIcon(Slot.WORLDS_MODIFY_INDICATOR.index(), modifiedSlot);
else setIcon(Slot.WORLDS_MODIFY_INDICATOR.index(), unmodifiedSlot);
}
catch(IllegalArgumentException exception) {
gui.getPlayer().sendMessage(
ChatColor.RED + "Failed to edit value: " +
exception.getMessage()
);
}

gui.openPage(this, gui.getPlayer());
});

TextInputButton removeWorldPrompt = new TextInputButton(
Material.WATER_BUCKET,
"Remove world",
new PlayerPrompt(gui.getPlugin(), "Enter a world to remove")
);
removeWorldPrompt.onResponse(response -> {
try {
this.tradeEntry.getUpdates().removeWorld(response);

icons.get("worlds").setCurrentValue(this.tradeEntry.getUpdates().getWorlds().toString());
setIcon(Slot.WORLDS_ICON.index(), icons.get("worlds"));

if(this.tradeEntry.isWorldsModified())
setIcon(Slot.WORLDS_MODIFY_INDICATOR.index(), modifiedSlot);
else setIcon(Slot.WORLDS_MODIFY_INDICATOR.index(), unmodifiedSlot);
}
catch(IllegalArgumentException exception) {
gui.getPlayer().sendMessage(
ChatColor.RED + "Failed to edit value: " +
exception.getMessage()
);
}

gui.openPage(this, gui.getPlayer());
});

// place prompts
setButton(Slot.MAX_USES_PROMPT.index(), "maxUsesPrompt", maxUsesPrompt);
setButton(Slot.PRICE_MULTIPLIER_PROMPT.index(), "priceMultiplierPrompt", priceMultiplierPrompt);
Expand All @@ -538,6 +602,9 @@ public TradeConfigPage(GUI gui, String title) {
setButton(Slot.BIOMES_ADD_PROMPT.index(), "addBiomePrompt", addBiomePrompt);
setButton(Slot.BIOMES_REMOVE_PROMPT.index(), "removeBiomePrompt", removeBiomePrompt);

setButton(Slot.WORLDS_ADD_PROMPT.index(), "addWorldPrompt", addWorldPrompt);
setButton(Slot.WORLDS_REMOVE_PROMPT.index(), "removeWorldPrompt", removeWorldPrompt);

// disabled slots
setIcon(4, disabledSlot);
setIcon(13, disabledSlot);
Expand All @@ -552,11 +619,6 @@ public TradeConfigPage(GUI gui, String title) {
setIcon(20, emptySlot);
setIcon(29, emptySlot);
setIcon(38, emptySlot);

setIcon(41, emptySlot);
setIcon(42, emptySlot);
setIcon(43, emptySlot);
setIcon(44, emptySlot);

setIcon(46, emptySlot);
setIcon(47, emptySlot);
Expand Down Expand Up @@ -586,6 +648,7 @@ public void setCustomTradeEntry(CustomTradeEntry tradeEntry) {
String levels = Arrays.toString(trade.getLevels().toArray());
String villagerTypes = Arrays.toString(trade.getVillagerTypes().toArray());
String biomes = Arrays.toString(trade.getBiomes().toArray());
String worlds = Arrays.toString(trade.getWorlds().toArray());

icons.get("maxUses").setCurrentValue(maxUses);
icons.get("priceMultiplier").setCurrentValue(priceMultiplier);
Expand All @@ -597,6 +660,7 @@ public void setCustomTradeEntry(CustomTradeEntry tradeEntry) {
icons.get("levels").setCurrentValue(levels);
icons.get("villagerTypes").setCurrentValue(villagerTypes);
icons.get("biomes").setCurrentValue(biomes);
icons.get("worlds").setCurrentValue(worlds);

// set previous values

Expand All @@ -612,6 +676,7 @@ public void setCustomTradeEntry(CustomTradeEntry tradeEntry) {
levels = Arrays.toString(trade.getLevels().toArray());
villagerTypes = Arrays.toString(trade.getVillagerTypes().toArray());
biomes = Arrays.toString(trade.getBiomes().toArray());
worlds = Arrays.toString(trade.getWorlds().toArray());

icons.get("maxUses").setPreviousValue(maxUses);
icons.get("priceMultiplier").setPreviousValue(priceMultiplier);
Expand All @@ -623,6 +688,7 @@ public void setCustomTradeEntry(CustomTradeEntry tradeEntry) {
icons.get("levels").setPreviousValue(levels);
icons.get("villagerTypes").setPreviousValue(villagerTypes);
icons.get("biomes").setPreviousValue(biomes);
icons.get("worlds").setPreviousValue(worlds);

// update icons
setIcon(Slot.MAX_USES_ICON.index(), icons.get("maxUses"));
Expand All @@ -635,6 +701,7 @@ public void setCustomTradeEntry(CustomTradeEntry tradeEntry) {
setIcon(Slot.LEVELS_ICON.index(), icons.get("levels"));
setIcon(Slot.VILLAGER_TYPES_ICON.index(), icons.get("villagerTypes"));
setIcon(Slot.BIOMES_ICON.index(), icons.get("biomes"));
setIcon(Slot.WORLDS_ICON.index(), icons.get("worlds"));

// update modification indication slots
updateIndicator(tradeEntry.isMaxUsesModified(), Slot.MAX_USES_MODIFY_INDICATOR);
Expand All @@ -646,6 +713,7 @@ public void setCustomTradeEntry(CustomTradeEntry tradeEntry) {
updateIndicator(tradeEntry.isLevelsModified(), Slot.LEVELS_MODIFY_INDICATOR);
updateIndicator(tradeEntry.isVillagerTypesModified(), Slot.VILLAGER_TYPES_MODIFY_INDICATOR);
updateIndicator(tradeEntry.isBiomesModified(), Slot.BIOMES_MODIFY_INDICATOR);
updateIndicator(tradeEntry.isWorldsModified(), Slot.WORLDS_MODIFY_INDICATOR);

// set navigation properties
backButton.setPage(tradeEntry.getTradeListPage());
Expand All @@ -672,6 +740,7 @@ private enum Slot {
LEVELS_ICON(15),
VILLAGER_TYPES_ICON(24),
BIOMES_ICON(33),
WORLDS_ICON(42),

MAX_USES_MODIFY_INDICATOR(0),
PRICE_MULTIPLIER_MODIFY_INDICATOR(9),
Expand All @@ -682,6 +751,7 @@ private enum Slot {
LEVELS_MODIFY_INDICATOR(14),
VILLAGER_TYPES_MODIFY_INDICATOR(23),
BIOMES_MODIFY_INDICATOR(32),
WORLDS_MODIFY_INDICATOR(41),

MAX_USES_PROMPT(3),
PRICE_MULTIPLIER_PROMPT(12),
Expand All @@ -693,11 +763,13 @@ private enum Slot {
LEVELS_ADD_PROMPT(17),
VILLAGER_TYPES_ADD_PROMPT(26),
BIOMES_ADD_PROMPT(35),
WORLDS_ADD_PROMPT(44),

PROFESSIONS_REMOVE_PROMPT(7),
LEVELS_REMOVE_PROMPT(16),
VILLAGER_TYPES_REMOVE_PROMPT(25),
BIOMES_REMOVE_PROMPT(34),
WORLDS_REMOVE_PROMPT(43),

BACK_BUTTON(45);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class CustomTrade implements Cloneable {
private List<Integer> levels;
private List<Villager.Type> villagerTypes;
private List<Biome> biomes;
private List<String> worlds;

private MerchantRecipe recipe;

Expand All @@ -43,7 +44,8 @@ public CustomTrade(
List<Villager.Profession> professions,
List<Integer> levels,
List<Villager.Type> villagerTypes,
List<Biome> biomes
List<Biome> biomes,
List<String> worlds

) {

Expand Down Expand Up @@ -79,6 +81,9 @@ public CustomTrade(

if(biomes == null) biomes = new ArrayList<>();
this.biomes = biomes;

if(worlds == null) worlds = new ArrayList<>();
this.worlds = worlds;

// create recipe
recipe = new MerchantRecipe(
Expand Down Expand Up @@ -152,6 +157,10 @@ public List<Biome> getBiomes() {
return biomes;
}

public List<String> getWorlds() {
return worlds;
}

public CustomTrade clone() {

ItemStack result = null;
Expand Down Expand Up @@ -182,6 +191,9 @@ public CustomTrade clone() {
List<Biome> biomes = new ArrayList<>();
if(this.biomes != null) biomes.addAll(this.biomes);

List<String> worlds = new ArrayList<>();
if(this.worlds != null) worlds.addAll(this.worlds);

return new CustomTrade(

getKey(),
Expand All @@ -196,7 +208,8 @@ public CustomTrade clone() {
professions,
levels,
villagerTypes,
biomes
biomes,
worlds

);

Expand Down Expand Up @@ -284,4 +297,15 @@ public void removeBiome(Biome biome) {
biomes.remove(biome);
}

public void addWorld(String worldName) {
if(worlds.contains(worldName)) {
throw new IllegalArgumentException("world already added");
}
worlds.add(worldName);
}

public void removeWorld(String worldName) {
worlds.remove(worldName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ static public Map<String, CustomTrade> loadTrades(CustomVillagerTrades plugin) {
List<Biome> biomes = CustomTradeLoader.toBiomeList(
(List<String>) tradeSection.getStringList("biomes")
);
List<String> worlds = tradeSection.getStringList("worlds");

try {
// objects
Expand Down Expand Up @@ -191,7 +192,8 @@ static public Map<String, CustomTrade> loadTrades(CustomVillagerTrades plugin) {
professions,
levels,
villagerTypes,
biomes
biomes,
worlds

);

Expand Down
Loading

0 comments on commit 0f72250

Please sign in to comment.