Lisenochek

Untitled

Oct 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.62 KB | None | 0 0
  1. package ru.lisenochek.mcrust.objects.blockMechanic;
  2.  
  3. import com.comphenix.protocol.wrappers.EnumWrappers;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.ChatColor;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.block.BlockBreakEvent;
  10. import org.bukkit.inventory.Inventory;
  11. import org.bukkit.scheduler.BukkitRunnable;
  12. import ru.lisenochek.mcrust.Main;
  13. import ru.lisenochek.mcrust.events.block.RustBlockBreakEvent;
  14. import ru.lisenochek.mcrust.events.block.RustBlockDecayEvent;
  15. import ru.lisenochek.mcrust.events.block.RustPhysicBreakBlockEvent;
  16. import ru.lisenochek.mcrust.objects.misc.CustomBlock;
  17. import ru.lisenochek.mcrust.sql.SQLManager;
  18. import ru.lisenochek.mcrust.utils.ISBuilder;
  19. import ru.lisenochek.mcrust.utils.Utils;
  20.  
  21. import java.util.HashMap;
  22.  
  23. public class Buildings {
  24.  
  25. private static HashMap<Location, Buildings> buildingsMap = new HashMap<>();
  26.  
  27. private Location location;
  28. private CustomBlock.Type type;
  29. private int health;
  30. private int decayTime;
  31. private int blockDecayTime;
  32.  
  33. private Buildings(Location location, CustomBlock.Type type, int health, int blockDecayTime) {
  34. this.location = location;
  35. this.type = type;
  36. this.health = health;
  37. this.decayTime = type.getDecayTime();
  38. this.blockDecayTime = blockDecayTime;
  39. buildingsMap.put(location, this);
  40. if (type.isDoubleBlock()) buildingsMap.put(location.clone().add(0, 1, 0), this);
  41. runTimer();
  42. }
  43.  
  44. public static Buildings create(Location location, CustomBlock.Type type, int health, int blockDecayTime) {
  45. return new Buildings(location, type, health, blockDecayTime);
  46. }
  47.  
  48. public static void save() {
  49. for (Buildings buildings : buildingsMap.values()) SQLManager.getManager().updateBuildingData(buildings);
  50. }
  51.  
  52. public static Buildings fromLocation(Location location) {
  53. return buildingsMap.get(location);
  54. }
  55.  
  56. public static void openGUI(Player player, Buildings buildings) {
  57.  
  58. Inventory inventory = Bukkit.createInventory(null, 27, Utils.stripColor("&2&lКонструкция"));
  59. int resourceAmount = Utils.getStacksAmount(player, buildings.getType().getResourceStack()), repairCost = buildings.getRepairCost(player);
  60.  
  61. ISBuilder repairBuilder = ISBuilder.getBuilder(Material.WOOD_SPADE, 0, 1, "&a&l» &e&lОтремонтировать",
  62. "",
  63. "&7&l&nДля ремонта нужны ресурсы:",
  64. "",
  65. "&7- " + ChatColor.stripColor(buildings.getType().getResourceStack().getItemMeta().getDisplayName().toLowerCase()) + " (" + (resourceAmount >= repairCost ? "&a" : "&c") + repairCost + "&7) - " + (resourceAmount < repairCost ? "не хватает - &c" + (repairCost - resourceAmount) : "у вас - &a" + resourceAmount)
  66. ).hideFlags();
  67.  
  68. ISBuilder removeBuilder = ISBuilder.getBuilder(Material.BARRIER, 0, 1, "&a&l» &c&lСломать");
  69.  
  70. inventory.setItem(13, buildings.getDecayTime() == -1
  71. ?
  72. ISBuilder.getBuilder(Material.BOOK, 0, 1, "&a&l» &e&lИнформация",
  73. "",
  74. "&7&l&nПрочность&7&l » &a&l" + buildings.getHealth() + "&7&l/&a&l" + buildings.getType().getMaxHealth(),
  75. "",
  76. "&7- Если прочность будет равна &a0&7, то конструкция",
  77. "&aсломается"
  78. ).getStack()
  79. :
  80. ISBuilder.getBuilder(Material.BOOK, 0, 1, "&a&l» &e&lИнформация",
  81. "",
  82. "&7&l&nПрочность&7&l » &a&l" + buildings.getHealth() + "&7&l/&a&l" + buildings.getType().getMaxHealth(),
  83. "",
  84. "&7- Если прочность будет равна &a0&7, то конструкция",
  85. "&aсломается",
  86. "",
  87. "&7&l&nЗадержка перед гниением&7&l » &a&l" + Utils.getTimeFormat(buildings.getBlockDecayTime()),
  88. "",
  89. "&7- Задержка, в течение которого конструкция &aне &7гниет.",
  90. "&7Сбрасывается, если конструкция в &aзоне &7шкафа",
  91. "",
  92. "&7&l&nВремя гниения&7&l » &a&l" + Utils.getTimeFormat(buildings.getDecayTime()),
  93. "",
  94. "&7- Время, по &aистечении &7которого конструкция потеряет",
  95. "&a1 &7единицу прочности"
  96. ).getStack()
  97. );
  98.  
  99. inventory.setItem(11, (buildings.getHealth() == buildings.getType().getMaxHealth()
  100. ?
  101. ISBuilder.getBuilder(Material.WOOD_SPADE, 0, 1, "&a&l» &c&lКонструкция целая").hideFlags()
  102. :
  103. resourceAmount < repairCost
  104. ?
  105. repairBuilder.addLore(
  106. "",
  107. "&cНедостаточно &7ресурсов для починки"
  108. )
  109. :
  110. repairBuilder.addLore(
  111. "",
  112. "&aНажмите&7, чтобы починить"
  113. ).setTag("location", Utils.serialiseLocation(buildings.getLocation())).setTag("repairCost", repairCost)).getStack()
  114. );
  115.  
  116. inventory.setItem(15, (buildings.getType().getDrop() == null
  117. ?
  118. removeBuilder.addLore(
  119. "",
  120. "&7Вы уничтожите конструкцию и &cне &7получите &7ее назад"
  121. )
  122. :
  123. removeBuilder.addLore(
  124. "",
  125. "&7Вы уничтожите конструкцию и &aполучите &7ее назад"
  126. )).setTag("location", Utils.serialiseLocation(buildings.getLocation())).getStack()
  127. );
  128.  
  129. for (int freeSlot = 0; freeSlot < inventory.getSize(); freeSlot++) if (inventory.getItem(freeSlot) == null) inventory.setItem(freeSlot, ISBuilder.getBuilder(Material.STAINED_GLASS_PANE, 0, 1, " ").getStack());
  130. player.openInventory(inventory);
  131. }
  132.  
  133. public Location getLocation() {
  134. return location;
  135. }
  136.  
  137. public CustomBlock.Type getType() {
  138. return type;
  139. }
  140.  
  141. public int getHealth() {
  142. return health;
  143. }
  144.  
  145. public int getDecayTime() {
  146. return decayTime;
  147. }
  148.  
  149. public int getBlockDecayTime() {
  150. return blockDecayTime;
  151. }
  152.  
  153. public void setType(CustomBlock.Type type) {
  154. this.type = type;
  155. }
  156.  
  157. public void setHealth(int health) {
  158. this.health = health;
  159. }
  160.  
  161. public void setDecayTime(int decayTime) {
  162. this.decayTime = decayTime;
  163. }
  164.  
  165. public void setBlockDecayTime(int blockDecayTime) {
  166. this.blockDecayTime = blockDecayTime;
  167. }
  168.  
  169. public void damage(int damage) {
  170.  
  171. health = health - damage;
  172.  
  173. if (health > 0) {
  174. SQLManager.getManager().updateBuildingData(this);
  175. return;
  176. }
  177.  
  178. if (type.isDoubleBlock()) {
  179. location.clone().add(0, 1, 0).getBlock().setType(Material.AIR, false);
  180. buildingsMap.remove(location.clone().add(0, 1, 0));
  181. }
  182.  
  183. breakUpBlocks(location.clone().add(0, 1, 0).getBlock().getLocation());
  184. Bukkit.getPluginManager().callEvent(new RustBlockDecayEvent(location.getBlock(), CustomBlock.getBlock(location.getBlock())));
  185.  
  186. breakBlock(location);
  187. }
  188.  
  189. public void remove(Player player) {
  190.  
  191. if (type.isDoubleBlock()) {
  192. location.clone().add(0, 1, 0).getBlock().setType(Material.AIR, false);
  193. buildingsMap.remove(location.clone().add(0, 1, 0));
  194. }
  195.  
  196. breakUpBlocks(location.clone().add(0, 1, 0).getBlock().getLocation());
  197. Bukkit.getPluginManager().callEvent(new RustBlockBreakEvent(player, CustomBlock.getBlock(location.getBlock()), new BlockBreakEvent(location.getBlock(), player)));
  198.  
  199. breakBlock(location);
  200. }
  201.  
  202. private void breakUpBlocks(Location location) {
  203.  
  204. CustomBlock upperBlock = CustomBlock.getBlock(location.getBlock());
  205.  
  206. if (upperBlock == null) return;
  207. if (!upperBlock.getType().isGround()) return;
  208.  
  209. Buildings buildings = buildingsMap.get(location);
  210.  
  211. breakUpBlocks(location.clone().add(0, 1, 0));
  212. location.getBlock().setType(Material.AIR);
  213. buildingsMap.remove(location);
  214. SQLManager.getManager().deleteBuildingData(buildings);
  215. Utils.playEffect(location.clone().add(0.5D, 0, 0.5D), false, false, 0.5f, 0.5f, 0.5f, 0, EnumWrappers.Particle.CLOUD, Material.AIR, 25);
  216. Bukkit.getPluginManager().callEvent(new RustPhysicBreakBlockEvent(location.getBlock(), upperBlock));
  217. }
  218.  
  219. private void breakBlock(Location location) {
  220. location.getBlock().setType(Material.AIR, false);
  221. buildingsMap.remove(location);
  222. SQLManager.getManager().deleteBuildingData(this);
  223. Utils.playEffect(location.clone().add(0.5D, 0, 0.5D), false, false, 0.5f, 0.5f, 0.5f, 0, EnumWrappers.Particle.CLOUD, Material.AIR, 25);
  224. }
  225.  
  226. public int getRepairCost(Player player) {
  227. int coef = ((type.getMaxHealth() - health) / 10);
  228. return coef == 0 ? 1 : coef;
  229. }
  230.  
  231. public void runTimer() {
  232.  
  233. new BukkitRunnable() {
  234.  
  235. @Override
  236. public void run() {
  237.  
  238. if (!buildingsMap.containsKey(location)) {
  239. cancel();
  240. return;
  241. }
  242.  
  243. if (decayTime == -1) return;
  244.  
  245. if (Cupboard.fromLocation(location) != null) {
  246. blockDecayTime = type.getBlockDecayTime();
  247. return;
  248. }
  249.  
  250. if (blockDecayTime != 0) {
  251. --blockDecayTime;
  252. return;
  253. }
  254.  
  255. if (decayTime != 0) {
  256. --decayTime;
  257. return;
  258. }
  259.  
  260. damage(1);
  261. decayTime = type.getDecayTime();
  262. }
  263. }.runTaskTimer(Main.plugin, 1, 20);
  264. }
  265. }
Add Comment
Please, Sign In to add comment