goldpanning/src/main/java/gg/wildfrontier/goldpanning/GoldPan.java

97 lines
2.8 KiB
Java

package gg.wildfrontier.goldpanning;
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.entity.Player;
public class GoldPan implements Runnable {
JavaPlugin plugin;
Inventory goldpan;
String pan;
Player player;
int count = 0;
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.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");
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() {
if(count>cycles) {
player.closeInventory();
return;
}
for (PanItem item : this.items) {
int slot = (int)(Math.random()*54);
if(item.spawn()) {
new AddItem(slot, item);
}
}
Bukkit.getScheduler().runTaskLater(plugin, this, (int)(Math.random()*delaymax+delaymin));
count++;
}
private class AddItem implements Runnable {
int slot;
PanItem item;
private AddItem(int slot, PanItem item) {
this.slot = slot;
this.item = item;
Bukkit.getScheduler().runTaskLater(plugin, this, item.getAddDelay());
}
public void run() {
goldpan.setItem(slot, item);
new RemoveItem(slot, item);
}
}
private class RemoveItem implements Runnable {
int slot;
private RemoveItem(int slot, PanItem item) {
this.slot = slot;
Bukkit.getScheduler().runTaskLater(plugin, this, item.getRemoveDelay());
}
public void run() {
goldpan.setItem(slot, null);
}
}
}