Advertisement
Lisenochek

Untitled

Dec 26th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.63 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.Config;
  13. import ru.lisenochek.mcrust.Main;
  14. import ru.lisenochek.mcrust.events.block.RustBlockBreakEvent;
  15. import ru.lisenochek.mcrust.events.block.RustBlockDecayEvent;
  16. import ru.lisenochek.mcrust.events.block.RustPhysicBreakBlockEvent;
  17. import ru.lisenochek.mcrust.objects.misc.sound.Sounds;
  18. import ru.lisenochek.mcrust.sql.SQLManager;
  19. import ru.lisenochek.mcrust.utils.ISBuilder;
  20. import ru.lisenochek.mcrust.utils.Utils;
  21.  
  22. import java.util.HashMap;
  23.  
  24. public class Buildings {
  25.  
  26. private static HashMap<Location, Buildings> buildingsMap = new HashMap<>();
  27.  
  28. private Location location;
  29. private CustomBlock.Type type;
  30. private int health;
  31. private int decayTime;
  32. private int blockDecayTime;
  33. private boolean isDeleting;
  34.  
  35. private Buildings(Location location, CustomBlock.Type type, int health, int blockDecayTime, boolean isDeleting) {
  36. this.location = location;
  37. this.type = type;
  38. this.health = health;
  39. this.decayTime = Config.BUILDINGS_DECAY_TIME.getInteger();
  40. this.blockDecayTime = blockDecayTime;
  41. this.isDeleting = isDeleting;
  42. buildingsMap.put(location, this);
  43. if (type.isDoubleBlock()) buildingsMap.put(location.clone().add(0, 1, 0), this);
  44. runDecay();
  45. runTimeDelete();
  46. }
  47.  
  48. public static Buildings create(Location location, CustomBlock.Type type, int health, int blockDecayTime, boolean isDeleting) {
  49. return new Buildings(location, type, health, blockDecayTime, isDeleting);
  50. }
  51.  
  52. public static Buildings fromLocation(Location location) {
  53. return buildingsMap.get(location);
  54. }
  55.  
  56. public static int getBuildingsAmount() {
  57. return buildingsMap.size();
  58. }
  59.  
  60. public static void save() {
  61. for (Buildings buildings : buildingsMap.values()) SQLManager.getManager().updateBuildingData(buildings);
  62. }
  63.  
  64. public void openGUI(Player player) {
  65.  
  66. Inventory inventory = Bukkit.createInventory(null, 27, Utils.stripColor("&2&lКонструкция"));
  67. int resourceAmount = Utils.getStacksAmount(player, type.getResourceStack());
  68. int repairCost = getRepairCost(player);
  69.  
  70. inventory.setItem(13, type.isDecaying()
  71. ?
  72. ISBuilder.getBuilder(Material.RAW_CHICKEN, 0, 1, "&a&l» &e&lИнформация",
  73. "",
  74. "&7&l&nПрочность&7&l » &a&l" + health + "&7&l/&a&l" + type.getMaxHealth(),
  75. "",
  76. "&7- Если прочность будет равна &a0&7, то конструкция",
  77. "&aсломается",
  78. "",
  79. "&7&l&nЗадержка перед гниением&7&l » &a&l" + Utils.getTimeFormat(blockDecayTime),
  80. "",
  81. "&7- Задержка, в течение которого конструкция &aне &7гниет.",
  82. "&7Сбрасывается, если конструкция в &aзоне &7шкафа",
  83. "",
  84. "&7&l&nВремя гниения&7&l » &a&l" + Utils.getTimeFormat(decayTime),
  85. "",
  86. "&7- Время, по &aистечении &7которого конструкция потеряет",
  87. "&a1 &7единицу прочности"
  88. ).getStack()
  89. :
  90. ISBuilder.getBuilder(Material.RAW_CHICKEN, 0, 1, "&a&l» &e&lИнформация",
  91. "",
  92. "&7&l&nПрочность&7&l » &a&l" + health + "&7&l/&a&l" + type.getMaxHealth(),
  93. "",
  94. "&7- Если прочность будет равна &a0&7, то конструкция",
  95. "&aсломается"
  96. ).getStack()
  97. );
  98.  
  99. inventory.setItem(11, health == type.getMaxHealth()
  100. ?
  101. ISBuilder.getBuilder(Material.COOKIE, 0, 1, "&a&l» &c&lКонструкция целая").getStack()
  102. :
  103. resourceAmount < repairCost
  104. ?
  105. ISBuilder.getBuilder(Material.COOKIE, 0, 1, "&a&l» &e&lОтремонтировать").addLore(
  106. "",
  107. "&7&l&nДля ремонта нужны ресурсы:",
  108. "",
  109. "&7- " + ChatColor.stripColor(type.getResourceStack().getItemMeta().getDisplayName().toLowerCase()) + " (" + (resourceAmount >= repairCost ? "&a" : "&c") + repairCost + "&7) - " + (resourceAmount < repairCost ? "не хватает - &c" + (repairCost - resourceAmount) : "у вас - &a" + resourceAmount)
  110. ).getStack()
  111. :
  112. ISBuilder.getBuilder(Material.COOKIE, 0, 1, "&a&l» &e&lОтремонтировать").addLore(
  113. "",
  114. "&7&l&nДля ремонта нужны ресурсы:",
  115. "",
  116. "&7- " + ChatColor.stripColor(type.getResourceStack().getItemMeta().getDisplayName().toLowerCase()) + " (" + (resourceAmount >= repairCost ? "&a" : "&c") + repairCost + "&7) - " + (resourceAmount < repairCost ? "не хватает - &c" + (repairCost - resourceAmount) : "у вас - &a" + resourceAmount)
  117. ).setTag("location", Utils.serialiseLocation(location)).setTag("repairCost", repairCost).getStack()
  118. );
  119.  
  120.  
  121. inventory.setItem(15, (type.getDrop() == null
  122. ?
  123. isDeleting
  124. ?
  125. ISBuilder.getBuilder(Material.RAW_BEEF, 0, 1, "&a&l» &c&lСломать")
  126. .addLore(
  127. "",
  128. "&7Вы уничтожите конструкцию и &cне &7получите &7ее назад"
  129. ).setTag("location", Utils.serialiseLocation(location))
  130. :
  131. ISBuilder.getBuilder(Material.RAW_BEEF, 0, 1, "&a&l» &c&lНельзя сломать",
  132. "",
  133. "&7Время удаления конструкции истекло"
  134. )
  135. :
  136.  
  137. ISBuilder.getBuilder(Material.MELON, 0, 1, "&a&l» &e&lЗабрать")
  138. .addLore(
  139. "",
  140. "&7Вы уничтожите конструкцию и &aполучите &7ее назад"
  141. ).setTag("location", Utils.serialiseLocation(location))
  142. ).getStack()
  143. );
  144.  
  145. 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());
  146. player.openInventory(inventory);
  147. }
  148.  
  149. public Location getLocation() {
  150. return location;
  151. }
  152.  
  153. public CustomBlock.Type getType() {
  154. return type;
  155. }
  156.  
  157. public int getHealth() {
  158. return health;
  159. }
  160.  
  161. public int getDecayTime() {
  162. return decayTime;
  163. }
  164.  
  165. public int getBlockDecayTime() {
  166. return blockDecayTime;
  167. }
  168.  
  169. public boolean isDeleting() {
  170. return isDeleting;
  171. }
  172.  
  173. public void setType(CustomBlock.Type type) {
  174. this.type = type;
  175. }
  176.  
  177. public void setHealth(int health) {
  178. this.health = health;
  179. }
  180.  
  181. public void setDecayTime(int decayTime) {
  182. this.decayTime = decayTime;
  183. }
  184.  
  185. public void setBlockDecayTime(int blockDecayTime) {
  186. this.blockDecayTime = blockDecayTime;
  187. }
  188.  
  189. public void setDeleting(boolean deleting) {
  190. isDeleting = deleting;
  191. }
  192.  
  193. public void damage(int damage) {
  194.  
  195. health = health - damage;
  196.  
  197. if (health > 0) {
  198. SQLManager.getManager().updateBuildingData(this);
  199. return;
  200. }
  201.  
  202. Bukkit.getPluginManager().callEvent(new RustBlockDecayEvent(location.getBlock(), type));
  203.  
  204. if (type.isDoubleBlock()) {
  205. location.clone().add(0, 1, 0).getBlock().setType(Material.AIR, false);
  206. buildingsMap.remove(location.clone().add(0, 1, 0));
  207. }
  208.  
  209. checkUppedBlocks(location.clone().add(0, 1, 0).getBlock().getLocation());
  210. breakBlock();
  211. }
  212.  
  213. public void remove(Player player) {
  214.  
  215. Bukkit.getPluginManager().callEvent(new RustBlockBreakEvent(player, type, new BlockBreakEvent(location.getBlock(), player)));
  216.  
  217. if (type.isDoubleBlock()) {
  218. location.clone().add(0, 1, 0).getBlock().setType(Material.AIR, false);
  219. buildingsMap.remove(location.clone().add(0, 1, 0));
  220. }
  221.  
  222. checkUppedBlocks(location.clone().add(0, 1, 0).getBlock().getLocation());
  223. breakBlock();
  224. }
  225.  
  226. private void checkUppedBlocks(Location location) {
  227.  
  228. CustomBlock upperBlock = CustomBlock.getBlock(location.getBlock());
  229.  
  230. if (upperBlock == null) return;
  231. if (!upperBlock.getType().isGround()) return;
  232.  
  233. Buildings building = fromLocation(location);
  234.  
  235. if (building != null) {
  236. buildingsMap.remove(location);
  237. SQLManager.getManager().deleteBuildingData(building);
  238. }
  239.  
  240. Bukkit.getPluginManager().callEvent(new RustPhysicBreakBlockEvent(location.getBlock(), upperBlock.getType()));
  241. checkUppedBlocks(location.clone().add(0, 1, 0));
  242. location.getBlock().setType(Material.AIR);
  243. 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);
  244. location.getWorld().playSound(location, Sounds.BREAK.getSoundName(), 1, 1);
  245. }
  246.  
  247. private void breakBlock() {
  248. location.getBlock().setType(Material.AIR, false);
  249. buildingsMap.remove(location);
  250. SQLManager.getManager().deleteBuildingData(this);
  251. location.getWorld().playSound(location, Sounds.BREAK.getSoundName(), 1, 1);
  252. 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);
  253. }
  254.  
  255. public int getRepairCost(Player player) {
  256. int coef = ((type.getMaxHealth() - health) / 10);
  257. return coef == 0 ? 1 : coef;
  258. }
  259.  
  260. public void runDecay() {
  261.  
  262. new BukkitRunnable() {
  263.  
  264. @Override
  265. public void run() {
  266.  
  267. if (!buildingsMap.containsKey(location)) {
  268. cancel();
  269. return;
  270. }
  271.  
  272. if (decayTime == -1) return;
  273.  
  274. if (Cupboard.fromLocation(location) != null) {
  275. blockDecayTime = Config.BUILDINGS_BLOCK_DECAY_TIME.getInteger();
  276. return;
  277. }
  278.  
  279. if (blockDecayTime != 0) {
  280. --blockDecayTime;
  281. return;
  282. }
  283.  
  284. if (decayTime != 0) {
  285. --decayTime;
  286. return;
  287. }
  288.  
  289. damage(1);
  290. decayTime = Config.BUILDINGS_DECAY_TIME.getInteger();
  291. }
  292. }.runTaskTimer(Main.getPlugin(), 1, 20);
  293. }
  294.  
  295. public void runTimeDelete() {
  296.  
  297. if (!type.isTimedDelete()) return;
  298.  
  299. new BukkitRunnable() {
  300.  
  301. @Override
  302. public void run() {
  303.  
  304. if (!buildingsMap.containsKey(location)) {
  305. cancel();
  306. return;
  307. }
  308.  
  309. if (!isDeleting) return;
  310.  
  311. isDeleting = false;
  312. SQLManager.getManager().updateBuildingData(Buildings.this);
  313. }
  314. }.runTaskLater(Main.getPlugin(), Config.BUILDINGS_DELETE_TIME.getInteger() * 20);
  315. }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement