Advertisement
Guest User

Untitled

a guest
Feb 13th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.74 KB | None | 0 0
  1. package redlimerl.spigot.randombox;
  2.  
  3. import net.milkbowl.vault.economy.Economy;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.ChatColor;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.block.Block;
  9. import org.bukkit.block.ShulkerBox;
  10. import org.bukkit.configuration.file.FileConfiguration;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.event.EventHandler;
  13. import org.bukkit.event.Listener;
  14. import org.bukkit.event.player.PlayerInteractEvent;
  15. import org.bukkit.inventory.EquipmentSlot;
  16. import org.bukkit.inventory.Inventory;
  17. import org.bukkit.inventory.ItemStack;
  18. import redlimerl.spigot.randombox.files.CustomConfig;
  19.  
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23.  
  24. public class SpawnBox implements Listener {
  25.  
  26.     private CustomConfig customConfig = new CustomConfig();
  27.     private FileConfiguration config = customConfig.get();
  28.     private HashMap<String, Integer> spawnTime = new HashMap<>();
  29.     private HashMap<Location, List<Player>> cmdDelay = new HashMap<>();
  30.     private Economy econ = Main.econ;
  31.  
  32.     @SuppressWarnings("deprecation")
  33.     @EventHandler
  34.     public void CloseInv(PlayerInteractEvent e) {
  35.         if (e.getHand() == null) {
  36.             return;
  37.         }
  38.         if (e.getHand() == EquipmentSlot.HAND && e.getClickedBlock() != null) {
  39.             if (e.getClickedBlock().getType() == Material.WHITE_SHULKER_BOX) {
  40.                 if (e.getClickedBlock() == null) {
  41.                     return;
  42.                 }
  43.                 ShulkerBox box = (ShulkerBox) e.getClickedBlock().getState();
  44.                 if (box.getInventory().getTitle().contains(color(Main.getInstance().getConfig().getString("box.title")))
  45.                         && box.getInventory().getSize() == 27) {
  46.                     if (box.getInventory().getTitle().contains(" - ")) {
  47.                         int a = 0;
  48.                         while (!config.getString(a + ".name").equals(box.getInventory().getTitle().split(" - ")[1])) {
  49.                             a++;
  50.                         }
  51.                         int id = a;
  52.                         int stack = 0;
  53.                         if (customConfig.get().getItemStack(id + ".unlock.card").getType() != Material.AIR) {
  54.                             if (!e.getPlayer().getInventory().contains(customConfig.get().getItemStack(id + ".unlock.card").getType())) {
  55.                                 e.getPlayer().sendMessage(ChatColor.RED + "It is no item to open the box!");
  56.                                 e.setCancelled(true);
  57.                                 return;
  58.                             }
  59.                             for (int i = 0; i < e.getPlayer().getInventory().getSize(); i++) {
  60.                                 if (e.getPlayer().getInventory().getItem(i) == null) {
  61.                                     continue;
  62.                                 }
  63.                                 if (e.getPlayer().getInventory().getItem(i).getType().equals(customConfig.get().getItemStack(id + ".unlock.card").getType())) {
  64.                                     if (customConfig.get().getItemStack(id + ".unlock.card").hasItemMeta()) {
  65.                                         if (e.getPlayer().getInventory().getItem(i).getItemMeta().equals(customConfig.get().getItemStack(id + ".unlock.card").getItemMeta())) {
  66.                                             stack = stack + e.getPlayer().getInventory().getItem(i).getAmount();
  67.                                         }
  68.                                     } else {
  69.                                         stack = stack + e.getPlayer().getInventory().getItem(i).getAmount();
  70.                                     }
  71.                                 }
  72.                             }
  73.                             if (stack < customConfig.get().getItemStack(id + ".unlock.card").getAmount()) {
  74.                                 e.getPlayer().sendMessage(ChatColor.RED + "It is no item to open the box!");
  75.                                 e.setCancelled(true);
  76.                                 return;
  77.                             }
  78.                         }
  79.  
  80.                         if (customConfig.get().getInt(id + ".unlock.money") != 0 && Main.getInstance().getServer().getPluginManager().getPlugin("Vault") != null) {
  81.                             if (econ.getBalance(e.getPlayer()) > customConfig.get().getInt(id + ".unlock.money")) {
  82.                                 e.getPlayer().sendMessage(ChatColor.RED + "There is not enough money to open the box.");
  83.                                 e.setCancelled(true);
  84.                                 return;
  85.                             }
  86.                         }
  87.  
  88.                         if (Main.getInstance().getServer().getPluginManager().getPlugin("Vault") != null && econ != null) {
  89.                             econ.withdrawPlayer(e.getPlayer(), customConfig.get().getInt(id + ".unlock.money"));
  90.                         } else if (customConfig.get().getInt(id + ".unlock.money") != 0) {
  91.                             e.getPlayer().sendMessage(ChatColor.RED + "The \"Vault\" plugin did not exist, we could not deduct money.");
  92.                         }
  93.                         if (cmdDelay.get(e.getClickedBlock().getLocation()) != null) {
  94.                             if (!cmdDelay.get(e.getClickedBlock().getLocation()).contains(e.getPlayer())) {
  95.                                 int max = customConfig.get().getItemStack(id + ".unlock.card").getAmount();
  96.                                 for (int i = 0; i < e.getPlayer().getInventory().getSize(); i++) {
  97.                                     if (max > 0) {
  98.                                         if (e.getPlayer().getInventory().getItem(i) == null) {
  99.                                             continue;
  100.                                         }
  101.                                         if (e.getPlayer().getInventory().getItem(i).getType().equals(customConfig.get().getItemStack(id + ".unlock.card").getType())) {
  102.                                             if (customConfig.get().getItemStack(id + ".unlock.card").hasItemMeta()) {
  103.                                                 if (!e.getPlayer().getInventory().getItem(i).getItemMeta().equals(customConfig.get().getItemStack(id + ".unlock.card").getItemMeta())) {
  104.                                                     continue;
  105.                                                 }
  106.                                             }
  107.                                             int count = e.getPlayer().getInventory().getItem(i).getAmount();
  108.                                             if (count > max) {
  109.                                                 ItemStack item = e.getPlayer().getInventory().getItem(i).clone();
  110.                                                 item.setAmount(count - max);
  111.                                                 e.getPlayer().getInventory().setItem(i, item);
  112.                                                 max = 0;
  113.                                             } else {
  114.                                                 max = max - count;
  115.                                                 e.getPlayer().getInventory().clear(i);
  116.                                             }
  117.                                         }
  118.                                     }
  119.                                 }
  120.                             }
  121.                         } else {
  122.                             int max = customConfig.get().getItemStack(id + ".unlock.card").getAmount();
  123.                             for (int i = 0; i < e.getPlayer().getInventory().getSize(); i++) {
  124.                                 if (max > 0) {
  125.                                     if (e.getPlayer().getInventory().getItem(i) == null) {
  126.                                         continue;
  127.                                     }
  128.                                     if (e.getPlayer().getInventory().getItem(i).getType().equals(customConfig.get().getItemStack(id + ".unlock.card").getType())) {
  129.                                         if (customConfig.get().getItemStack(id + ".unlock.card").hasItemMeta()) {
  130.                                             if (!e.getPlayer().getInventory().getItem(i).getItemMeta().equals(customConfig.get().getItemStack(id + ".unlock.card").getItemMeta())) {
  131.                                                 continue;
  132.                                             }
  133.                                         }
  134.                                         int count = e.getPlayer().getInventory().getItem(i).getAmount();
  135.                                         if (count > max) {
  136.                                             ItemStack item = e.getPlayer().getInventory().getItem(i).clone();
  137.                                             item.setAmount(count - max);
  138.                                             e.getPlayer().getInventory().setItem(i, item);
  139.                                             max = 0;
  140.                                         } else {
  141.                                             max = max - count;
  142.                                             e.getPlayer().getInventory().clear(i);
  143.                                         }
  144.                                     }
  145.                                 }
  146.                             }
  147.                         }
  148.                         if (cmdDelay.get(e.getClickedBlock().getLocation()) != null) {
  149.                             if (!cmdDelay.get(e.getClickedBlock().getLocation()).contains(e.getPlayer())) {
  150.                                 List<String> list = customConfig.get().getStringList(id + ".commands");
  151.                                 for (String cmd : list) {
  152.                                     cmd = cmd.replace("%playername%", e.getPlayer().getName()).replace("%name%", customConfig.get().getString(id + ".name"))
  153.                                             .replace("%id%", String.valueOf(id)).replace("%x%", String.valueOf(e.getClickedBlock().getLocation().getBlockX())).
  154.                                                     replace("%y%", String.valueOf(e.getClickedBlock().getLocation().getBlockY())).
  155.                                                     replace("%z%", String.valueOf(e.getClickedBlock().getLocation().getBlockZ()));
  156.                                     IdManager.cmdOp(e.getPlayer(), cmd);
  157.                                 }
  158.                             }
  159.                         } else {
  160.                             List<String> list = customConfig.get().getStringList(id + ".commands");
  161.                             for (String cmd : list) {
  162.                                 cmd = cmd.replace("%playername%", e.getPlayer().getName()).replace("%name%", customConfig.get().getString(id + ".name"))
  163.                                         .replace("%id%", String.valueOf(id)).replace("%x%", String.valueOf(e.getClickedBlock().getLocation().getBlockX())).
  164.                                                 replace("%y%", String.valueOf(e.getClickedBlock().getLocation().getBlockY())).
  165.                                                 replace("%z%", String.valueOf(e.getClickedBlock().getLocation().getBlockZ()));
  166.                                 IdManager.cmdOp(e.getPlayer(), cmd);
  167.                             }
  168.                         }
  169.                         List<Player> li;
  170.                         if (cmdDelay.get(e.getClickedBlock().getLocation()) == null) {
  171.                             li = new ArrayList<>();
  172.                         } else { li = cmdDelay.get(e.getClickedBlock().getLocation()); }
  173.                         li.add(e.getPlayer());
  174.                         cmdDelay.put(e.getClickedBlock().getLocation(), li);
  175.                     }
  176.                 }
  177.                 return;
  178.             }
  179.         }
  180.         if (e.getHand() == EquipmentSlot.HAND && e.getItem() != null) {
  181.             int c = 0, p = -1, a = 0;
  182.             while (customConfig.get().get(String.valueOf(c)) != null) {
  183.                 if (e.getItem().getType().equals(customConfig.get().getItemStack(c+".unlock.key").getType())) {
  184.                     if (customConfig.get().getItemStack(c+".unlock.key").hasItemMeta()) {
  185.                         if (!e.getItem().getItemMeta().equals(customConfig.get().getItemStack(c+".unlock.key").getItemMeta())) {
  186.                             continue;
  187.                         }
  188.                     }
  189.                     p = c;
  190.                     a++;
  191.                 }
  192.                 c++;
  193.             }
  194.             if (p == -1) { return; }
  195.             if (e.getItem().getAmount() < customConfig.get().getItemStack(p + ".unlock.key").getAmount()) {
  196.                 return;
  197.             }
  198.             if (a > 1) {
  199.                 e.getPlayer().sendMessage(ChatColor.RED+"There are two or more boxes of the same key.");
  200.                 return;
  201.             }
  202.             if (customConfig.get().getInt(p+".unlock.money") != 0 && Main.getInstance().getServer().getPluginManager().getPlugin("Vault") != null) {
  203.                 if (econ.getBalance(e.getPlayer()) > customConfig.get().getInt(p+".unlock.money")) {
  204.                     e.getPlayer().sendMessage(ChatColor.RED+"There is not enough money to open the box.");
  205.                     return;
  206.                 }
  207.             }
  208.             IdManager.cmdOp(e.getPlayer(), "rb open "+p);
  209.             e.getItem().setAmount(e.getItem().getAmount()-customConfig.get().getItemStack(p + ".unlock.key").getAmount());
  210.             if (Main.getInstance().getServer().getPluginManager().getPlugin("Vault") != null && econ != null) {
  211.                 econ.withdrawPlayer(e.getPlayer(), customConfig.get().getInt(p + ".unlock.money"));
  212.             } else if (customConfig.get().getInt(p + ".unlock.money") != 0) {
  213.                 e.getPlayer().sendMessage(ChatColor.RED+"The \"Vault\" plugin did not exist, we could not deduct money.");
  214.             }
  215.         }
  216.     }
  217.  
  218.     void spawn() {
  219.         int c = 0;
  220.         while (customConfig.get().get(String.valueOf(c)) != null) {
  221.             if (spawnTime.get(config.getString(c+".name")) == null || spawnTime.get(config.getString(c+".name")) <= 0) {
  222.                 if (config.getInt(c + ".spawn.delay") != 0 && config.getInt(c + ".spawn.remove") != 0 ) {
  223.                     spawnTime.put(config.getString(c + ".name"), config.getInt(c + ".spawn.delay"));
  224.                 }
  225.             }
  226.             if (spawnTime.get(config.getString(c + ".name")) != null) {
  227.                 int x = 0, y = 0, z = 0, size = 0; String w = "";
  228.                 StringBuilder ws = new StringBuilder();
  229.                 spawnTime.put(config.getString(c + ".name"), spawnTime.get(config.getString(c + ".name")) - 1);
  230.                 if (spawnTime.get(config.getString(c + ".name")) == 0) {
  231.                     spawnTime.put(config.getString(c + ".name"), config.getInt(c + ".spawn.delay"));
  232.                     for (int a = 0; a < config.getInt(c + ".spawn.amount"); a++) {
  233.                         if (config.getBoolean(c+".spawn.locations.is")) {
  234.                             for (String Stloc : config.getStringList(c+".spawn.locations.loc")) {
  235.                                 if (!w.equals(Stloc.split(",")[0])) {
  236.                                     ws.append(Stloc.split(",")[0]).append(", ");
  237.                                 }
  238.                                 w = Stloc.split(",")[0];
  239.                                 Location loc = new Location(Bukkit.getWorld(Stloc.split(",")[0]),Integer.parseInt(Stloc.split(",")[1]),Integer.parseInt(Stloc.split(",")[2]),Integer.parseInt(Stloc.split(",")[3]));
  240.                                 Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> {
  241.                                     loc.getBlock().setType(Material.AIR);
  242.                                     cmdDelay.remove(loc);
  243.                                 }, config.getInt(c + ".spawn.remove") * 20);
  244.                                 loc.getBlock().setType(Material.WHITE_SHULKER_BOX);
  245.                                 Block sh = loc.getBlock();
  246.                                 ShulkerBox box = (ShulkerBox) sh.getState();
  247.                                 box.setCustomName(color(Main.getInstance().getConfig().getString("box.title")+" - "+config.getString(c+".name")));
  248.                                 Inventory inv = box.getSnapshotInventory();
  249.                                 ArrayList<ItemStack> alpha = resultItem(c, 1);
  250.                                 size = alpha.size();
  251.                                 setItems(c,alpha, inv);
  252.                                 box.update();
  253.                             }
  254.                         } else {
  255.                             w = config.getStringList(c + ".spawn.location.worlds").get((int) Math.floor(Math.random()*config.getStringList(c + ".spawn.location.worlds").size()));
  256.                             for (String worldname : config.getStringList(c + ".spawn.location.worlds")) {
  257.                                 ws.append(worldname).append(", ");
  258.                                 int max = config.getInt(c + ".spawn.location.max.x"), min = config.getInt(c + ".spawn.location.min.x");
  259.                                 if (max < min) {
  260.                                     max = config.getInt(c + ".spawn.location.min.x");
  261.                                     min = config.getInt(c + ".spawn.location.max.x");
  262.                                 }
  263.                                 x = (int) (Math.floor(Math.random() * (max - min)) + min);
  264.                                 max = config.getInt(c + ".spawn.location.max.y");
  265.                                 min = config.getInt(c + ".spawn.location.min.y");
  266.                                 if (max < min) {
  267.                                     max = config.getInt(c + ".spawn.location.min.y");
  268.                                     min = config.getInt(c + ".spawn.location.max.y");
  269.                                 }
  270.                                 y = (int) (Math.floor(Math.random() * (max - min)) + min);
  271.                                 max = config.getInt(c + ".spawn.location.max.z");
  272.                                 min = config.getInt(c + ".spawn.location.min.z");
  273.                                 if (max < min) {
  274.                                     max = config.getInt(c + ".spawn.location.min.z");
  275.                                     min = config.getInt(c + ".spawn.location.max.z");
  276.                                 }
  277.                                 z = (int) (Math.floor(Math.random() * (max - min)) + min);
  278.                                 if (Bukkit.getWorld(worldname) == null) {
  279.                                     continue;
  280.                                 }
  281.                                 randomspawn(worldname,x,y,z,max,min,5);
  282.  
  283.                                 Bukkit.getWorld(worldname).getBlockAt(x, y, z).setType(Material.WHITE_SHULKER_BOX);
  284.                                 int finalX = x;
  285.                                 int finalY = y;
  286.                                 int finalZ = z;
  287.                                 Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> {
  288.                                     Bukkit.getWorld(worldname).getBlockAt(finalX, finalY, finalZ).setType(Material.AIR);
  289.                                     cmdDelay.remove(new Location(Bukkit.getWorld(worldname), finalX,finalY,finalZ));
  290.                                 }, config.getInt(c + ".spawn.remove") * 20);
  291.                                 Block sh = Bukkit.getWorld(worldname).getBlockAt(x, y, z);
  292.                                 ShulkerBox box = (ShulkerBox) sh.getState();
  293.                                 box.setCustomName(color(Main.getInstance().getConfig().getString("box.title")+" - "+config.getString(c+".name")));
  294.                                 Inventory inv = box.getSnapshotInventory();
  295.                                 ArrayList<ItemStack> alpha = resultItem(c, 1);
  296.                                 size = alpha.size();
  297.                                 setItems(c,alpha, inv);
  298.                                 box.update();
  299.                             }
  300.                         }
  301.                     }
  302.                     String notice = config.getString(c + ".spawn.message").replace("%name%", config.getString(c + ".name")).replace("%id%", String.valueOf(c))
  303.                             .replace("%amount%", String.valueOf(config.getInt(c + ".spawn.amount"))).replace("%x%", String.valueOf(x))
  304.                             .replace("%y%", String.valueOf(y)).replace("%z%", String.valueOf(z))
  305.                             .replace("%area_x", config.getInt(c + ".spawn.location.max.x") + "~" + config.getInt(c + ".spawn.location.max.x"))
  306.                             .replace("%area_y", config.getInt(c + ".spawn.location.max.y") + "~" + config.getInt(c + ".spawn.location.max.y"))
  307.                             .replace("%area_z", config.getInt(c + ".spawn.location.max.z") + "~" + config.getInt(c + ".spawn.location.max.z"))
  308.                             .replace("%world%", w).replace("%worlds%", ws).replace("%size%", String.valueOf(size));
  309.                     Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', notice));
  310.                 }
  311.             }
  312.             c++;
  313.         }
  314.         Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), this::spawn, 20);
  315.     }
  316.  
  317.     public ArrayList<ItemStack> resultItem(int id, int rea) {
  318.         ArrayList<ItemStack> result = new ArrayList<>(), per = percentItems(id, rea), all = allItems(id);
  319.         if (per.size() + all.size() < config.getInt(id+".amount")) {
  320.             return resultItem(id, rea+1);
  321.         }
  322.         if (per.size() + all.size() > config.getInt(id+".amount")) {
  323.             return resultItem(id, rea+1);
  324.         }
  325.         result.addAll(per); result.addAll(all);
  326.         return result;
  327.     }
  328.  
  329.     private ArrayList<ItemStack> percentItems(int id, int rea) {
  330.         ArrayList<ItemStack> result = new ArrayList<>();
  331.         int c = 0;
  332.         while (config.getItemStack(id+".items."+c+".info") != null) {
  333.             if (rea > 20) { c++; continue; }
  334.             if (config.getDouble(id+".items."+c+".percent") >= Math.random()*100) {
  335.                 result.add(config.getItemStack(id+".items."+c+".info"));
  336.             }
  337.             c++;
  338.         }
  339.         if (rea > 20) {
  340.             for (int i = 0; i < config.getInt(id+".amount")-allItems(id).size() ; i++) {
  341.                 int r = (int) Math.floor(Math.random() * c);
  342.                 result.add(config.getItemStack(id + ".items." + r + ".info"));
  343.             }
  344.         }
  345.         while (result.size() > config.getInt(id+".amount")-allItems(id).size()) {
  346.             int r = (int) Math.floor(Math.random() * result.size());
  347.             result.remove(r);
  348.         }
  349.         return result;
  350.     }
  351.  
  352.     private ArrayList<ItemStack> allItems(int id) {
  353.         ArrayList<ItemStack> result = new ArrayList<>();
  354.         int c = 0;
  355.         while (config.getItemStack(id+".items."+c+".info") != null) {
  356.             if (config.getDouble(id+".items."+c+".percent") == -1.0) {
  357.                 result.add(config.getItemStack(id+".items."+c+".info"));
  358.             }
  359.             c++;
  360.         }
  361.         return result;
  362.     }
  363.  
  364.     private void randomspawn(String worldname, int x, int y, int z, int max, int min, int size) {
  365.         while (Bukkit.getWorld(worldname).getBlockAt(x, y, z).getType() != Material.AIR) {
  366.             y++;
  367.             if (y < 255) {
  368.                 while (Bukkit.getWorld(worldname).getBlockAt(x, y-1, z).getType() == Material.AIR) {
  369.                     y--;
  370.                     if (y < 1) {
  371.                         y = (int) (Math.floor(Math.random() * (max - min)) + min);
  372.                         break;
  373.                     }
  374.                 }
  375.             }
  376.         }
  377.         if (Bukkit.getWorld(worldname).getBlockAt(x, y - 1, z).getType() == Material.LAVA
  378.                 || Bukkit.getWorld(worldname).getBlockAt(x, y - 1, z).getType() == Material.WATER) {
  379.             x = (int) Math.floor(Math.random()*size);
  380.             y = (int) Math.floor(Math.random()*size);
  381.             z = (int) Math.floor(Math.random()*size);
  382.             randomspawn(worldname, x, y, z, max, min, size+3);
  383.         }
  384.     }
  385.  
  386.     private void setItems(int id, ArrayList<ItemStack> alpha, Inventory inv) {
  387.         int stacks = 0;
  388.         for (ItemStack itemStack : alpha) {
  389.             if (stacks > customConfig.get().getInt(id + ".amount")) {
  390.                 break;
  391.             }
  392.             int random = (int) Math.floor(Math.random() * 27);
  393.             if (inv.getItem(random) == null) {
  394.                 inv.setItem(random, itemStack);
  395.             } else {
  396.                 int ok = 0;
  397.                 while (inv.getItem(ok) != null) {
  398.                     ok++;
  399.                     if (ok > 26) {
  400.                         ok = (int) Math.floor(Math.random()*27);
  401.                         break;
  402.                     }
  403.                 }
  404.                 inv.setItem(ok, itemStack);
  405.             }
  406.             stacks++;
  407.         }
  408.     }
  409.  
  410.     private static String color(String s) {
  411.         return ChatColor.translateAlternateColorCodes('&',s);
  412.     }
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement