Advertisement
Guest User

Untitled

a guest
May 30th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.73 KB | None | 0 0
  1. package main.java.com.norcode.bukkit.jukeloop;
  2.  
  3. import java.util.LinkedHashMap;
  4.  
  5. import org.bukkit.Location;
  6. import org.bukkit.Material;
  7. import org.bukkit.block.BlockFace;
  8. import org.bukkit.block.BlockState;
  9. import org.bukkit.block.Chest;
  10. import org.bukkit.block.Hopper;
  11. import org.bukkit.block.Jukebox;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.inventory.Inventory;
  14. import org.bukkit.inventory.ItemStack;
  15.  
  16. public class LoopingJukebox {
  17. private Location location;
  18. private Jukebox jukebox;
  19. private Chest chest;
  20. private JukeLoopPlugin plugin;
  21. private int startedAt = -1;
  22. public boolean isDead = false;
  23. private int chestSlot = -1;
  24. public static LinkedHashMap<Location, LoopingJukebox> jukeboxMap = new LinkedHashMap<Location, LoopingJukebox>();
  25.  
  26. public static LoopingJukebox getAt(JukeLoopPlugin plugin, Location loc) {
  27. LoopingJukebox box = null;
  28. if (jukeboxMap.containsKey(loc)) {
  29. box = jukeboxMap.get(loc);
  30. } else {
  31. box = new LoopingJukebox(plugin, loc);
  32. }
  33. if (box.validate()) {
  34. jukeboxMap.put(loc, box);
  35. return box;
  36. }
  37. return null;
  38. }
  39.  
  40. public Location getLocation() {
  41. return location;
  42. }
  43.  
  44. public LoopingJukebox(JukeLoopPlugin plugin, Location location) {
  45. this.location = location;
  46. this.plugin = plugin;
  47. }
  48.  
  49. public void log(String msg) {
  50. if (plugin.debugMode) {
  51. plugin.getLogger().info(
  52. "[Jukebox@" + location.getWorld().getName() + " "
  53. + location.getBlockX() + " " + location.getBlockY()
  54. + " " + location.getBlockZ() + "] " + msg);
  55. }
  56. }
  57.  
  58. public Jukebox getJukebox() {
  59. try {
  60. return (Jukebox) this.location.getBlock().getState();
  61. } catch (ClassCastException ex) {
  62. return null;
  63. }
  64. }
  65.  
  66. public Chest getChest() {
  67. if (this.chest == null) {
  68. return null;
  69. } else {
  70. try {
  71. return (Chest) this.chest.getBlock().getState();
  72. } catch (ClassCastException ex) {
  73. }
  74. }
  75. return null;
  76. }
  77.  
  78. public boolean validate() {
  79. try {
  80. this.jukebox = (Jukebox) this.location.getBlock().getState();
  81. } catch (ClassCastException ex) {
  82. return false;
  83. } catch (NullPointerException ex) {
  84. return false;
  85. }
  86. this.chest = null;
  87. BlockState rel = null;
  88. for (BlockFace f: JukeLoopPlugin.directions) {
  89. try {
  90. rel = (BlockState)this.jukebox.getBlock().getRelative(f).getState();
  91. this.chest = (Chest) rel;
  92. if (!containsRecords(this.chest.getInventory())) {
  93. log(this.chest + " does not contain records. skipping.");
  94. continue;
  95. }
  96. this.chest = (Chest) rel;
  97. break;
  98. } catch (ClassCastException ex) {
  99. log(ex.getMessage());
  100. continue;
  101. }
  102. }
  103. return true;
  104. }
  105.  
  106. public boolean containsRecords(Inventory inv) {
  107. for (ItemStack s : inv.getContents()) {
  108. if (s != null && JukeLoopPlugin.recordDurations.keySet().contains(s.getType())) {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114.  
  115. public boolean playersNearby() {
  116. double dist;
  117. for (Player p : plugin.getServer().getOnlinePlayers()) {
  118. try {
  119. dist = getJukebox().getLocation().distance(p.getLocation());
  120. plugin.debug("distance from " + getJukebox().getLocation() + " to " + p.getLocation());
  121. if (dist <= 64) {
  122. return true;
  123. }
  124. } catch (IllegalArgumentException ex) { // cross-world.
  125. plugin.debug("Cross world distance-check.");
  126. }
  127. }
  128.  
  129. return false;
  130. }
  131.  
  132. public void doLoop() {
  133.  
  134. Jukebox jukebox = getJukebox();
  135. if (jukebox == null) {
  136. this.isDead = true;
  137. log("doLoop:Died.");
  138. return;
  139. }
  140.  
  141. if (!getJukebox().isPlaying()) {
  142. log("doLoop:not playing.");
  143. return;
  144. }
  145.  
  146. int now = (int) (System.currentTimeMillis() / 1000);
  147. Material record = jukebox.getPlaying();
  148. Integer duration = JukeLoopPlugin.recordDurations.get(record);
  149. if (duration != null) {
  150. if (now - startedAt > duration) {
  151. if (!playersNearby()) {
  152. log("doLoop:No player nearby.");
  153. return;
  154. }
  155. if (!putInHopper()) {
  156. if (!putInChest()) {
  157. log("doLoop:Couldn't put " + record + " anywhere, repeating.");
  158. jukebox.setPlaying(record);
  159. onInsert(record);
  160. return;
  161. }
  162. }
  163. if (!takeFromHopper()) {
  164. if (!takeFromChest()) {
  165. log("This shouldn't happen");
  166. }
  167. }
  168. }
  169. }
  170. }
  171.  
  172. private static final BlockFace[] hopperDirections = new BlockFace[] {
  173. BlockFace.DOWN,
  174. BlockFace.UP,
  175. BlockFace.NORTH,
  176. BlockFace.SOUTH,
  177. BlockFace.WEST,
  178. BlockFace.EAST
  179. };
  180.  
  181. private boolean putInHopper() {
  182. BlockState blockBelow = jukebox.getBlock().getRelative(BlockFace.DOWN).getState();
  183. if (blockBelow.getType().equals(Material.HOPPER)) {
  184. Hopper hopper = (Hopper)blockBelow;
  185. if (hopper.getInventory().addItem(new ItemStack(jukebox.getPlaying())).isEmpty()) {
  186. jukebox.setPlaying(null);
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. private boolean putInChest() {
  193. Chest chest = getChest();
  194. if (chest != null) {
  195. Inventory inv = chest.getInventory();
  196. if (chestSlot == -1 || chestSlot > chest.getInventory().getSize()-1 ||(inv.getItem(chestSlot) != null && !inv.getItem(chestSlot).getType().equals(Material.AIR))) {
  197. chestSlot = inv.firstEmpty();
  198. }
  199. if (chestSlot >= 0) {
  200. log("Placing " + jukebox.getPlaying() + " in slot " + chestSlot + " of chest@"+chest.getLocation().getBlockX() + ","+chest.getLocation().getBlockY() +","+ chest.getLocation().getBlockZ());
  201. inv.setItem(chestSlot, new ItemStack(jukebox.getPlaying()));
  202. jukebox.setPlaying(null);
  203. return true;
  204. } else {
  205. log("Failed to place " + jukebox.getPlaying() + " in chest@"+chest.getLocation().getBlockX() + ","+chest.getLocation().getBlockY() +","+ chest.getLocation().getBlockZ() + " there is no free slot.");
  206. }
  207. } else {
  208. log("putInChest:there is no chest.");
  209. }
  210. return false;
  211. }
  212.  
  213. public boolean takeFromChest() {
  214. Chest chest = getChest();
  215. if (chest != null) {
  216. Inventory inv = chest.getInventory();
  217. int i = chestSlot + 1;
  218. while (i != chestSlot) {
  219. if (i > inv.getSize()-1) {
  220. i = 0;
  221. }
  222. ItemStack s = inv.getItem(i);
  223. if (s != null && JukeLoopPlugin.recordDurations.containsKey(s.getType())) {
  224. log("Taking " + s.getType() + " from slot " + i + " of chest@"+chest.getLocation().getBlockX() + ","+chest.getLocation().getBlockY() +","+ chest.getLocation().getBlockZ());
  225. jukebox.setPlaying(s.getType());
  226. onInsert(jukebox.getPlaying());
  227. inv.setItem(i, null);
  228. chestSlot = i;
  229. return true;
  230. }
  231. ++i;
  232. }
  233. } else {
  234. log("putInChest:there is no chest.");
  235. }
  236. log("Failed to take a disc from the chest, there wasn't one.");
  237. return false;
  238. }
  239.  
  240. private boolean takeFromHopper() {
  241. for (BlockFace dir: JukeLoopPlugin.directions) {
  242. if (!dir.equals(BlockFace.DOWN)) {
  243. plugin.debug("Checking " + dir + " for hopper.");
  244. BlockState bs = this.getJukebox().getBlock().getRelative(dir).getState();
  245. if (bs.getType().equals(Material.HOPPER)) {
  246. plugin.debug("Found Hopper.");
  247. byte data = ((Hopper) bs).getData().getData();
  248. BlockFace attachedFace = hopperDirections[data & 7];
  249. boolean isAttached = attachedFace.getOppositeFace().equals(dir);
  250. Inventory inv = ((Hopper) bs).getInventory();
  251. if (isAttached) {
  252. for (int i=0;i<inv.getSize();i++) {
  253. ItemStack s = inv.getItem(i);
  254. if (s != null && JukeLoopPlugin.recordDurations.containsKey(s.getType())) {
  255. inv.setItem(i, null);
  256. this.getJukebox().setPlaying(s.getType());
  257. onInsert(s.getType());
  258. return true;
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }
  265. return false;
  266. }
  267.  
  268. public void onInsert(Material record) {
  269. startedAt = (int) (System.currentTimeMillis() / 1000);
  270. }
  271.  
  272. public void onEject() {
  273. this.startedAt = -1;
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement