Skip to content

Commit

Permalink
Changed config to allow probabilities for chest items #62
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jun 10, 2023
1 parent 6cf23f7 commit 561f210
Show file tree
Hide file tree
Showing 4 changed files with 792 additions and 956 deletions.
22 changes: 12 additions & 10 deletions src/main/java/world/bentobox/skygrid/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ public class Settings implements WorldSettings {
@ConfigComment("The End chest fill setting")
@ConfigEntry(path = "world.chest-fill.end")
private int chestFillEnd = 5;
@ConfigComment("Chest items will be taken randomly from this list. All items have an equal chance.")
@ConfigComment("Chest items will be taken randomly from this list according to the relative probabilities given")
@ConfigComment("Format: Material : Probability")
@ConfigComment("Material values can be found at https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html")
@ConfigEntry(path = "world.chest-items.overworld")
private List<String> chestItemsOverworld = new ArrayList<>();
private Map<Material, Integer> chestItemsOverworld = new EnumMap<>(Material.class);
@ConfigEntry(path = "world.chest-items.nether")
private List<String> chestItemsNether = new ArrayList<>();
private Map<Material, Integer> chestItemsNether = new EnumMap<>(Material.class);
@ConfigEntry(path = "world.chest-items.end")
private List<String> chestItemsEnd = new ArrayList<>();
private Map<Material, Integer> chestItemsEnd = new EnumMap<>(Material.class);

/* Blocks */
@ConfigComment("World block types. If the material cannot be placed, bedrock will be used.")
Expand Down Expand Up @@ -1283,42 +1285,42 @@ public void setChestFillEnd(int chestFillEnd) {
/**
* @return the chestItemsOverworld
*/
public List<String> getChestItemsOverworld() {
public Map<Material, Integer> getChestItemsOverworld() {
return chestItemsOverworld;
}

/**
* @param chestItemsOverworld the chestItemsOverworld to set
*/
public void setChestItemsOverworld(List<String> chestItemsOverworld) {
public void setChestItemsOverworld(Map<Material, Integer> chestItemsOverworld) {
this.chestItemsOverworld = chestItemsOverworld;
}

/**
* @return the chestItemsNether
*/
public List<String> getChestItemsNether() {
public Map<Material, Integer> getChestItemsNether() {
return chestItemsNether;
}

/**
* @param chestItemsNether the chestItemsNether to set
*/
public void setChestItemsNether(List<String> chestItemsNether) {
public void setChestItemsNether(Map<Material, Integer> chestItemsNether) {
this.chestItemsNether = chestItemsNether;
}

/**
* @return the chestItemsEnd
*/
public List<String> getChestItemsEnd() {
public Map<Material, Integer> getChestItemsEnd() {
return chestItemsEnd;
}

/**
* @param chestItemsEnd the chestItemsEnd to set
*/
public void setChestItemsEnd(List<String> chestItemsEnd) {
public void setChestItemsEnd(Map<Material, Integer> chestItemsEnd) {
this.chestItemsEnd = chestItemsEnd;
}

Expand Down
62 changes: 37 additions & 25 deletions src/main/java/world/bentobox/skygrid/generators/SkyGridPop.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package world.bentobox.skygrid.generators;

import java.util.List;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.Random;
import java.util.TreeMap;

import org.bukkit.Chunk;
import org.bukkit.Material;
Expand All @@ -28,9 +28,12 @@ public class SkyGridPop extends BlockPopulator {
private static final RandomSeries slt = new RandomSeries(27);
private final int size;
private final SkyGrid addon;
private final List<Material> chestItemsWorld;
private final List<Material> chestItemsNether;
private final List<Material> chestItemsEnd;
private final NavigableMap<Integer, Material> chestItemsWorld = new TreeMap<>();
private final NavigableMap<Integer, Material> chestItemsNether = new TreeMap<>();
private final NavigableMap<Integer, Material> chestItemsEnd = new TreeMap<>();
private int worldTotal;
private int netherTotal;
private int endTotal;
private Random random;
private Chunk chunk;

Expand All @@ -49,9 +52,18 @@ public SkyGridPop(SkyGrid addon) {
this.addon = addon;
this.size = addon.getSettings().getIslandHeight();
// Load the chest items
chestItemsWorld = addon.getSettings().getChestItemsOverworld().stream().map(Material::matchMaterial).filter(Objects::nonNull).toList();
chestItemsNether = addon.getSettings().getChestItemsNether().stream().map(Material::matchMaterial).filter(Objects::nonNull).toList();
chestItemsEnd = addon.getSettings().getChestItemsEnd().stream().map(Material::matchMaterial).filter(Objects::nonNull).toList();
for (Entry<Material, Integer> en : addon.getSettings().getChestItemsOverworld().entrySet()) {
worldTotal += en.getValue();
chestItemsWorld.put(worldTotal, en.getKey());
}
for (Entry<Material, Integer> en : addon.getSettings().getChestItemsNether().entrySet()) {
netherTotal += en.getValue();
chestItemsNether.put(netherTotal, en.getKey());
}
for (Entry<Material, Integer> en : addon.getSettings().getChestItemsEnd().entrySet()) {
endTotal += en.getValue();
chestItemsEnd.put(endTotal, en.getKey());
}
addon.log(LOADED + chestItemsWorld.size() + " chest items for world");
addon.log(LOADED + chestItemsNether.size() + " chest items for nether world");
addon.log(LOADED + chestItemsEnd.size() + " chest items for end world");
Expand Down Expand Up @@ -154,7 +166,7 @@ private void setSaplingType(Block b) {
break;
case DESERT:
b.setType(Material.DEAD_BUSH, false);
break;
break;
case SAVANNA:
b.setType(Material.ACACIA_SAPLING, false); // Acacia
break;
Expand All @@ -180,25 +192,25 @@ private void setChest(Block b) {
Inventory inv = chest.getBlockInventory();
slt.reset();
switch (b.getWorld().getEnvironment()) {
case NETHER:
for (int i = 0; !chestItemsNether.isEmpty() && i < addon.getSettings().getChestFillNether() && i < 27; i ++) {
ItemStack item = new ItemStack(this.chestItemsNether.get(random.nextInt(chestItemsNether.size())));
inv.setItem(slt.next(), item);
}
break;
case THE_END:
for (int i = 0; !chestItemsNether.isEmpty() && i < addon.getSettings().getChestFillEnd() && i < 27; i ++) {
ItemStack item = new ItemStack(this.chestItemsEnd.get(random.nextInt(chestItemsEnd.size())));
inv.setItem(slt.next(), item);
case NETHER -> fillChest(inv, chestItemsNether, addon.getSettings().getChestFillNether(), netherTotal);
case THE_END -> fillChest(inv, chestItemsEnd, addon.getSettings().getChestFillEnd(), endTotal);
default -> fillChest(inv, chestItemsWorld, addon.getSettings().getChestFill(), worldTotal);
}
}

private void fillChest(Inventory inv, NavigableMap<Integer, Material> probMap, int chestFill,
int total) {
for (int i = 0; !probMap.isEmpty() && i < chestFill && i < 27; i ++) {
Material temp = probMap.get(random.nextInt(total));
if (temp == null) {
temp = probMap.ceilingEntry(random.nextInt(total)).getValue();
}
break;
default:
for (int i = 0; !chestItemsNether.isEmpty() && i < addon.getSettings().getChestFill() && i < 27; i ++) {
ItemStack item = new ItemStack(this.chestItemsWorld.get(random.nextInt(chestItemsWorld.size())));
inv.setItem(slt.next(), item);
if (temp == null) {
temp = probMap.firstEntry().getValue();
}
break;
inv.setItem(slt.next(), new ItemStack(temp));
}

}

}

0 comments on commit 561f210

Please sign in to comment.