/src/com/afforess/minecartmaniaadmincontrols/AdminControlsListener.java

https://gitlab.com/N3X15/Minecart-Mania-Admin-Controls · Java · 199 lines · 178 code · 18 blank · 3 comment · 59 complexity · 0765c0f57039f04802a612f43a86519d MD5 · raw file

  1. package com.afforess.minecartmaniaadmincontrols;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4. import java.util.logging.Logger;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Material;
  7. import org.bukkit.entity.Minecart;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.EventHandler;
  10. import org.bukkit.event.EventPriority;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.event.block.Action;
  13. import org.bukkit.event.block.BlockBreakEvent;
  14. import org.bukkit.event.block.SignChangeEvent;
  15. import org.bukkit.event.player.PlayerInteractEvent;
  16. import org.bukkit.event.vehicle.VehicleEnterEvent;
  17. import com.afforess.minecartmaniaadmincontrols.permissions.SignTextUpdater;
  18. import com.afforess.minecartmaniacore.MinecartManiaCore;
  19. import com.afforess.minecartmaniacore.config.LocaleParser;
  20. import com.afforess.minecartmaniacore.event.MinecartManiaSignFoundEvent;
  21. import com.afforess.minecartmaniacore.event.MinecartManiaSignUpdatedEvent;
  22. import com.afforess.minecartmaniacore.event.MinecartTimeEvent;
  23. import com.afforess.minecartmaniacore.minecart.MinecartManiaMinecart;
  24. import com.afforess.minecartmaniacore.minecart.MinecartManiaStorageCart;
  25. import com.afforess.minecartmaniacore.signs.MinecartTypeSign;
  26. import com.afforess.minecartmaniacore.signs.Sign;
  27. import com.afforess.minecartmaniacore.signs.SignAction;
  28. import com.afforess.minecartmaniacore.signs.SignManager;
  29. import com.afforess.minecartmaniacore.world.MinecartManiaWorld;
  30. import com.afforess.minecartmaniacore.world.SpecificMaterial;
  31. public class AdminControlsListener implements Listener {
  32. @EventHandler(priority = EventPriority.NORMAL)
  33. public void onVehicleEnter(final VehicleEnterEvent event) {
  34. if (event.isCancelled())
  35. return;
  36. if (event.getVehicle() instanceof Minecart) {
  37. if (event.getEntered() instanceof Player) {
  38. if (VehicleControl.isBlockedFromEntering((Player) event.getEntered())) {
  39. event.setCancelled(true);
  40. ((Player) event.getEntered()).sendMessage(LocaleParser.getTextKey("AdminControlsBlockMinecartEntry"));
  41. }
  42. }
  43. }
  44. }
  45. @EventHandler(priority = EventPriority.NORMAL)
  46. public void onMinecartTimeEvent(final MinecartTimeEvent event) {
  47. final MinecartManiaMinecart minecart = event.getMinecart();
  48. int timer = -1;
  49. if (minecart.isStandardMinecart()) {
  50. timer = VehicleControl.getMinecartKillTimer();
  51. } else if (minecart.isStorageMinecart()) {
  52. timer = VehicleControl.getStorageMinecartKillTimer();
  53. } else {
  54. timer = VehicleControl.getPoweredMinecartKillTimer();
  55. }
  56. final boolean kill = (minecart.minecart.getPassenger() == null) && (!minecart.isStorageMinecart() || ((MinecartManiaStorageCart) minecart).isEmpty());
  57. if (timer > 0) {
  58. if (kill) {
  59. //No timer, start counting
  60. if (minecart.getDataValue("Empty Timer") == null) {
  61. minecart.setDataValue("Empty Timer", new Integer(timer));
  62. } else {
  63. //Decrement timer
  64. final Integer timeLeft = (Integer) minecart.getDataValue("Empty Timer");
  65. if (timeLeft > 1) {
  66. minecart.setDataValue("Empty Timer", new Integer(timeLeft.intValue() - 1));
  67. } else {
  68. minecart.kill();
  69. }
  70. }
  71. }
  72. //has passenger, resent counter if already set
  73. else {
  74. if (minecart.getDataValue("Empty Timer") != null) {
  75. minecart.setDataValue("Empty Timer", null);
  76. }
  77. }
  78. } else if (timer == 0) {
  79. if (kill) {
  80. minecart.kill();
  81. }
  82. }
  83. }
  84. @EventHandler(priority = EventPriority.MONITOR)
  85. public void onPlayerInteract(final PlayerInteractEvent event) {
  86. if (event.isCancelled())
  87. return;
  88. if (event.getAction() != Action.RIGHT_CLICK_BLOCK)
  89. return;
  90. if (MinecartManiaWorld.getConfigurationValue("MinecartTrackAdjuster") == null)
  91. return;
  92. if (event.getItem() == null)
  93. return;
  94. final Material type = event.getItem().getType();
  95. if (type == null)
  96. return;
  97. final SpecificMaterial mat = (SpecificMaterial) MinecartManiaWorld.getConfigurationValue("MinecartTrackAdjuster");
  98. if (type.getId() == mat.getId()) {
  99. if ((event.getClickedBlock() != null) && (event.getClickedBlock().getTypeId() == Material.RAILS.getId())) {
  100. final int oldData = event.getClickedBlock().getData();
  101. int data = oldData + 1;
  102. if (data > 9) {
  103. data = 0;
  104. }
  105. MinecartManiaWorld.setBlockData(event.getPlayer().getWorld(), event.getClickedBlock().getX(), event.getClickedBlock().getY(), event.getClickedBlock().getZ(), data);
  106. event.setCancelled(true);
  107. }
  108. }
  109. }
  110. @EventHandler(priority = EventPriority.NORMAL)
  111. public void onSignChange(final SignChangeEvent event) {
  112. if (event.isCancelled())
  113. return;
  114. Logger.getLogger("Minecraft").info("ONSIGNUPDATE: " + event.getBlock().getState().toString());
  115. if (!(event.getBlock().getState() instanceof org.bukkit.block.Sign))
  116. return;
  117. Sign sign = SignManager.getSignAt(event.getBlock().getLocation(), event.getPlayer());
  118. final String[] old = new String[4];
  119. for (int i = 0; i < 4; i++) {
  120. old[i] = ((org.bukkit.block.Sign) event.getBlock().getState()).getLine(i);
  121. sign.setLine(i, event.getLine(i), false);
  122. }
  123. final MinecartManiaSignFoundEvent mmsfe = new MinecartManiaSignUpdatedEvent(sign, event.getPlayer());
  124. MinecartManiaCore.callEvent(mmsfe);
  125. sign = mmsfe.getSign();
  126. final Collection<SignAction> actions = sign.getSignActions();
  127. final Iterator<SignAction> i = actions.iterator();
  128. final Player player = event.getPlayer();
  129. while (i.hasNext()) {
  130. final SignAction action = i.next();
  131. if (!MinecartManiaAdminControls.permissions.canCreateSign(player, action.getName())) {
  132. event.setCancelled(true);
  133. player.sendMessage(LocaleParser.getTextKey("LackPermissionForSign", action.getFriendlyName()));
  134. SignManager.updateSign(sign.getLocation(), null);
  135. break;
  136. }
  137. }
  138. if (!event.isCancelled() && (sign instanceof MinecartTypeSign)) {
  139. if (!MinecartManiaAdminControls.permissions.canCreateSign(player, "minecarttypesign")) {
  140. player.sendMessage(LocaleParser.getTextKey("LackPermissionForSign", "Minecart Type Sign"));
  141. SignManager.updateSign(sign.getLocation(), null);
  142. event.setCancelled(true);
  143. }
  144. }
  145. if (event.isCancelled()) {
  146. for (int j = 0; j < 4; j++) {
  147. sign.setLine(j, old[j], false);
  148. }
  149. } else {
  150. SignManager.updateSign(sign.getLocation(), sign);
  151. }
  152. }
  153. @EventHandler(priority = EventPriority.NORMAL)
  154. public void onBlockBreak(final BlockBreakEvent event) {
  155. if (event.isCancelled())
  156. return;
  157. if (event.getBlock().getState() instanceof org.bukkit.block.Sign) {
  158. final Sign sign = SignManager.getSignAt(event.getBlock().getLocation());
  159. final Collection<SignAction> actions = sign.getSignActions();
  160. final Iterator<SignAction> i = actions.iterator();
  161. final Player player = event.getPlayer();
  162. while (i.hasNext()) {
  163. final SignAction action = i.next();
  164. if (!MinecartManiaAdminControls.permissions.canBreakSign(player, action.getName())) {
  165. event.setCancelled(true);
  166. player.sendMessage(LocaleParser.getTextKey("LackPermissionToRemoveSign", action.getFriendlyName()));
  167. break;
  168. }
  169. }
  170. if (sign instanceof MinecartTypeSign) {
  171. if (!MinecartManiaAdminControls.permissions.canBreakSign(player, "minecarttypesign")) {
  172. player.sendMessage(LocaleParser.getTextKey("LackPermissionToRemoveSign", "Minecart Type Sign"));
  173. event.setCancelled(true);
  174. }
  175. }
  176. }
  177. if (event.isCancelled()) {
  178. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(MinecartManiaCore.getInstance(), new SignTextUpdater(event.getBlock().getLocation()), 5);
  179. }
  180. }
  181. }