/magiccarpet/src/MagicCarpet.java

https://github.com/ricin/minecraft-hey0-plugins · Java · 176 lines · 167 code · 8 blank · 1 comment · 29 complexity · f6e41c23e302a1b3aa7c11b76d66801a MD5 · raw file

  1. import java.util.Hashtable;
  2. import java.util.Enumeration;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5. public class MagicCarpet extends Plugin
  6. {
  7. private static Logger a = Logger.getLogger("Minecraft");
  8. private Hashtable carpets = new Hashtable();
  9. private Listener l = new Listener(this);
  10. public MagicCarpet()
  11. {
  12. }
  13. public class CarpetFiber
  14. {
  15. public CarpetFiber(int x, int y, int z, int type)
  16. {
  17. this.x = x;
  18. this.y = y;
  19. this.z = z;
  20. this.type = type;
  21. }
  22. int x,y,z,type = 0;
  23. boolean destroyed = false;
  24. boolean imadeit = false;
  25. }
  26. public class Carpet
  27. {
  28. public CarpetFiber[] fibers = {
  29. new CarpetFiber(2, 0, 2, 20),
  30. new CarpetFiber(2, 0, 1, 20),
  31. new CarpetFiber(2, 0, 0, 20),
  32. new CarpetFiber(2, 0, -1, 20),
  33. new CarpetFiber(2, 0, -2, 20),
  34. new CarpetFiber(1, 0, 2, 20),
  35. new CarpetFiber(1, 0, 1, 20),
  36. new CarpetFiber(1, 0, 0, 20),
  37. new CarpetFiber(1, 0, -1, 20),
  38. new CarpetFiber(1, 0, -2, 20),
  39. new CarpetFiber(0, 0, 2, 20),
  40. new CarpetFiber(0, 0, 1, 20),
  41. new CarpetFiber(0, 0, 0, 20),
  42. new CarpetFiber(0, 0, -1, 20),
  43. new CarpetFiber(0, 0, -2, 20),
  44. new CarpetFiber(-1, 0, 2, 20),
  45. new CarpetFiber(-1, 0, 1, 20),
  46. new CarpetFiber(-1, 0, 0, 20),
  47. new CarpetFiber(-1, 0, -1, 20),
  48. new CarpetFiber(-1, 0, -2, 20),
  49. new CarpetFiber(-2, 0, 2, 20),
  50. new CarpetFiber(-2, 0, 1, 20),
  51. new CarpetFiber(-2, 0, 0, 20),
  52. new CarpetFiber(-2, 0, -1, 20),
  53. new CarpetFiber(-2, 0, -2, 20)
  54. };
  55. Location currentLoc;
  56. public void removeCarpet() {
  57. if (currentLoc == null)
  58. return;
  59. for(int i = 0; i < fibers.length; i++)
  60. {
  61. if (fibers[i].imadeit) etc.getServer().setBlockAt(0, (int)Math.floor(currentLoc.x) + fibers[i].x, (int)Math.floor(currentLoc.y) - fibers[i].y, (int)Math.floor(currentLoc.z) + fibers[i].z);
  62. fibers[i].imadeit = false;
  63. }
  64. }
  65. public void drawCarpet() {
  66. for(int i = 0; i < fibers.length; i++)
  67. {
  68. if (!fibers[i].destroyed && etc.getServer().getBlockAt((int)Math.floor(currentLoc.x) + fibers[i].x, (int)Math.floor(currentLoc.y) - fibers[i].y, (int)Math.floor(currentLoc.z) + fibers[i].z).getType() == 0) {
  69. fibers[i].imadeit = true;
  70. int blocktypeunder = etc.getServer().getBlockIdAt((int)Math.floor(currentLoc.x) + fibers[i].x, (int)Math.floor(currentLoc.y) - fibers[i].y-1, (int)Math.floor(currentLoc.z) + fibers[i].z);
  71. if (blocktypeunder!=67 && blocktypeunder!=53) {
  72. etc.getServer().setBlockAt(fibers[i].type, (int)Math.floor(currentLoc.x) + fibers[i].x, (int)Math.floor(currentLoc.y) - fibers[i].y, (int)Math.floor(currentLoc.z) + fibers[i].z);
  73. }
  74. } else {
  75. fibers[i].imadeit = false;
  76. }
  77. }
  78. }
  79. public CarpetFiber getCenterFiber()
  80. {
  81. for(int i = 0; i < fibers.length; i++)
  82. {
  83. if(fibers[i].x == 0 && fibers[i].y == 0 && fibers[i].z == 0)
  84. return fibers[i];
  85. }
  86. return null;
  87. }
  88. }
  89. public void enable()
  90. {
  91. etc.getInstance().addCommand("/magiccarpet", "Take yourself wonder by wonder");
  92. }
  93. public void disable()
  94. {
  95. etc.getInstance().removeCommand("/magiccarpet");
  96. Enumeration e = carpets.elements();
  97. //iterate through Hashtable keys Enumeration
  98. while(e.hasMoreElements()) {
  99. Carpet c = (Carpet)e.nextElement();
  100. c.removeCarpet();
  101. }
  102. carpets.clear();
  103. }
  104. public void initialize()
  105. {
  106. etc.getLoader().addListener(PluginLoader.Hook.COMMAND, l, this, PluginListener.Priority.MEDIUM);
  107. etc.getLoader().addListener(PluginLoader.Hook.PLAYER_MOVE, l, this, PluginListener.Priority.MEDIUM);
  108. etc.getLoader().addListener(PluginLoader.Hook.DISCONNECT, l, this, PluginListener.Priority.MEDIUM);
  109. }
  110. public class Listener extends PluginListener {
  111. private MagicCarpet plugin;
  112. Listener(MagicCarpet pl)
  113. {
  114. plugin = pl;
  115. }
  116. public boolean onCommand(Player player, String[] split)
  117. {
  118. try {
  119. if ((split[0].equalsIgnoreCase("/magiccarpet")||split[0].equalsIgnoreCase("/mc")) && player.canUseCommand("/magiccarpet")) {
  120. Carpet carpet = (Carpet)carpets.get(player.getName());
  121. if (carpet == null)
  122. {
  123. if(player.canUseCommand("/adult_language"))
  124. player.sendMessage("You are now on a carpet! OH SHIT!");
  125. else
  126. player.sendMessage("My goodness dear chap, it would appear you are on some sort of magical carpet capable of arial travel!");
  127. carpets.put(player.getName(), new Carpet());
  128. return true;
  129. }
  130. else
  131. {
  132. if(player.canUseCommand("/adult_language"))
  133. player.sendMessage("Hot damn! The carpet disappears!");
  134. else
  135. player.sendMessage("... And as quickly as it arrived, the magically imbued decorative rug leaves you.");
  136. carpets.remove(player.getName());
  137. Location from = player.getLocation();
  138. carpet.removeCarpet();
  139. }
  140. return true;
  141. }
  142. } catch (Exception ex) {
  143. a.log(Level.SEVERE, "Exception in command handler (Report this to chrisinajar):", ex);
  144. return false;
  145. }
  146. return false;
  147. }
  148. public void onPlayerMove(Player player, Location from, Location to)
  149. {
  150. Carpet carpet = (Carpet)carpets.get(player.getName());
  151. if (carpet == null)
  152. return;
  153. carpet.removeCarpet();
  154. to.y = to.y-1;
  155. if(player.getPitch() >= 80)
  156. to.y = to.y-1;
  157. carpet.currentLoc = to;
  158. carpet.drawCarpet();
  159. }
  160. public void onDisconnect(Player player) {
  161. Carpet carpet = (Carpet)carpets.get(player.getName());
  162. if (carpet == null)
  163. return;
  164. carpets.remove(player.getName());
  165. carpet.removeCarpet();
  166. }
  167. }
  168. }