Add configuration

This commit is contained in:
Jimmy 2021-04-24 11:33:22 +12:00
parent 7df836845d
commit d18c504825
1 changed files with 48 additions and 30 deletions

View File

@ -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<PanItem> items = new ArrayList<PanItem>();
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);