/timecop/src/TimeCop.java

https://github.com/ricin/minecraft-hey0-plugins · Java · 182 lines · 156 code · 26 blank · 0 comment · 53 complexity · 5d8288586a6b485f229ac7428042f9fc 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 TimeCop extends Plugin {
  8. private TimeCopListener listener = new TimeCopListener();
  9. static final Logger log = Logger.getLogger("Minecraft");
  10. static MinecraftServer mcworld = etc.getMCServer();
  11. private Timer TimeCopTicker;
  12. private int TimeCopTickInterval = 40000;
  13. private long timetofreeze = 0;
  14. private int curmode = 0;
  15. private long loopfrom = 0;
  16. private long loopto = 0;
  17. public void enable() {
  18. log.info("[TimeCop] Mod Enabled.");
  19. if (TimeCopTickInterval > 0) {
  20. TimeCopTicker = new Timer();
  21. TimeCopTicker.schedule(new TimeCopTickerTask(), 400, TimeCopTickInterval);
  22. }
  23. }
  24. public void disable() {
  25. log.info("[TimeCop] Mod Disabled");
  26. if (TimeCopTicker != null) {
  27. TimeCopTicker.cancel();
  28. }
  29. }
  30. public void initialize() {
  31. etc.getLoader().addListener(PluginLoader.Hook.COMMAND, listener, this, PluginListener.Priority.MEDIUM);
  32. }
  33. public class TimeCopListener extends PluginListener {
  34. public boolean onCommand(Player player,String[] split) {
  35. if (split[0].equalsIgnoreCase("/enforcetime") && (split.length>1)) {
  36. if(split[1].equalsIgnoreCase("day")) {
  37. player.sendMessage("Daytime enforced!");
  38. timetofreeze = 6000;
  39. etc.getServer().setTime(timetofreeze);
  40. curmode = 1;
  41. } else if(split[1].equalsIgnoreCase("night")) {
  42. player.sendMessage("Nighttime enforced!");
  43. timetofreeze = 18000;
  44. etc.getServer().setTime(timetofreeze);
  45. curmode = 1;
  46. } else {
  47. player.sendMessage("Enforcetime can ony enforce day or night");
  48. }
  49. return true;
  50. } else if (split[0].equalsIgnoreCase("/normaltime")) {
  51. player.sendMessage("Timeflow returned to normal!");
  52. curmode = 0;
  53. return true;
  54. } else if (split[0].equalsIgnoreCase("/freezetime") && (split.length>1)) {
  55. if(converttime(split[1]) != -1) {
  56. timetofreeze = converttime(split[1]);
  57. etc.getServer().setTime(timetofreeze);
  58. curmode = 1;
  59. player.sendMessage("Time Frozen");
  60. } else {
  61. player.sendMessage(split[1] + " is not a valid input");
  62. }
  63. return true;
  64. } else if (split[0].equalsIgnoreCase("/looptime")) {
  65. if(split.length==2) {
  66. if(split[1].equalsIgnoreCase("day")) {
  67. loopfrom = 24000;
  68. loopto = 11000;
  69. etc.getServer().setTime(loopfrom);
  70. curmode = 2;
  71. } else if(split[1].equalsIgnoreCase("night")) {
  72. player.sendMessage("You want to loop night? Not implemented yet!");
  73. } else {
  74. player.sendMessage("You are doing it wrong!");
  75. }
  76. } else if(split.length>2) {
  77. if(converttime(split[1])!=-1 && converttime(split[2])!=-1) {
  78. loopfrom = converttime(split[1]);
  79. loopto = converttime(split[2]);
  80. etc.getServer().setTime(loopfrom);
  81. curmode = 2;
  82. player.sendMessage("Timeloop started");
  83. } else {
  84. player.sendMessage("Get the inputs right. Jerk!");
  85. }
  86. }
  87. return true;
  88. } else if (split[0].equalsIgnoreCase("/gettime")) {
  89. player.sendMessage("The time is " + etc.getServer().getTime());
  90. return true;
  91. }
  92. return false;
  93. }
  94. }
  95. private class TimeCopTickerTask extends TimerTask {
  96. public void run()
  97. {
  98. updateTime();
  99. }
  100. }
  101. private void updateTime() {
  102. switch(curmode) {
  103. case 1:
  104. setthetime(timetofreeze);
  105. break;
  106. case 2:
  107. if(loopfrom>loopto) {
  108. if(getthetime()<loopfrom && getthetime()>loopto) {
  109. setthetime(loopfrom);
  110. }
  111. } else if (loopto>loopfrom) {
  112. if(getthetime()<loopfrom || getthetime()>loopto) {
  113. setthetime(loopfrom);
  114. }
  115. } else {
  116. setthetime(loopfrom);
  117. }
  118. break;
  119. default:
  120. break;
  121. }
  122. return;
  123. }
  124. private long converttime(String input) {
  125. long returnnumber = -1;
  126. try {
  127. returnnumber = Long.parseLong(input.trim());
  128. } catch (NumberFormatException nfe) {
  129. if(input.equalsIgnoreCase("noon") || input.equalsIgnoreCase("day")) {
  130. returnnumber = 6000;
  131. } else if(input.equalsIgnoreCase("midnight") || input.equalsIgnoreCase("night")) {
  132. returnnumber = 18000;
  133. } else if(input.equalsIgnoreCase("dusk")) {
  134. returnnumber = 12500;
  135. } else if(input.equalsIgnoreCase("dawn")) {
  136. returnnumber = 22500;
  137. }
  138. }
  139. if(returnnumber>24000 || returnnumber<0) {
  140. returnnumber = -1;
  141. }
  142. return returnnumber;
  143. }
  144. private void setthetime(long timetoset) {
  145. long curtime = getthetime();
  146. long curtimelong = etc.getServer().getTime();
  147. if(timetoset > curtime) {
  148. etc.getServer().setTime(curtimelong - curtime + timetoset);
  149. } else if (curtime > timetoset) {
  150. etc.getServer().setTime(curtimelong - curtime + timetoset + 24000);
  151. }
  152. }
  153. private long getthetime() {
  154. return (long) etc.getServer().getTime()%24000;
  155. }
  156. }