PageRenderTime 72ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/MinersHelmet/src/MinersHelmet.java

https://github.com/mrsheen/minecraft-hey0-plugins
Java | 174 lines | 142 code | 26 blank | 6 comment | 33 complexity | 1d51eb03f7a8c29ca8c7e6d47ee66408 MD5 | raw file
  1. import java.util.logging.Level;
  2. import java.util.logging.Logger;
  3. import net.minecraft.server.MinecraftServer;
  4. import java.util.ArrayList;
  5. import java.io.*;
  6. import java.util.*;
  7. public class MinersHelmet extends Plugin {
  8. private MinersHelmetListener listener = new MinersHelmetListener();
  9. static final Logger log = Logger.getLogger("Minecraft");
  10. static MinecraftServer mcworld = etc.getMCServer();
  11. static ArrayList<String> playerList = new ArrayList<String>();
  12. static ArrayList<Integer> torchfollowints = new ArrayList<Integer>();
  13. public void enable() {
  14. log.info("[Jim's Test Plugin] Mod Enabled.");
  15. }
  16. public void disable() {
  17. log.info("[Jim's Test Plugin] Mod Disabled");
  18. }
  19. public void initialize() {
  20. etc.getLoader().addListener(PluginLoader.Hook.COMMAND, listener, this, PluginListener.Priority.MEDIUM);
  21. etc.getLoader().addListener(PluginLoader.Hook.PLAYER_MOVE, listener, this, PluginListener.Priority.MEDIUM);
  22. }
  23. public class MinersHelmetListener extends PluginListener
  24. {
  25. public boolean onCommand(Player player,java.lang.String[] split) {
  26. if (split[0].equalsIgnoreCase("/minehelmet") || split[0].equalsIgnoreCase("/mh")) {
  27. int torchplayerIndex = getPlayerIndex(player.getName())*6;
  28. if(torchfollowints.get(torchplayerIndex) == 2) {
  29. player.sendMessage("You now have a miners helmet!");
  30. torchfollowints.set(torchplayerIndex,1);
  31. } else {
  32. player.sendMessage("Aww. No more helmet for you.");
  33. torchfollowints.set(torchplayerIndex,2);
  34. }
  35. return true;
  36. } else if (split[0].equalsIgnoreCase("/mhleash")) {
  37. if(split.length == 2) {
  38. int torchplayerIndex = getPlayerIndex(player.getName())*6;
  39. try
  40. {
  41. torchfollowints.set(torchplayerIndex+5, Integer.parseInt(split[1]));
  42. }
  43. catch (NumberFormatException nfe)
  44. {
  45. player.sendMessage("Input was not a number");
  46. }
  47. return true;
  48. } else {
  49. player.sendMessage("Incorrect number of inputs");
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. public void onPlayerMove(Player player, Location from, Location to) {
  56. int torchplayerIndex = getPlayerIndex(player.getName())*6;
  57. //if player has torch turned on
  58. if(torchfollowints.get(torchplayerIndex) == 1) {
  59. //if the new block will hold a torch
  60. int toX = (int)to.x;
  61. int toY = (int)to.y;
  62. int toZ = (int)to.z;
  63. if(willholdtorch(toX,toY,toZ)) {
  64. //if there is an old torch
  65. if(torchfollowints.get(torchplayerIndex+1) == 1) {
  66. //if we are more than 5 blocks away from it
  67. if(Math.abs(toX-torchfollowints.get(torchplayerIndex+2)) +
  68. Math.abs(toY-torchfollowints.get(torchplayerIndex+3)) +
  69. Math.abs(toZ-torchfollowints.get(torchplayerIndex+4)) > torchfollowints.get(torchplayerIndex+5)) {
  70. //if the torch is still there remove it
  71. if(getablocktype(torchfollowints.get(torchplayerIndex+2),
  72. torchfollowints.get(torchplayerIndex+3),
  73. torchfollowints.get(torchplayerIndex+4))==50) {
  74. setablocktype(torchfollowints.get(torchplayerIndex+2),
  75. torchfollowints.get(torchplayerIndex+3),
  76. torchfollowints.get(torchplayerIndex+4),0);
  77. }
  78. torchfollowints.set(torchplayerIndex+1,0);
  79. //place a torch
  80. setablocktype(toX,toY,toZ,50);
  81. torchfollowints.set(torchplayerIndex+1,1);
  82. torchfollowints.set(torchplayerIndex+2,toX);
  83. torchfollowints.set(torchplayerIndex+3,toY);
  84. torchfollowints.set(torchplayerIndex+4,toZ);
  85. }
  86. } else {
  87. setablocktype(toX,toY,toZ,50);
  88. torchfollowints.set(torchplayerIndex+1,1);
  89. torchfollowints.set(torchplayerIndex+2,toX);
  90. torchfollowints.set(torchplayerIndex+3,toY);
  91. torchfollowints.set(torchplayerIndex+4,toZ);
  92. }
  93. }
  94. }
  95. return;
  96. }
  97. }
  98. private static int getPlayerIndex(String playerName){
  99. boolean inList = false;
  100. for (String p : playerList){
  101. if (p==playerName)
  102. inList = true;
  103. }
  104. if (!inList){
  105. playerList.add(playerName);
  106. torchfollowints.add(2);
  107. torchfollowints.add(0);
  108. torchfollowints.add(null);
  109. torchfollowints.add(null);
  110. torchfollowints.add(null);
  111. torchfollowints.add(0);
  112. }
  113. return playerList.indexOf(playerName);
  114. }
  115. public static int getablocktype(int X, int Y, int Z) {
  116. return mcworld.e.a(X,Y,Z);
  117. }
  118. public static void setablocktype(int X, int Y, int Z, int Blocktype) {
  119. mcworld.e.d(X,Y,Z,Blocktype);
  120. return;
  121. }
  122. public static boolean willholdtorch(int X, int Y, int Z) {
  123. if(getablocktype(X,Y,Z)==0) {
  124. if(isblocksolid(getablocktype(X,Y-1,Z)) ||
  125. isblocksolid(getablocktype(X+1,Y,Z)) ||
  126. isblocksolid(getablocktype(X-1,Y,Z)) ||
  127. isblocksolid(getablocktype(X,Y,Z+1)) ||
  128. isblocksolid(getablocktype(X,Y,Z-1))) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. public static boolean isblocksolid(int type) {
  135. if(type == 1 ||
  136. type == 2 ||
  137. type == 3 ||
  138. type == 4 ||
  139. type == 5 ||
  140. type == 17 ||
  141. type == 18 ||
  142. type == 12) {
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. }