From d18c50482511170bbcc99ff1196494578bfd95ec Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sat, 24 Apr 2021 11:33:22 +1200 Subject: [PATCH] Add configuration --- .../gg/wildfrontier/goldpanning/GoldPan.java | 78 ++++++++++++------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/src/main/java/gg/wildfrontier/goldpanning/GoldPan.java b/src/main/java/gg/wildfrontier/goldpanning/GoldPan.java index e847cb0..9a69fc3 100644 --- a/src/main/java/gg/wildfrontier/goldpanning/GoldPan.java +++ b/src/main/java/gg/wildfrontier/goldpanning/GoldPan.java @@ -1,73 +1,91 @@ package gg.wildfrontier.goldpanning; -import java.io.Console; + +import java.util.ArrayList; +import java.util.List; import java.util.logging.Level; import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.Inventory; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitRunnable; -import org.bukkit.scheduler.BukkitTask; import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.Material; + + public class GoldPan implements Runnable { JavaPlugin plugin; Inventory goldpan; + String pan; Player player; int count = 0; - int maxcount; - public GoldPan(JavaPlugin plugin, Player player, String name) { + int cycles, delaymin, delaymax; + List items = new ArrayList(); + public GoldPan(JavaPlugin plugin, Player player, String pan) { this.plugin = plugin; this.player = player; - this.maxcount = (int)(Math.random()*50+5); - - this.goldpan = Bukkit.createInventory(this.player, 54, name); + this.pan = pan; + int cyclesmin = plugin.getConfig().getInt("cycles.min"); + int cyclesmax = plugin.getConfig().getInt("cycles.max"); + this.cycles = (int)(Math.random()*cyclesmax+cyclesmin); + this.delaymin = plugin.getConfig().getInt("cycles.delaymin"); + this.delaymax = plugin.getConfig().getInt("cycles.delaymax"); + + ConfigurationSection items = plugin.getConfig().getConfigurationSection("pans."+pan+".items"); + for (String item : items.getKeys(false)) { + int chance = plugin.getConfig().getInt("pans."+pan+".items."+item+".chance"); + int adddelaymin = plugin.getConfig().getInt("pans."+pan+".items."+item+".delay.add.min"); + int adddelaymax = plugin.getConfig().getInt("pans."+pan+".items."+item+".delay.add.max"); + int removedelaymin = plugin.getConfig().getInt("pans."+pan+".items."+item+".delay.remove.min"); + int removedelaymax = plugin.getConfig().getInt("pans."+pan+".items."+item+".delay.remove.max"); + plugin.getLogger().log(Level.INFO, item+String.valueOf(chance)); + this.items.add(new PanItem(item, chance, adddelaymin, adddelaymax, + removedelaymin, removedelaymax)); + + } + + //create and open an inventory + String displayname = plugin.getConfig().getString("pans."+pan+".name"); + this.goldpan = Bukkit.createInventory(this.player, 54, displayname); this.player.openInventory(this.goldpan); + //kick it off run(); } public void run() { - ItemStack goldnugget = new ItemStack(Material.getMaterial("GOLD_NUGGET"), 1); - ItemStack gravel = new ItemStack(Material.GRAVEL, 1); - - if(count>maxcount) { + if(count>cycles) { player.closeInventory(); return; } + for (PanItem item : this.items) { + int slot = (int)(Math.random()*54); + if(item.spawn()) { + new AddItem(slot, item); + } + } - int slot = (int)(Math.random()*54); - if((Math.random()*100)<75) { - new AddItem(slot, goldnugget); - } - slot = (int)(Math.random()*54); - if((Math.random()*100)<90) { - new AddItem(slot, gravel); - } - Bukkit.getScheduler().runTaskLater(plugin, this, (int)(Math.random()*25+5)); - plugin.getLogger().log(Level.INFO, String.valueOf(count)); + Bukkit.getScheduler().runTaskLater(plugin, this, (int)(Math.random()*delaymax+delaymin)); count++; } private class AddItem implements Runnable { int slot; - ItemStack item; - private AddItem(int slot, ItemStack item) { + PanItem item; + private AddItem(int slot, PanItem item) { this.slot = slot; this.item = item; - Bukkit.getScheduler().runTaskLater(plugin, this, (int)(Math.random()*25+5)); + Bukkit.getScheduler().runTaskLater(plugin, this, item.getAddDelay()); } public void run() { goldpan.setItem(slot, item); - new RemoveItem(slot); + new RemoveItem(slot, item); } } private class RemoveItem implements Runnable { int slot; - private RemoveItem(int slot) { + private RemoveItem(int slot, PanItem item) { this.slot = slot; - Bukkit.getScheduler().runTaskLater(plugin, this, (int)(Math.random()*25+5)); + Bukkit.getScheduler().runTaskLater(plugin, this, item.getRemoveDelay()); } public void run() { goldpan.setItem(slot, null);