Advertisement
Lisenochek

Untitled

Oct 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. package ru.lisenochek.mcrust.objects.blockMechanic;
  2.  
  3. import org.bukkit.Location;
  4. import org.bukkit.Material;
  5. import org.bukkit.inventory.ItemStack;
  6. import org.bukkit.scheduler.BukkitRunnable;
  7. import ru.lisenochek.mcrust.Items;
  8. import ru.lisenochek.mcrust.Main;
  9. import ru.lisenochek.mcrust.sql.SQLManager;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.List;
  14.  
  15. public class Chair {
  16.  
  17. private static HashMap<Location, Chair> chairsMap = new HashMap<>();
  18.  
  19. private Location location;
  20. private String ownerName;
  21. private Type type;
  22. private int time;
  23.  
  24. public Chair(Location location, String ownerName, Type type, int time) {
  25. this.location = location;
  26. this.ownerName = ownerName;
  27. this.type = type;
  28. this.time = time;
  29. chairsMap.put(location, this);
  30. runTimer();
  31. }
  32.  
  33. public static Chair create(Location location, String ownerName, Type type, int time) {
  34. return new Chair(location, ownerName, type, time);
  35. }
  36.  
  37. public static List<Chair> getFromOwner(String playerName) {
  38. List<Chair> list = new ArrayList<>();
  39. for (Chair spc : chairsMap.values()) if (spc.getOwnerName().equals(playerName)) list.add(spc);
  40. return list;
  41. }
  42.  
  43. public static Chair fromLocation(Location location) {
  44. return chairsMap.get(location);
  45. }
  46.  
  47. public Location getLocation() {
  48. return location;
  49. }
  50.  
  51. public String getOwnerName() {
  52. return ownerName;
  53. }
  54.  
  55. public Type getType() {
  56. return type;
  57. }
  58.  
  59. public int getTime() {
  60. return time;
  61. }
  62.  
  63. public void setTime(int time) {
  64. this.time = time;
  65. }
  66.  
  67. public void remove() {
  68. SQLManager.getManager().deleteChairData(this);
  69. Chair.chairsMap.remove(location);
  70. }
  71.  
  72. public void runTimer() {
  73.  
  74. new BukkitRunnable() {
  75.  
  76. @Override
  77. public void run() {
  78.  
  79. if (!chairsMap.containsValue(Chair.this)) {
  80. cancel();
  81. return;
  82. }
  83.  
  84. if (location.getBlock().getType() == Material.AIR) {
  85. chairsMap.remove(location);
  86. SQLManager.getManager().deleteChairData(Chair.this);
  87. cancel();
  88. return;
  89. }
  90.  
  91. if (time == 0) return;
  92.  
  93. --time;
  94. SQLManager.getManager().updateChairData(Chair.this);
  95. }
  96. }.runTaskTimer(Main.plugin, 1, 20);
  97. }
  98.  
  99. public enum Type {
  100.  
  101. WOODEN_CHAIR(150, Items.WOODEN_CHAIR.getStack()),
  102. UPGRADED_CHAIR(90, Items.UPGRADED_CHAIR.getStack()),
  103. METAL_CHAIR(60, Items.METAL_CHAIR.getStack());
  104.  
  105. private int cooldown;
  106. private ItemStack icon;
  107.  
  108. Type(int cooldown, ItemStack stack) {
  109. this.cooldown = cooldown;
  110. this.icon = stack;
  111. }
  112.  
  113. public int getCooldown() {
  114. return cooldown;
  115. }
  116.  
  117. public ItemStack getStack() {
  118. return icon;
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement