PageRenderTime 182ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/mcautils/src/MCAUtils.java

https://github.com/mrsheen/minecraft-hey0-plugins
Java | 206 lines | 131 code | 18 blank | 57 comment | 29 complexity | 8b33227fc81dea5374b7bcd22a1cba23 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 MCAUtils extends Plugin {
  8. static final Logger log = Logger.getLogger("Minecraft");
  9. private Server server = etc.getServer();
  10. //private Properties props;
  11. private int[] disalloweditems;
  12. private Timer SaveAllTicker;
  13. private long SaveAllTickInterval = 3600000;
  14. PluginRegisteredListener walkListener;
  15. PluginRegisteredListener blockcreateListener;
  16. PluginRegisteredListener commandListener;
  17. walkListener listener_walk = new walkListener();
  18. blockcreateListener listener_blockcreate = new blockcreateListener();
  19. commandListener listener_command = new commandListener();
  20. public void enable() {
  21. log.info("[MCAUtils] Mod Enabled.");
  22. loadProperties();
  23. SaveAllTicker = new Timer();
  24. SaveAllTicker.schedule(new SaveAllTickerTask(), SaveAllTickInterval, SaveAllTickInterval);
  25. }
  26. public void disable() {
  27. log.info("[MCAUtils] Mod Disabled");
  28. if (SaveAllTicker != null) {
  29. SaveAllTicker.cancel();
  30. }
  31. etc.getLoader().removeListener(walkListener);
  32. etc.getLoader().removeListener(blockcreateListener);
  33. etc.getLoader().removeListener(commandListener);
  34. }
  35. public void initialize() {
  36. blockcreateListener = etc.getLoader().addListener(PluginLoader.Hook.BLOCK_CREATED, listener_blockcreate, this, PluginListener.Priority.MEDIUM);
  37. walkListener = etc.getLoader().addListener(PluginLoader.Hook.PLAYER_MOVE, listener_walk, this, PluginListener.Priority.MEDIUM);
  38. commandListener = etc.getLoader().addListener(PluginLoader.Hook.COMMAND, listener_command, this, PluginListener.Priority.MEDIUM);
  39. }
  40. private void loadProperties(){
  41. PropertiesFile properties = new PropertiesFile("MCAUtilsPlugin.properties");
  42. try {
  43. SaveAllTickInterval = properties.getLong("saveallintervalinminutes", 60) * 60000;
  44. String[] stringdisalloweditems = properties.getString("disalloweditems", "19,66,328,342,343").split(",");
  45. disalloweditems = new int[stringdisalloweditems.length];
  46. for (int i=0;i<stringdisalloweditems.length;i++) {
  47. try {
  48. disalloweditems[i] = Integer.parseInt(stringdisalloweditems[i]);
  49. } catch (NumberFormatException nfe) {
  50. disalloweditems[i] = -2;
  51. }
  52. }
  53. } catch (Exception e) {
  54. log.log(Level.SEVERE, "Exception while reading from MCAUtilsPlugin.properties", e);
  55. }
  56. // TODO : non-existant file
  57. }
  58. /*
  59. worser
  60. private void loadprops()
  61. {
  62. props = new Properties();
  63. props.setProperty("disalloweditems", "19,66,328,342,343");
  64. props.setProperty("saveallintervalinminutes", "60");
  65. try {
  66. props.load(new FileInputStream("MCAUtils.properties"));
  67. }
  68. catch(IOException e) {
  69. e.printStackTrace();
  70. }
  71. if(props.getProperty("disalloweditems") != "") {
  72. String[] stringdisalloweditems = props.getProperty("disalloweditems").split(",");
  73. disalloweditems = new int[stringdisalloweditems.length];
  74. for (int i=0;i<stringdisalloweditems.length;i++) {
  75. disalloweditems[i] = Integer.parseInt(stringdisalloweditems[i]);
  76. }
  77. }
  78. if(props.getProperty("saveallintervalinminutes") != "") {
  79. try {
  80. SaveAllTickInterval = Long.parseLong(props.getProperty("saveallintervalinminutes").trim()) * 60000;
  81. } catch (NumberFormatException nfe) {
  82. log.info(props.getProperty("saveallintervalinminutes") + " is not a valid time in minutes");
  83. }
  84. }
  85. try {
  86. OutputStream propOut = new FileOutputStream(new File("MCAUtils.properties"));
  87. props.store(propOut, "Properties for the MCAUtils plugin");
  88. }
  89. catch(IOException e) {
  90. e.printStackTrace();
  91. }
  92. return;
  93. }*/
  94. private class SaveAllTickerTask extends TimerTask {
  95. public void run() {
  96. log.info("MCAUtils is calling a save-all");
  97. server.useConsoleCommand("save-all");
  98. }
  99. }
  100. public class walkListener extends PluginListener {
  101. public void onPlayerMove(Player player, Location from, Location to) {
  102. if(player.getY()<-300) {
  103. player.setY(300);
  104. }
  105. }
  106. }
  107. public class commandListener extends PluginListener
  108. {
  109. public boolean onCommand(Player player, java.lang.String[] split) {
  110. if(split[0].equalsIgnoreCase("/tpos") && player.canUseCommand("/tpos")) {
  111. if(split.length==3) {
  112. try {
  113. player.teleportTo(Double.parseDouble(split[1]) + 0.5D,
  114. server.getHighestBlockY(Integer.parseInt(split[1]),
  115. Integer.parseInt(split[2])),
  116. Double.parseDouble(split[2]) + 0.5D,
  117. player.getRotation(),
  118. player.getPitch());
  119. player.sendMessage("Teleported");
  120. return true;
  121. } catch (NumberFormatException ex) {
  122. }
  123. }
  124. player.sendMessage("Incorrect arguments");
  125. return true;
  126. }
  127. return false;
  128. }
  129. }
  130. public class blockcreateListener extends PluginListener
  131. {
  132. public boolean onBlockCreate(Player player, Block blockPlaced, Block blockClicked, int itemInHand) {
  133. //block item if disallowed
  134. /*if(disalloweditems.length>0 && !player.canUseCommand("/useblockeditems")) {
  135. player.sendMessage("Reached");
  136. for (int i = 0; i<disalloweditems.length;i++) {
  137. if (itemInHand == disalloweditems[i]) {
  138. player.sendMessage("The use of this item has been blocked. Talk to an admin for more info");
  139. return true;
  140. }
  141. }
  142. }*/
  143. //whitelist fire
  144. if(itemInHand==259 || itemInHand==51) {
  145. if(player.canUseCommand("/usefire")) {
  146. player.sendMessage("Be careful with that fire");
  147. return false;
  148. } else {
  149. player.sendMessage("You can't use fire. Ask an admin to use it for you");
  150. return true;
  151. }
  152. }
  153. //feather
  154. else if ( itemInHand==288 && player.canUseCommand("/allowfirefeather")){
  155. for ( int i = (blockClicked.getX()-8); i<= blockClicked.getX()+8; i++ ){
  156. for ( int j = blockClicked.getY(); j<= 128; j++ ){
  157. for ( int k = (blockClicked.getZ()-8); k<= (blockClicked.getZ()+8); k++ ){
  158. if( server.getBlockIdAt(i, j, k) == 51 ){
  159. server.setBlockAt(0,i,j,k);
  160. }
  161. }
  162. }
  163. }
  164. return true;
  165. }
  166. // String
  167. else if ( itemInHand==287 && player.canUseCommand("/allowlavastring")){
  168. for ( int i = (blockClicked.getX()-2); i<= blockClicked.getX()+2; i++ ){
  169. for ( int j = blockClicked.getY(); j<= 128; j++ ){
  170. for ( int k = (blockClicked.getZ()-2); k<= (blockClicked.getZ()+2); k++ ){
  171. if( server.getBlockIdAt(i, j, k) == 11 || server.getBlockIdAt(i, j, k) == 10){
  172. server.setBlockAt(0,i,j,k);
  173. }
  174. }
  175. }
  176. }
  177. return true;
  178. }
  179. return false;
  180. }
  181. }
  182. }