/jimtest/src/jimtest.java

https://github.com/ricin/minecraft-hey0-plugins · Java · 262 lines · 225 code · 31 blank · 6 comment · 76 complexity · 2cd80678a302cd3839b6da360ecd26ca 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 jimtest extends Plugin {
  8. static final Logger log = Logger.getLogger("Minecraft");
  9. static MinecraftServer mcworld = etc.getMCServer();
  10. static ArrayList<Integer> watergateList = new ArrayList<Integer>();
  11. static ArrayList<String> playerList = new ArrayList<String>();
  12. static ArrayList<Integer> selectionStatus = new ArrayList<Integer>();
  13. static ArrayList<Integer> pointsCoordinates = new ArrayList<Integer>();
  14. static ArrayList<Integer> torchfollowints = new ArrayList<Integer>();
  15. private Timer watergateTicker;
  16. private int watergateTickInterval = 200;
  17. public void enable() {
  18. log.info("[Jim's Test Plugin] Mod Enabled.");
  19. if (watergateTickInterval > 0) {
  20. watergateTicker = new Timer();
  21. watergateTicker.schedule(new watergateTickerTask(), 400, watergateTickInterval);
  22. }
  23. }
  24. public void disable() {
  25. log.info("[Jim's Test Plugin] Mod Disabled");
  26. if (watergateTicker != null) {
  27. watergateTicker.cancel();
  28. }
  29. }
  30. public boolean onCommand(Player player,java.lang.String[] split) {
  31. if (split[0].equalsIgnoreCase("/saveWatergate")) {
  32. int savestatus = saveWaterGate(player.getName());
  33. if(savestatus == 1) {
  34. player.sendMessage("Not enough points set");
  35. } else if (savestatus == 2) {
  36. player.sendMessage("Watergate saved");
  37. }
  38. return true;
  39. } else if (split[0].equalsIgnoreCase("/clearunusedWatergates")) {
  40. if(watergateList.size()>0) {
  41. for(int i=0; i<watergateList.size()/6;i++) {
  42. if( mcworld.e.a(watergateList.get(i*6+3), watergateList.get(i*6+4), watergateList.get(i*6+5)) != 76 &&
  43. mcworld.e.a(watergateList.get(i*6+3), watergateList.get(i*6+4), watergateList.get(i*6+5)) != 75 ) {
  44. for(int j=0;j<6;j++) {
  45. watergateList.remove(i*6);
  46. }
  47. }
  48. }
  49. }
  50. player.sendMessage("Unused watergates have been cleared");
  51. return true;
  52. } else if (split[0].equalsIgnoreCase("/toggletorchfollow")) {
  53. int torchplayerIndex = getPlayerIndex(player.getName())*5;
  54. if(torchfollowints.get(torchplayerIndex) == 2) {
  55. player.sendMessage("Turned torchfollow on");
  56. torchfollowints.set(torchplayerIndex,1);
  57. } else {
  58. player.sendMessage("Turned torchfollow off");
  59. torchfollowints.set(torchplayerIndex,2);
  60. }
  61. return true;
  62. } else if (split[0].equalsIgnoreCase("/trytogetunloadedblock")) {
  63. int test = 0;
  64. for(int i=0;i<500;i++) {
  65. if(getablocktype((int)player.getX()+1000+i,(int)player.getY(),(int)player.getZ()) != 0) {
  66. test = 1;
  67. }
  68. }
  69. player.sendMessage(Integer.toString(test));
  70. return true;
  71. }
  72. return false;
  73. }
  74. public boolean onBlockCreate(Player player, Block block2, Block block, int ItemInHand) {
  75. if(ItemInHand == -1) {
  76. int whichPoint = setPoint(player.getName(),block.getX(),block.getY(),block.getZ());
  77. if(whichPoint == 2) {
  78. player.sendMessage("Watergate block selected");
  79. } else if (whichPoint == 1) {
  80. player.sendMessage("Redstone trigger selected");
  81. }
  82. }
  83. return false;
  84. }
  85. public void onPlayerMove(Player player, Location from, Location to) {
  86. int torchplayerIndex = getPlayerIndex(player.getName())*5;
  87. //if player has torch turned on
  88. if(torchfollowints.get(torchplayerIndex) == 1) {
  89. //if the new block will hold a torch
  90. int toX = (int)to.x;
  91. int toY = (int)to.y;
  92. int toZ = (int)to.z;
  93. if(willholdtorch(toX,toY,toZ)) {
  94. //if there is an old torch
  95. if(torchfollowints.get(torchplayerIndex+1) == 1) {
  96. //if we are more than 5 blocks away from it
  97. if(Math.abs(toX-torchfollowints.get(torchplayerIndex+2)) +
  98. Math.abs(toY-torchfollowints.get(torchplayerIndex+3)) +
  99. Math.abs(toZ-torchfollowints.get(torchplayerIndex+4)) > 6) {
  100. //if the torch is still there remove it
  101. if(getablocktype(torchfollowints.get(torchplayerIndex+2),
  102. torchfollowints.get(torchplayerIndex+3),
  103. torchfollowints.get(torchplayerIndex+4))==50) {
  104. setablocktype(torchfollowints.get(torchplayerIndex+2),
  105. torchfollowints.get(torchplayerIndex+3),
  106. torchfollowints.get(torchplayerIndex+4),0);
  107. }
  108. torchfollowints.set(torchplayerIndex+1,0);
  109. //place a torch
  110. setablocktype(toX,toY,toZ,50);
  111. torchfollowints.set(torchplayerIndex+1,1);
  112. torchfollowints.set(torchplayerIndex+2,(int)to.x);
  113. torchfollowints.set(torchplayerIndex+3,(int)to.y);
  114. torchfollowints.set(torchplayerIndex+4,(int)to.z);
  115. }
  116. } else {
  117. setablocktype(toX,toY,toZ,50);
  118. torchfollowints.set(torchplayerIndex+1,1);
  119. torchfollowints.set(torchplayerIndex+2,(int)to.x);
  120. torchfollowints.set(torchplayerIndex+3,(int)to.y);
  121. torchfollowints.set(torchplayerIndex+4,(int)to.z);
  122. }
  123. }
  124. }
  125. return;
  126. }
  127. private class watergateTickerTask extends TimerTask {
  128. public void run()
  129. {
  130. updateWaterGates();
  131. }
  132. }
  133. private void updateWaterGates() {
  134. if(watergateList.size()>0) {
  135. for(int i=0; i<watergateList.size()/6;i++) {
  136. if(mcworld.e.a(watergateList.get(i*6+3), watergateList.get(i*6+4), watergateList.get(i*6+5)) == 76 &&
  137. mcworld.e.a(watergateList.get(i*6), watergateList.get(i*6+1), watergateList.get(i*6+2)) == 47) {
  138. mcworld.e.d(watergateList.get(i*6), watergateList.get(i*6+1), watergateList.get(i*6+2),0);
  139. } else if(mcworld.e.a(watergateList.get(i*6+3), watergateList.get(i*6+4), watergateList.get(i*6+5)) == 75) {
  140. mcworld.e.d(watergateList.get(i*6), watergateList.get(i*6+1), watergateList.get(i*6+2),47);
  141. }
  142. }
  143. }
  144. }
  145. public int saveWaterGate(String playerName) {
  146. if(pointsCoordinates.get(getPlayerIndex(playerName)) != null) {
  147. int playerIndex = getPlayerIndex(playerName);
  148. for(int i = 0;i<6;i++) {
  149. watergateList.add(pointsCoordinates.get(playerIndex*6+i));
  150. }
  151. return 2;
  152. } else {
  153. return 1;
  154. }
  155. }
  156. private static int getPlayerIndex(String playerName){
  157. boolean inList = false;
  158. for (String p : playerList){
  159. if (p==playerName)
  160. inList = true;
  161. }
  162. if (!inList){
  163. playerList.add(playerName);
  164. selectionStatus.add(1);
  165. pointsCoordinates.add(null);
  166. pointsCoordinates.add(null);
  167. pointsCoordinates.add(null);
  168. pointsCoordinates.add(null);
  169. pointsCoordinates.add(null);
  170. pointsCoordinates.add(null);
  171. torchfollowints.add(1);
  172. torchfollowints.add(0);
  173. torchfollowints.add(null);
  174. torchfollowints.add(null);
  175. torchfollowints.add(null);
  176. }
  177. return playerList.indexOf(playerName);
  178. }
  179. public static int setPoint(String playerName, int X, int Y, int Z){
  180. int index = getPlayerIndex(playerName);
  181. int secondPoint = selectionStatus.get(index);
  182. if ( selectionStatus.get(index) == 1 ){
  183. pointsCoordinates.set(index*6, X);
  184. pointsCoordinates.set(index*6+1, Y);
  185. pointsCoordinates.set(index*6+2, Z);
  186. pointsCoordinates.set(index*6+3, null);
  187. pointsCoordinates.set(index*6+4, null);
  188. pointsCoordinates.set(index*6+5, null);
  189. selectionStatus.set(index, 2);
  190. }
  191. else if ( selectionStatus.get(index) == 2){
  192. pointsCoordinates.set(index*6+3, X);
  193. pointsCoordinates.set(index*6+4, Y);
  194. pointsCoordinates.set(index*6+5, Z);
  195. selectionStatus.set(index, 1);
  196. }
  197. return selectionStatus.get(index);
  198. }
  199. public static int getablocktype(int X, int Y, int Z) {
  200. return mcworld.e.a(X,Y,Z);
  201. }
  202. public static void setablocktype(int X, int Y, int Z, int Blocktype) {
  203. mcworld.e.d(X,Y,Z,Blocktype);
  204. return;
  205. }
  206. public static boolean willholdtorch(int X, int Y, int Z) {
  207. if(isblocksolid(getablocktype(X,Y-1,Z)) ||
  208. isblocksolid(getablocktype(X+1,Y,Z)) ||
  209. isblocksolid(getablocktype(X-1,Y,Z)) ||
  210. isblocksolid(getablocktype(X,Y,Z+1)) ||
  211. isblocksolid(getablocktype(X,Y,Z-1))) {
  212. return true;
  213. } else {
  214. return false;
  215. }
  216. }
  217. public static boolean isblocksolid(int type) {
  218. if(type == 1 ||
  219. type == 2 ||
  220. type == 3 ||
  221. type == 4 ||
  222. type == 5 ||
  223. type == 17 ||
  224. type == 18 ||
  225. type == 12) {
  226. return true;
  227. } else {
  228. return false;
  229. }
  230. }
  231. }