Advertisement
Lisenochek

Untitled

Dec 2nd, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. package ru.lisenochek.mcrust.objects.player;
  2.  
  3. import org.bukkit.GameMode;
  4. import org.bukkit.Location;
  5. import org.bukkit.Material;
  6. import org.bukkit.entity.ArmorStand;
  7. import org.bukkit.entity.Entity;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.inventory.ItemStack;
  10. import org.bukkit.metadata.FixedMetadataValue;
  11. import ru.lisenochek.mcrust.Main;
  12. import ru.lisenochek.mcrust.objects.misc.custom.CustomWorld;
  13. import ru.lisenochek.mcrust.objects.misc.entity.CustomEntity;
  14. import ru.lisenochek.mcrust.sql.SQLManager;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.Arrays;
  18. import java.util.HashMap;
  19. import java.util.List;
  20.  
  21. public class Backpack {
  22.  
  23. private static HashMap<String, Backpack> backpacksMap = new HashMap<>();
  24.  
  25. private String ownerName;
  26. private Location location;
  27. private Entity entity;
  28. private List<ItemStack> dropList;
  29.  
  30. private Backpack(String ownerName, Location location) {
  31. this.ownerName = ownerName;
  32. this.location = location.getBlock().getType() == Material.STATIONARY_WATER ? location.getWorld().getHighestBlockAt(location).getLocation() : location.getBlock().getType() != Material.IRON_DOOR_BLOCK && location.getBlock().getType().isSolid() ? location.clone().add(0, 1, 0) : location;
  33. }
  34.  
  35. public static Backpack create(Player player) {
  36. if (player.getGameMode() != GameMode.SURVIVAL) return null;
  37. if (player.getWorld() != CustomWorld.getCustomWorld().getWorld()) return null;
  38. return new Backpack(player.getName(), player.getLocation().getBlock().getLocation()).setDropList(Arrays.asList(player.getInventory().getContents())).spawn();
  39. }
  40.  
  41. public static Backpack create(String ownerName, Location location, List<ItemStack> dropList) {
  42. return new Backpack(ownerName, location).setDropList(dropList).spawn();
  43. }
  44.  
  45. public static Backpack fromOwnerName(String ownerName) {
  46. return backpacksMap.get(ownerName);
  47. }
  48.  
  49. public static int getBackpacksAmount() {
  50. return backpacksMap.size();
  51. }
  52.  
  53. public static void save() {
  54. for (Backpack backpack : backpacksMap.values()) {
  55. SQLManager.getManager().addBackpackData(backpack);
  56. backpack.getEntity().remove();
  57. }
  58. }
  59.  
  60. public String getOwnerName() {
  61. return ownerName;
  62. }
  63.  
  64. public Location getLocation() {
  65. return location;
  66. }
  67.  
  68. public Entity getEntity() {
  69. return entity;
  70. }
  71.  
  72. public List<ItemStack> getDropList() {
  73. return dropList;
  74. }
  75.  
  76. public Backpack setDropList(List<ItemStack> list) {
  77. List<ItemStack> dropList = new ArrayList<>();
  78. for (ItemStack stack : list) if (stack != null) dropList.add(stack);
  79. this.dropList = dropList;
  80. return this;
  81. }
  82.  
  83. public Backpack spawn() {
  84.  
  85. if (dropList.size() == 0) return null;
  86.  
  87. ArmorStand backpack = CustomEntity.spawn(location.clone().add(0.5D, -1.4D, 0.5D), ArmorStand.class);
  88.  
  89. backpack.setHelmet(new ItemStack(Material.CARPET, 1, (short) 4));
  90. backpack.setGravity(false);
  91. backpack.setVisible(false);
  92. backpack.setMetadata("backpackOwner", new FixedMetadataValue(Main.plugin, ownerName));
  93. entity = backpack;
  94. backpacksMap.put(ownerName, this);
  95. return this;
  96. }
  97.  
  98. public void remove() {
  99. backpacksMap.remove(ownerName);
  100. entity.remove();
  101. SQLManager.getManager().deleteBackpackData(this);
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement