Advertisement
Lisenochek

Untitled

Oct 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 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.Location;
  6. import org.bukkit.Material;
  7. import org.bukkit.entity.Entity;
  8. import org.bukkit.entity.HumanEntity;
  9. import org.bukkit.entity.LivingEntity;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.inventory.Inventory;
  12. import org.bukkit.inventory.ItemStack;
  13. import org.bukkit.scheduler.BukkitRunnable;
  14. import ru.lisenochek.mcrust.Items;
  15. import ru.lisenochek.mcrust.Main;
  16. import ru.lisenochek.mcrust.objects.world.entity.CustomEntity;
  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.*;
  22.  
  23. public class Campfire {
  24.  
  25. private static HashMap<Location, Campfire> campfireMap = new HashMap<>();
  26. private static List<Integer> campfireSlots = Arrays.asList(10, 11, 12, 13, 14);
  27.  
  28. private Location location;
  29. private boolean isBuring;
  30. private int taskID;
  31. private Inventory inventory;
  32.  
  33. public Campfire(Location location) {
  34. this.location = location;
  35. this.inventory = Bukkit.createInventory(null, 27, Utils.stripColor("&2&lКостер"));
  36. Campfire.campfireMap.put(location, this);
  37. }
  38.  
  39. public Campfire(Location location, String data) {
  40. this.location = location;
  41. this.inventory = Bukkit.createInventory(null, 27, Utils.stripColor("&2&lКостер"));
  42. for (String s : data.split(" ")) {
  43. String[] s1 = s.split("-");
  44. inventory.setItem(Integer.parseInt(s1[0]), ISBuilder.deserialiseStack(s1[1]));
  45. }
  46. Campfire.campfireMap.put(location, this);
  47. }
  48.  
  49. public static Campfire create(Location location) {
  50. return new Campfire(location);
  51. }
  52.  
  53. public static Campfire create(Location location, String data) {
  54. return new Campfire(location, data);
  55. }
  56.  
  57. public static void save() {
  58. for (Campfire campfire : campfireMap.values()) {
  59. campfire.stopBuring();
  60. SQLManager.getManager().updateCampfireData(campfire);
  61. }
  62. }
  63.  
  64. public static List<Integer> getCampfireSlots() {
  65. return campfireSlots;
  66. }
  67.  
  68. public static Campfire fromLocation(Location location) {
  69. return campfireMap.get(location);
  70. }
  71.  
  72. public Location getLocation() {
  73. return location;
  74. }
  75.  
  76. public boolean isBuring() {
  77. return isBuring;
  78. }
  79.  
  80. public List<ItemStack> getContents() {
  81. List<ItemStack> stacks = new ArrayList<>();
  82. for (int slot : campfireSlots) {
  83. ItemStack stack = inventory.getItem(slot);
  84. if (stack != null) stacks.add(stack);
  85. }
  86. return stacks;
  87. }
  88.  
  89. public void remove() {
  90. for (ItemStack stack : getContents()) location.getWorld().dropItemNaturally(location.clone().add(0.5D, 0, 0.5D), stack);
  91. stopBuring();
  92. Campfire.campfireMap.remove(location);
  93. SQLManager.getManager().deleteCampfireData(this);
  94. }
  95.  
  96. public void openGUI(Player player) {
  97.  
  98. inventory.setItem(16, (isBuring
  99. ?
  100. ISBuilder.getBuilder(Material.BOOK, 0, 1, "&a&l» &e&lКостер зажжен",
  101. "",
  102. "&7Нажмите, чтобы &aпотушить",
  103. "&7костер"
  104. )
  105. :
  106. ISBuilder.getBuilder(Material.BOOK, 0, 1, "&a&l» &e&lКостер потушен",
  107. "",
  108. "&7Нажмите, чтобы &aзажжечь",
  109. "&7костер"
  110. )).setTag("location", Utils.serialiseLocation(location)).getStack()
  111. );
  112.  
  113. inventory.setItem(18, ISBuilder.getBuilder(Material.BOOK, 0, 1, "&a&l» &e&lРабота костра",
  114. "",
  115. "&7&l&nЕда для костра&7&l:",
  116. "",
  117. "&aПоложите &7в любые &a5 &7слотов &aсырую еду &7и &aдревесину &7в качестве",
  118. "&aтоплива&7. Еда готовится &aодновременно &7и появляется в &aсвободных",
  119. "&7слотах",
  120. "",
  121. "&7&l&nРабота костра&7&l:",
  122. "",
  123. "&7Нажмите по иконке &aсправа&7, чтобы &aзажжечь &7костер. Костер",
  124. "&aпотухнет&7, если в нем &aзакончится &7древесина или он будет &aпереполнен",
  125. "&7едой"
  126. ).getStack());
  127.  
  128. for (int freeSlot = 0; freeSlot < inventory.getSize(); freeSlot++) if (!campfireSlots.contains(freeSlot) && inventory.getItem(freeSlot) == null) inventory.setItem(freeSlot, ISBuilder.getBuilder(Material.STAINED_GLASS_PANE, 0, 1, " ").getStack());
  129. player.openInventory(inventory);
  130. }
  131.  
  132. public void startBuring() {
  133.  
  134. if (isFull() || isEmpty()) return;
  135.  
  136. isBuring = true;
  137. for (HumanEntity entity : new ArrayList<>(inventory.getViewers())) openGUI((Player) entity);
  138.  
  139. BukkitRunnable runnable = new BukkitRunnable() {
  140.  
  141. int milsec = 0;
  142.  
  143. @Override
  144. public void run() {
  145.  
  146. if (!campfireMap.containsKey(location)) {
  147. cancel();
  148. return;
  149. }
  150.  
  151. if (isFull() || isEmpty()) {
  152. stopBuring();
  153. return;
  154. }
  155.  
  156. milsec++;
  157. Utils.playEffect(location.clone().add(0.5D, 0.1, 0.5D), false, false, 0.1F, 0.1F, 0.1F, 0, EnumWrappers.Particle.FLAME, Material.AIR, 3);
  158. Utils.playEffect(location.clone().add(0.5D, 0.1, 0.5D), false, false, 0.1F, 0.1F, 0.1F, 0, EnumWrappers.Particle.SMOKE_NORMAL, Material.AIR, 1);
  159.  
  160. if (milsec % 20 == 0) {
  161. for (Entity entity : location.getWorld().getNearbyEntities(location.clone().add(0.5D, 0, 0.5D), 0.5, 1, 0.5)) {
  162.  
  163. CustomEntity customEntity = CustomEntity.getEntity(entity.getType());
  164.  
  165. if (customEntity == null) continue;
  166. if (!customEntity.getType().isLiving()) continue;
  167.  
  168. ((LivingEntity) entity).damage(2);
  169. }
  170. }
  171.  
  172. if (milsec % (20 * 5) != 0) return;
  173.  
  174. for (int slot : campfireSlots) {
  175.  
  176. ItemStack item = inventory.getItem(slot);
  177.  
  178. if (item == null) continue;
  179.  
  180. if (Utils.checkItem(item, Items.WOOD.getStack())) {
  181. inventory.setItem(slot, ISBuilder.getBuilder(item).setAmount(item.getAmount() - 1).getStack());
  182. continue;
  183. }
  184.  
  185. for (Food food : Food.values()) {
  186.  
  187. if (!Utils.checkItem(item, food.getStack())) continue;
  188.  
  189. inventory.setItem(slot, ISBuilder.getBuilder(item).setAmount(item.getAmount() - 1).getStack());
  190.  
  191. Collection<ItemStack> dropList = inventory.addItem(food.getResult()).values();
  192.  
  193. if (dropList.size() != 0) stopBuring();
  194. for (ItemStack drops : dropList) location.getWorld().dropItemNaturally(location.clone().add(0.5D, 1, 0.5D), drops);
  195. }
  196. }
  197. }
  198. };
  199. runnable.runTaskTimer(Main.plugin, 1, 1);
  200. taskID = runnable.getTaskId();
  201. }
  202.  
  203. public void stopBuring() {
  204. isBuring = false;
  205. for (HumanEntity entity : new ArrayList<>(inventory.getViewers())) openGUI((Player) entity);
  206. Bukkit.getScheduler().cancelTask(taskID);
  207. }
  208.  
  209. private boolean isFull() {
  210.  
  211. boolean isFull = true;
  212.  
  213. for (int slot : campfireSlots) {
  214.  
  215. ItemStack stack = inventory.getItem(slot);
  216.  
  217. if (stack == null) {
  218. isFull = false;
  219. break;
  220. }
  221.  
  222. for (Food food : Food.values()) {
  223. if (!Utils.checkItem(food.getResult(), stack)) continue;
  224. if (stack.getAmount() == stack.getMaxStackSize()) continue;
  225. isFull = false;
  226. break;
  227. }
  228. }
  229.  
  230. return isFull;
  231. }
  232.  
  233. private boolean isEmpty() {
  234.  
  235. boolean isEmpty = true;
  236.  
  237. for (int slot : campfireSlots) {
  238. ItemStack stack = inventory.getItem(slot);
  239. if (stack == null) continue;
  240. if (!Utils.checkItem(Items.WOOD.getStack(), stack)) continue;
  241. isEmpty = false;
  242. break;
  243. }
  244.  
  245. return isEmpty;
  246. }
  247.  
  248. public String serialize() {
  249.  
  250. if (getContents().size() == 0) return null;
  251.  
  252. StringBuilder builder = new StringBuilder();
  253.  
  254. for (int slot : campfireSlots) {
  255. ItemStack stack = inventory.getItem(slot);
  256. if (stack == null) continue;
  257. builder.append(slot).append("-").append(ISBuilder.serialise(stack)).append(" ");
  258. }
  259.  
  260. return builder.toString();
  261. }
  262.  
  263. public enum Food {
  264.  
  265. RAW_MEAT(Items.RAW_MEAT.getStack(), Items.COOKED_MEAT.getStack()),
  266. RAW_FISH(Items.RAW_FISH.getStack(), Items.COOKED_FISH.getStack());
  267.  
  268. private ItemStack stack;
  269. private ItemStack result;
  270.  
  271. Food(ItemStack stack, ItemStack result) {
  272. this.stack = stack;
  273. this.result = result;
  274. }
  275.  
  276. public ItemStack getStack() {
  277. return stack;
  278. }
  279.  
  280. public ItemStack getResult() {
  281. return result;
  282. }
  283. }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement