Advertisement
Lisenochek

Untitled

Oct 22nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. package ru.lisenochek.mcrust.objects.blockMechanic;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.Location;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.inventory.Inventory;
  7. import org.bukkit.inventory.ItemStack;
  8. import ru.lisenochek.mcrust.sql.SQLManager;
  9. import ru.lisenochek.mcrust.utils.ISBuilder;
  10. import ru.lisenochek.mcrust.utils.Utils;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15.  
  16. public class Crate {
  17.  
  18. private static HashMap<Location, Crate> cratesMap = new HashMap<>();
  19.  
  20. private Location location;
  21. private Type type;
  22. private Inventory inventory;
  23.  
  24. private Crate(Location location, Type type) {
  25. this.location = location;
  26. this.type = type;
  27. this.inventory = Bukkit.createInventory(null, type.getSize(), Utils.stripColor(type.getDisplayName()));
  28. cratesMap.put(location, this);
  29. }
  30.  
  31. private Crate(Location location, Type type, String data) {
  32. this.location = location;
  33. this.type = type;
  34. this.inventory = Bukkit.createInventory(null, type.getSize(), Utils.stripColor(type.getDisplayName()));
  35. for (String s : data.split(" ")) {
  36. String[] s1 = s.split("-");
  37. inventory.setItem(Integer.parseInt(s1[0]), ISBuilder.deserialiseStack(s1[1]));
  38. }
  39. cratesMap.put(location, this);
  40. }
  41.  
  42. public static Crate create(Location location, Type type) {
  43. return new Crate(location, type);
  44. }
  45.  
  46. public static Crate create(Location location, Type type, String data) {
  47. return new Crate(location, type, data);
  48. }
  49.  
  50. public static void save() {
  51. for (Crate crate : cratesMap.values()) SQLManager.getManager().updateCrateData(crate);
  52. }
  53.  
  54. public static Crate fromLocation(Location location) {
  55. return cratesMap.get(location);
  56. }
  57.  
  58. public Location getLocation() {
  59. return location;
  60. }
  61.  
  62. public Type getType() {
  63. return type;
  64. }
  65.  
  66. public List<ItemStack> getContents() {
  67. List<ItemStack> stacks = new ArrayList<>();
  68. for (int slot = 0; slot < type.getSize(); slot++) {
  69. ItemStack stack = inventory.getItem(slot);
  70. if (stack != null) stacks.add(stack);
  71. }
  72. return stacks;
  73. }
  74.  
  75. public void remove() {
  76. for (ItemStack stack : getContents()) location.getWorld().dropItemNaturally(location.clone().add(0.5D, 0, 0.5D), stack);
  77. cratesMap.remove(location);
  78. SQLManager.getManager().deleteCrateData(this);
  79. }
  80.  
  81. public void openGUI(Player player) {
  82. player.openInventory(inventory);
  83. }
  84.  
  85. public String serialize() {
  86.  
  87. if (getContents().size() == 0) return null;
  88.  
  89. StringBuilder builder = new StringBuilder();
  90.  
  91. for (int slot = 0; slot < type.getSize(); slot++) {
  92. ItemStack stack = inventory.getItem(slot);
  93. if (stack == null) continue;
  94. builder.append(slot).append("-").append(ISBuilder.serialise(stack)).append(" ");
  95. }
  96.  
  97. return builder.toString();
  98. }
  99.  
  100. public enum Type {
  101.  
  102. SMALL("&2&lМаленький ящик", 18),
  103. LARGE("&2&lБольшой ящик", 54);
  104.  
  105. private String displayName;
  106. private int slots;
  107.  
  108. Type(String displayName, int slots) {
  109. this.displayName = displayName;
  110. this.slots = slots;
  111. }
  112.  
  113. public String getDisplayName() {
  114. return displayName;
  115. }
  116.  
  117. public int getSize() {
  118. return slots;
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement