/src/me/desmin88/mobdisguise/disguises/CreeperHandler.java

https://gitlab.com/N3X15/MobDisguise · Java · 86 lines · 75 code · 11 blank · 0 comment · 10 complexity · ed96aa3a6bf1541c39a45c541a7f6b2b MD5 · raw file

  1. package me.desmin88.mobdisguise.disguises;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4. import java.util.logging.Logger;
  5. import me.desmin88.mobdisguise.MobDisguise;
  6. import me.desmin88.mobdisguise.utils.MobIdEnum;
  7. import org.bukkit.entity.Player;
  8. public class CreeperHandler extends DisguiseHandler {
  9. Timer explodeTimer = new Timer();
  10. public CreeperHandler(final Player pl, final MobDisguise p) {
  11. super(pl, p, MobIdEnum.CREEPER.id);
  12. datawatcher.a(16, Byte.valueOf((byte) -1));
  13. datawatcher.a(17, Byte.valueOf((byte) 0));
  14. Logger.getLogger("Minecraft").info(pl.getName() + " has been disguised as a creeper.");
  15. }
  16. class ExplodeTask extends TimerTask {
  17. @Override
  18. public void run() {
  19. try {
  20. setExploding(false);
  21. explode();
  22. } catch (final Exception e) {
  23. return;
  24. }
  25. }
  26. }
  27. @Override
  28. public boolean handleEffectCommand(final String cmd, final String[] arg) {
  29. if (cmd.equalsIgnoreCase("explode")) {
  30. if (datawatcher == null) {
  31. player.sendMessage("Sorry, it appears that the datawatcher we need is null.");
  32. return true;
  33. }
  34. setExploding(true);
  35. explodeTimer.schedule(new ExplodeTask(), 1500);
  36. player.sendMessage(MobDisguise.pref + "You have been set to explode!");
  37. return true;
  38. }
  39. if (cmd.equalsIgnoreCase("power")) {
  40. if (datawatcher == null) {
  41. player.sendMessage("Sorry, it appears that the datawatcher we need is null.");
  42. return true;
  43. }
  44. setPowered(true);
  45. player.sendMessage(MobDisguise.pref + "You have been powered!");
  46. return true;
  47. }
  48. if (cmd.equalsIgnoreCase("unpower")) {
  49. if (datawatcher == null) {
  50. player.sendMessage("Sorry, it appears that the datawatcher we need is null.");
  51. return true;
  52. }
  53. setPowered(false);
  54. player.sendMessage(MobDisguise.pref + "You have been unpowered!");
  55. return true;
  56. }
  57. player.sendMessage(MobDisguise.pref + "/md e explode|power|unpower");
  58. return false;
  59. }
  60. public void explode() {
  61. player.getWorld().createExplosion(player.getLocation(), isPowered() ? 6f : 3f);
  62. player.teleport(player.getWorld().getSpawnLocation());
  63. }
  64. public boolean isPowered() {
  65. return datawatcher.getByte(17) == 1;
  66. }
  67. private void setPowered(final boolean b) {
  68. datawatcher.watch(17, Byte.valueOf(b ? (byte) 1 : (byte) 0));
  69. sendUpdate();
  70. }
  71. private void setExploding(final boolean b) {
  72. datawatcher.watch(16, Byte.valueOf((b) ? (byte) 1 : (byte) -1));
  73. sendUpdate();
  74. }
  75. }