Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package redlimerl.spigot.randombox;
- import net.milkbowl.vault.economy.Economy;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.block.ShulkerBox;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.inventory.EquipmentSlot;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import redlimerl.spigot.randombox.files.CustomConfig;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- public class SpawnBox implements Listener {
- private CustomConfig customConfig = new CustomConfig();
- private FileConfiguration config = customConfig.get();
- private HashMap<String, Integer> spawnTime = new HashMap<>();
- private HashMap<Location, List<Player>> cmdDelay = new HashMap<>();
- private Economy econ = Main.econ;
- @SuppressWarnings("deprecation")
- @EventHandler
- public void CloseInv(PlayerInteractEvent e) {
- if (e.getHand() == null) {
- return;
- }
- if (e.getHand() == EquipmentSlot.HAND && e.getClickedBlock() != null) {
- if (e.getClickedBlock().getType() == Material.WHITE_SHULKER_BOX) {
- if (e.getClickedBlock() == null) {
- return;
- }
- ShulkerBox box = (ShulkerBox) e.getClickedBlock().getState();
- if (box.getInventory().getTitle().contains(color(Main.getInstance().getConfig().getString("box.title")))
- && box.getInventory().getSize() == 27) {
- if (box.getInventory().getTitle().contains(" - ")) {
- int a = 0;
- while (!config.getString(a + ".name").equals(box.getInventory().getTitle().split(" - ")[1])) {
- a++;
- }
- int id = a;
- int stack = 0;
- if (customConfig.get().getItemStack(id + ".unlock.card").getType() != Material.AIR) {
- if (!e.getPlayer().getInventory().contains(customConfig.get().getItemStack(id + ".unlock.card").getType())) {
- e.getPlayer().sendMessage(ChatColor.RED + "It is no item to open the box!");
- e.setCancelled(true);
- return;
- }
- for (int i = 0; i < e.getPlayer().getInventory().getSize(); i++) {
- if (e.getPlayer().getInventory().getItem(i) == null) {
- continue;
- }
- if (e.getPlayer().getInventory().getItem(i).getType().equals(customConfig.get().getItemStack(id + ".unlock.card").getType())) {
- if (customConfig.get().getItemStack(id + ".unlock.card").hasItemMeta()) {
- if (e.getPlayer().getInventory().getItem(i).getItemMeta().equals(customConfig.get().getItemStack(id + ".unlock.card").getItemMeta())) {
- stack = stack + e.getPlayer().getInventory().getItem(i).getAmount();
- }
- } else {
- stack = stack + e.getPlayer().getInventory().getItem(i).getAmount();
- }
- }
- }
- if (stack < customConfig.get().getItemStack(id + ".unlock.card").getAmount()) {
- e.getPlayer().sendMessage(ChatColor.RED + "It is no item to open the box!");
- e.setCancelled(true);
- return;
- }
- }
- if (customConfig.get().getInt(id + ".unlock.money") != 0 && Main.getInstance().getServer().getPluginManager().getPlugin("Vault") != null) {
- if (econ.getBalance(e.getPlayer()) > customConfig.get().getInt(id + ".unlock.money")) {
- e.getPlayer().sendMessage(ChatColor.RED + "There is not enough money to open the box.");
- e.setCancelled(true);
- return;
- }
- }
- if (Main.getInstance().getServer().getPluginManager().getPlugin("Vault") != null && econ != null) {
- econ.withdrawPlayer(e.getPlayer(), customConfig.get().getInt(id + ".unlock.money"));
- } else if (customConfig.get().getInt(id + ".unlock.money") != 0) {
- e.getPlayer().sendMessage(ChatColor.RED + "The \"Vault\" plugin did not exist, we could not deduct money.");
- }
- if (cmdDelay.get(e.getClickedBlock().getLocation()) != null) {
- if (!cmdDelay.get(e.getClickedBlock().getLocation()).contains(e.getPlayer())) {
- int max = customConfig.get().getItemStack(id + ".unlock.card").getAmount();
- for (int i = 0; i < e.getPlayer().getInventory().getSize(); i++) {
- if (max > 0) {
- if (e.getPlayer().getInventory().getItem(i) == null) {
- continue;
- }
- if (e.getPlayer().getInventory().getItem(i).getType().equals(customConfig.get().getItemStack(id + ".unlock.card").getType())) {
- if (customConfig.get().getItemStack(id + ".unlock.card").hasItemMeta()) {
- if (!e.getPlayer().getInventory().getItem(i).getItemMeta().equals(customConfig.get().getItemStack(id + ".unlock.card").getItemMeta())) {
- continue;
- }
- }
- int count = e.getPlayer().getInventory().getItem(i).getAmount();
- if (count > max) {
- ItemStack item = e.getPlayer().getInventory().getItem(i).clone();
- item.setAmount(count - max);
- e.getPlayer().getInventory().setItem(i, item);
- max = 0;
- } else {
- max = max - count;
- e.getPlayer().getInventory().clear(i);
- }
- }
- }
- }
- }
- } else {
- int max = customConfig.get().getItemStack(id + ".unlock.card").getAmount();
- for (int i = 0; i < e.getPlayer().getInventory().getSize(); i++) {
- if (max > 0) {
- if (e.getPlayer().getInventory().getItem(i) == null) {
- continue;
- }
- if (e.getPlayer().getInventory().getItem(i).getType().equals(customConfig.get().getItemStack(id + ".unlock.card").getType())) {
- if (customConfig.get().getItemStack(id + ".unlock.card").hasItemMeta()) {
- if (!e.getPlayer().getInventory().getItem(i).getItemMeta().equals(customConfig.get().getItemStack(id + ".unlock.card").getItemMeta())) {
- continue;
- }
- }
- int count = e.getPlayer().getInventory().getItem(i).getAmount();
- if (count > max) {
- ItemStack item = e.getPlayer().getInventory().getItem(i).clone();
- item.setAmount(count - max);
- e.getPlayer().getInventory().setItem(i, item);
- max = 0;
- } else {
- max = max - count;
- e.getPlayer().getInventory().clear(i);
- }
- }
- }
- }
- }
- if (cmdDelay.get(e.getClickedBlock().getLocation()) != null) {
- if (!cmdDelay.get(e.getClickedBlock().getLocation()).contains(e.getPlayer())) {
- List<String> list = customConfig.get().getStringList(id + ".commands");
- for (String cmd : list) {
- cmd = cmd.replace("%playername%", e.getPlayer().getName()).replace("%name%", customConfig.get().getString(id + ".name"))
- .replace("%id%", String.valueOf(id)).replace("%x%", String.valueOf(e.getClickedBlock().getLocation().getBlockX())).
- replace("%y%", String.valueOf(e.getClickedBlock().getLocation().getBlockY())).
- replace("%z%", String.valueOf(e.getClickedBlock().getLocation().getBlockZ()));
- IdManager.cmdOp(e.getPlayer(), cmd);
- }
- }
- } else {
- List<String> list = customConfig.get().getStringList(id + ".commands");
- for (String cmd : list) {
- cmd = cmd.replace("%playername%", e.getPlayer().getName()).replace("%name%", customConfig.get().getString(id + ".name"))
- .replace("%id%", String.valueOf(id)).replace("%x%", String.valueOf(e.getClickedBlock().getLocation().getBlockX())).
- replace("%y%", String.valueOf(e.getClickedBlock().getLocation().getBlockY())).
- replace("%z%", String.valueOf(e.getClickedBlock().getLocation().getBlockZ()));
- IdManager.cmdOp(e.getPlayer(), cmd);
- }
- }
- List<Player> li;
- if (cmdDelay.get(e.getClickedBlock().getLocation()) == null) {
- li = new ArrayList<>();
- } else { li = cmdDelay.get(e.getClickedBlock().getLocation()); }
- li.add(e.getPlayer());
- cmdDelay.put(e.getClickedBlock().getLocation(), li);
- }
- }
- return;
- }
- }
- if (e.getHand() == EquipmentSlot.HAND && e.getItem() != null) {
- int c = 0, p = -1, a = 0;
- while (customConfig.get().get(String.valueOf(c)) != null) {
- if (e.getItem().getType().equals(customConfig.get().getItemStack(c+".unlock.key").getType())) {
- if (customConfig.get().getItemStack(c+".unlock.key").hasItemMeta()) {
- if (!e.getItem().getItemMeta().equals(customConfig.get().getItemStack(c+".unlock.key").getItemMeta())) {
- continue;
- }
- }
- p = c;
- a++;
- }
- c++;
- }
- if (p == -1) { return; }
- if (e.getItem().getAmount() < customConfig.get().getItemStack(p + ".unlock.key").getAmount()) {
- return;
- }
- if (a > 1) {
- e.getPlayer().sendMessage(ChatColor.RED+"There are two or more boxes of the same key.");
- return;
- }
- if (customConfig.get().getInt(p+".unlock.money") != 0 && Main.getInstance().getServer().getPluginManager().getPlugin("Vault") != null) {
- if (econ.getBalance(e.getPlayer()) > customConfig.get().getInt(p+".unlock.money")) {
- e.getPlayer().sendMessage(ChatColor.RED+"There is not enough money to open the box.");
- return;
- }
- }
- IdManager.cmdOp(e.getPlayer(), "rb open "+p);
- e.getItem().setAmount(e.getItem().getAmount()-customConfig.get().getItemStack(p + ".unlock.key").getAmount());
- if (Main.getInstance().getServer().getPluginManager().getPlugin("Vault") != null && econ != null) {
- econ.withdrawPlayer(e.getPlayer(), customConfig.get().getInt(p + ".unlock.money"));
- } else if (customConfig.get().getInt(p + ".unlock.money") != 0) {
- e.getPlayer().sendMessage(ChatColor.RED+"The \"Vault\" plugin did not exist, we could not deduct money.");
- }
- }
- }
- void spawn() {
- int c = 0;
- while (customConfig.get().get(String.valueOf(c)) != null) {
- if (spawnTime.get(config.getString(c+".name")) == null || spawnTime.get(config.getString(c+".name")) <= 0) {
- if (config.getInt(c + ".spawn.delay") != 0 && config.getInt(c + ".spawn.remove") != 0 ) {
- spawnTime.put(config.getString(c + ".name"), config.getInt(c + ".spawn.delay"));
- }
- }
- if (spawnTime.get(config.getString(c + ".name")) != null) {
- int x = 0, y = 0, z = 0, size = 0; String w = "";
- StringBuilder ws = new StringBuilder();
- spawnTime.put(config.getString(c + ".name"), spawnTime.get(config.getString(c + ".name")) - 1);
- if (spawnTime.get(config.getString(c + ".name")) == 0) {
- spawnTime.put(config.getString(c + ".name"), config.getInt(c + ".spawn.delay"));
- for (int a = 0; a < config.getInt(c + ".spawn.amount"); a++) {
- if (config.getBoolean(c+".spawn.locations.is")) {
- for (String Stloc : config.getStringList(c+".spawn.locations.loc")) {
- if (!w.equals(Stloc.split(",")[0])) {
- ws.append(Stloc.split(",")[0]).append(", ");
- }
- w = Stloc.split(",")[0];
- Location loc = new Location(Bukkit.getWorld(Stloc.split(",")[0]),Integer.parseInt(Stloc.split(",")[1]),Integer.parseInt(Stloc.split(",")[2]),Integer.parseInt(Stloc.split(",")[3]));
- Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> {
- loc.getBlock().setType(Material.AIR);
- cmdDelay.remove(loc);
- }, config.getInt(c + ".spawn.remove") * 20);
- loc.getBlock().setType(Material.WHITE_SHULKER_BOX);
- Block sh = loc.getBlock();
- ShulkerBox box = (ShulkerBox) sh.getState();
- box.setCustomName(color(Main.getInstance().getConfig().getString("box.title")+" - "+config.getString(c+".name")));
- Inventory inv = box.getSnapshotInventory();
- ArrayList<ItemStack> alpha = resultItem(c, 1);
- size = alpha.size();
- setItems(c,alpha, inv);
- box.update();
- }
- } else {
- w = config.getStringList(c + ".spawn.location.worlds").get((int) Math.floor(Math.random()*config.getStringList(c + ".spawn.location.worlds").size()));
- for (String worldname : config.getStringList(c + ".spawn.location.worlds")) {
- ws.append(worldname).append(", ");
- int max = config.getInt(c + ".spawn.location.max.x"), min = config.getInt(c + ".spawn.location.min.x");
- if (max < min) {
- max = config.getInt(c + ".spawn.location.min.x");
- min = config.getInt(c + ".spawn.location.max.x");
- }
- x = (int) (Math.floor(Math.random() * (max - min)) + min);
- max = config.getInt(c + ".spawn.location.max.y");
- min = config.getInt(c + ".spawn.location.min.y");
- if (max < min) {
- max = config.getInt(c + ".spawn.location.min.y");
- min = config.getInt(c + ".spawn.location.max.y");
- }
- y = (int) (Math.floor(Math.random() * (max - min)) + min);
- max = config.getInt(c + ".spawn.location.max.z");
- min = config.getInt(c + ".spawn.location.min.z");
- if (max < min) {
- max = config.getInt(c + ".spawn.location.min.z");
- min = config.getInt(c + ".spawn.location.max.z");
- }
- z = (int) (Math.floor(Math.random() * (max - min)) + min);
- if (Bukkit.getWorld(worldname) == null) {
- continue;
- }
- randomspawn(worldname,x,y,z,max,min,5);
- Bukkit.getWorld(worldname).getBlockAt(x, y, z).setType(Material.WHITE_SHULKER_BOX);
- int finalX = x;
- int finalY = y;
- int finalZ = z;
- Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> {
- Bukkit.getWorld(worldname).getBlockAt(finalX, finalY, finalZ).setType(Material.AIR);
- cmdDelay.remove(new Location(Bukkit.getWorld(worldname), finalX,finalY,finalZ));
- }, config.getInt(c + ".spawn.remove") * 20);
- Block sh = Bukkit.getWorld(worldname).getBlockAt(x, y, z);
- ShulkerBox box = (ShulkerBox) sh.getState();
- box.setCustomName(color(Main.getInstance().getConfig().getString("box.title")+" - "+config.getString(c+".name")));
- Inventory inv = box.getSnapshotInventory();
- ArrayList<ItemStack> alpha = resultItem(c, 1);
- size = alpha.size();
- setItems(c,alpha, inv);
- box.update();
- }
- }
- }
- String notice = config.getString(c + ".spawn.message").replace("%name%", config.getString(c + ".name")).replace("%id%", String.valueOf(c))
- .replace("%amount%", String.valueOf(config.getInt(c + ".spawn.amount"))).replace("%x%", String.valueOf(x))
- .replace("%y%", String.valueOf(y)).replace("%z%", String.valueOf(z))
- .replace("%area_x", config.getInt(c + ".spawn.location.max.x") + "~" + config.getInt(c + ".spawn.location.max.x"))
- .replace("%area_y", config.getInt(c + ".spawn.location.max.y") + "~" + config.getInt(c + ".spawn.location.max.y"))
- .replace("%area_z", config.getInt(c + ".spawn.location.max.z") + "~" + config.getInt(c + ".spawn.location.max.z"))
- .replace("%world%", w).replace("%worlds%", ws).replace("%size%", String.valueOf(size));
- Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', notice));
- }
- }
- c++;
- }
- Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), this::spawn, 20);
- }
- public ArrayList<ItemStack> resultItem(int id, int rea) {
- ArrayList<ItemStack> result = new ArrayList<>(), per = percentItems(id, rea), all = allItems(id);
- if (per.size() + all.size() < config.getInt(id+".amount")) {
- return resultItem(id, rea+1);
- }
- if (per.size() + all.size() > config.getInt(id+".amount")) {
- return resultItem(id, rea+1);
- }
- result.addAll(per); result.addAll(all);
- return result;
- }
- private ArrayList<ItemStack> percentItems(int id, int rea) {
- ArrayList<ItemStack> result = new ArrayList<>();
- int c = 0;
- while (config.getItemStack(id+".items."+c+".info") != null) {
- if (rea > 20) { c++; continue; }
- if (config.getDouble(id+".items."+c+".percent") >= Math.random()*100) {
- result.add(config.getItemStack(id+".items."+c+".info"));
- }
- c++;
- }
- if (rea > 20) {
- for (int i = 0; i < config.getInt(id+".amount")-allItems(id).size() ; i++) {
- int r = (int) Math.floor(Math.random() * c);
- result.add(config.getItemStack(id + ".items." + r + ".info"));
- }
- }
- while (result.size() > config.getInt(id+".amount")-allItems(id).size()) {
- int r = (int) Math.floor(Math.random() * result.size());
- result.remove(r);
- }
- return result;
- }
- private ArrayList<ItemStack> allItems(int id) {
- ArrayList<ItemStack> result = new ArrayList<>();
- int c = 0;
- while (config.getItemStack(id+".items."+c+".info") != null) {
- if (config.getDouble(id+".items."+c+".percent") == -1.0) {
- result.add(config.getItemStack(id+".items."+c+".info"));
- }
- c++;
- }
- return result;
- }
- private void randomspawn(String worldname, int x, int y, int z, int max, int min, int size) {
- while (Bukkit.getWorld(worldname).getBlockAt(x, y, z).getType() != Material.AIR) {
- y++;
- if (y < 255) {
- while (Bukkit.getWorld(worldname).getBlockAt(x, y-1, z).getType() == Material.AIR) {
- y--;
- if (y < 1) {
- y = (int) (Math.floor(Math.random() * (max - min)) + min);
- break;
- }
- }
- }
- }
- if (Bukkit.getWorld(worldname).getBlockAt(x, y - 1, z).getType() == Material.LAVA
- || Bukkit.getWorld(worldname).getBlockAt(x, y - 1, z).getType() == Material.WATER) {
- x = (int) Math.floor(Math.random()*size);
- y = (int) Math.floor(Math.random()*size);
- z = (int) Math.floor(Math.random()*size);
- randomspawn(worldname, x, y, z, max, min, size+3);
- }
- }
- private void setItems(int id, ArrayList<ItemStack> alpha, Inventory inv) {
- int stacks = 0;
- for (ItemStack itemStack : alpha) {
- if (stacks > customConfig.get().getInt(id + ".amount")) {
- break;
- }
- int random = (int) Math.floor(Math.random() * 27);
- if (inv.getItem(random) == null) {
- inv.setItem(random, itemStack);
- } else {
- int ok = 0;
- while (inv.getItem(ok) != null) {
- ok++;
- if (ok > 26) {
- ok = (int) Math.floor(Math.random()*27);
- break;
- }
- }
- inv.setItem(ok, itemStack);
- }
- stacks++;
- }
- }
- private static String color(String s) {
- return ChatColor.translateAlternateColorCodes('&',s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement