PageRenderTime 112ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/herocraftonline/squallseed31/herosneak/HeroSneak.java

https://github.com/SquallSeeD31/HeroSneak
Java | 295 lines | 236 code | 30 blank | 29 comment | 63 complexity | 34c55986ca7f6918ac4bafb1c7485738 MD5 | raw file
  1. package com.herocraftonline.squallseed31.herosneak;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Timer;
  7. import java.util.TimerTask;
  8. import java.util.logging.Logger;
  9. import org.bukkit.command.Command;
  10. import org.bukkit.command.CommandSender;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.event.Event.Priority;
  13. import org.bukkit.event.Event.Type;
  14. import org.bukkit.plugin.Plugin;
  15. import org.bukkit.plugin.PluginDescriptionFile;
  16. import org.bukkit.plugin.PluginManager;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18. import org.bukkit.util.config.Configuration;
  19. import com.nijiko.permissions.PermissionHandler;
  20. import com.nijikokun.bukkit.Permissions.Permissions;
  21. public class HeroSneak extends JavaPlugin
  22. {
  23. protected ArrayList<Player> sneakingPlayers = new ArrayList<Player>();
  24. private HashMap<Player,Long> cooldownTimes = new HashMap<Player,Long>();
  25. //private static Timer refreshTimer = null;
  26. //Plugin variables
  27. private final HeroSneakListener listener = new HeroSneakListener(this);
  28. private PluginDescriptionFile pdfFile;
  29. private String name;
  30. private String version;
  31. private static final Logger log = Logger.getLogger("Minecraft");
  32. private static PermissionHandler Permissions = null;
  33. //Configuration variables
  34. private Configuration config;
  35. private String sneakOnMessage;
  36. private String sneakOffMessage;
  37. private String sneakCooldownMessage;
  38. private String permissionSystem;
  39. private int refreshInterval;
  40. private int sneakDuration;
  41. private int sneakCooldown;
  42. private boolean opsAutoSneak;
  43. private List<String> canAutosneak;
  44. private List<String> canSneak;
  45. //Set debugging true to see debug messages
  46. public static final Boolean debugging = false;
  47. public void onEnable()
  48. {
  49. this.config = getConfiguration();
  50. pdfFile = getDescription();
  51. name = pdfFile.getName();
  52. version = pdfFile.getVersion();
  53. PluginManager pm = getServer().getPluginManager();
  54. pm.registerEvent(Type.PLAYER_JOIN, this.listener, Priority.Monitor, this);
  55. pm.registerEvent(Type.PLAYER_QUIT, this.listener, Priority.Monitor, this);
  56. pm.registerEvent(Type.PLAYER_RESPAWN, this.listener, Priority.Monitor, this);
  57. pm.registerEvent(Type.PLAYER_TELEPORT, this.listener, Priority.Monitor, this);
  58. try {
  59. pm.registerEvent(Type.PLAYER_TOGGLE_SNEAK, this.listener, Priority.Highest, this);
  60. } catch (NoSuchFieldError e) {
  61. log.severe("[" + name + "] PLAYER_TOGGLE_SNEAK unsupported in this version of CraftBukkit! Disabling plugin.");
  62. if (debugging) e.printStackTrace();
  63. pm.disablePlugin(this);
  64. }
  65. //Start config
  66. sneakOnMessage = this.config.getString("messages.sneakOn", "&7You are now sneaking.").replace("&", "\u00A7");
  67. sneakOffMessage = this.config.getString("messages.sneakOff", "&7You are no longer sneaking.").replace("&", "\u00A7");
  68. sneakCooldownMessage = this.config.getString("messages.sneakCooldown", "&4You must wait <time> seconds before you may sneak again.").replace("&", "\u00A7");
  69. permissionSystem = this.config.getString("options.permissions", "permissions");
  70. opsAutoSneak = this.config.getBoolean("options.opsAutoSneak", false);
  71. refreshInterval = this.config.getInt("options.timers.refresh", this.config.getInt("options.refreshInterval", 5));
  72. sneakDuration = this.config.getInt("options.timers.duration", 0);
  73. sneakCooldown = this.config.getInt("options.timers.cooldown", 0);
  74. if (permissionSystem.equalsIgnoreCase("config")) {
  75. canAutosneak = config.getStringList("permissions.autosneak", null);
  76. canSneak = config.getStringList("permissions.sneak", null);
  77. } else if (permissionSystem.equalsIgnoreCase("permissions"))
  78. setupPermissions();
  79. else if (permissionSystem.equalsIgnoreCase("none")) {
  80. //Do nothing; this value is acceptable now.
  81. }
  82. else
  83. permissionSystem = "Ops";
  84. //End config
  85. saveConfig();
  86. setupAutosneak();
  87. setupRefresh();
  88. String strEnable = "[" + name + "] " + version + " enabled.";
  89. log.info(strEnable);
  90. }
  91. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  92. if (!(sender instanceof Player)) return false;
  93. Player player = (Player)sender;
  94. if (!hasPermission(player, "herosneak.sneak"))
  95. return true;
  96. if (args.length > 1)
  97. return false;
  98. if(args.length == 0)
  99. toggleSneak(player);
  100. else {
  101. if (args[0].equalsIgnoreCase("on"))
  102. setSneak(player, true);
  103. else if (args[0].equalsIgnoreCase("off"))
  104. setSneak(player, false);
  105. else
  106. return false;
  107. }
  108. return true;
  109. }
  110. private void setupAutosneak() {
  111. for (Player p : getServer().getOnlinePlayers()) {
  112. if (hasPermission(p, "herosneak.auto")) {
  113. p.setSneaking(true);
  114. sneakingPlayers.add(p);
  115. }
  116. }
  117. }
  118. private void toggleSneak(Player player){
  119. if (sneakingPlayers.contains(player))
  120. setSneak(player, false);
  121. else
  122. setSneak(player, true);
  123. }
  124. private void setSneak(Player player, boolean sneak){
  125. if (sneak) {
  126. if (sneakCooldown > 0 && cooldownTimes.containsKey(player) && cooldownTimes.get(player)>System.currentTimeMillis()) {
  127. player.sendMessage(sneakCooldownMessage.replaceAll("<time>", Integer.toString((int)Math.ceil((cooldownTimes.get(player)-System.currentTimeMillis())/1000))));
  128. return;
  129. }
  130. if (!hasPermission(player, "herosneak.exempt")) {
  131. if (sneakCooldown > 0)
  132. cooldownTimes.put(player, System.currentTimeMillis()+sneakCooldown*1000L);
  133. if (sneakDuration > 0)
  134. getServer().getScheduler().scheduleSyncDelayedTask(this, new SneakCooldown(player), sneakDuration*20L);
  135. }
  136. player.setSneaking(true);
  137. player.sendMessage(sneakOnMessage);
  138. if(!sneakingPlayers.contains(player))
  139. sneakingPlayers.add(player);
  140. } else{
  141. player.setSneaking(false);
  142. player.sendMessage(sneakOffMessage);
  143. if(sneakingPlayers.contains(player))
  144. sneakingPlayers.remove(player);
  145. }
  146. }
  147. private void setupRefresh() {
  148. if (refreshInterval != 0) {
  149. long refreshTicks = refreshInterval*20L;
  150. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
  151. public void run() {
  152. if (!sneakingPlayers.isEmpty()) {
  153. for (Player p : sneakingPlayers) {
  154. HeroSneak.debug("Re-snuck: " + p.getName() + ", are they sneaking? " + p.isSneaking());
  155. p.setSneaking(false);
  156. p.setSneaking(true);
  157. }
  158. }
  159. }
  160. }, refreshTicks, refreshTicks);
  161. }
  162. /*
  163. refreshTimer = new Timer();
  164. refreshTimer.scheduleAtFixedRate(new TimerTask() {
  165. public void run() {
  166. if (!sneakingPlayers.isEmpty()) {
  167. for (Player p : sneakingPlayers) {
  168. p.setSneaking(false);
  169. p.setSneaking(true);
  170. }
  171. }
  172. }
  173. }
  174. , 500L, refreshInterval * 1000L);
  175. */
  176. }
  177. //This method is the default API hook for Permissions
  178. public void setupPermissions() {
  179. Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
  180. if(Permissions == null) {
  181. if(test != null) {
  182. if (!test.isEnabled())
  183. this.getServer().getPluginManager().enablePlugin(test);
  184. Permissions = ((Permissions)test).getHandler();
  185. } else {
  186. log.info("[" + this.name + "]" + " Permission system not enabled. Defaulting to ops only.");
  187. permissionSystem = "Ops";
  188. }
  189. }
  190. }
  191. //Permissions system check
  192. public boolean hasPermission(Player p, String permission) {
  193. debug(p.getName() + " requested permission " + permission);
  194. //Added support for "none" permission system to grant access to all
  195. if (permissionSystem.equalsIgnoreCase("none") && (permission.equalsIgnoreCase("herosneak.sneak") || opsAutoSneak))
  196. return true;
  197. //Ops always win
  198. if (p.isOp() && (!permission.equalsIgnoreCase("herosneak.auto") || opsAutoSneak))
  199. return true;
  200. //If using Nijikokun's Permissions, do a Permissions check
  201. if (permissionSystem.equalsIgnoreCase("permissions") && Permissions.has(p, permission)) {
  202. if ((permission.equalsIgnoreCase("herosneak.auto") || permission.equalsIgnoreCase("herosneak.off")) && Permissions.has(p, "*") && !opsAutoSneak)
  203. return false;
  204. else
  205. return true;
  206. }
  207. //If using config.yml definitions, iterate over the list (for case insensitivity)
  208. if (permissionSystem.equalsIgnoreCase("config")) {
  209. if (permission.equalsIgnoreCase("herosneak.auto")) {
  210. if (canAutosneak != null) {
  211. Iterator<String> it = canAutosneak.iterator();
  212. while (it.hasNext()) {
  213. if (it.next().equalsIgnoreCase(p.getName()))
  214. return true;
  215. }
  216. }
  217. } else if (permission.equalsIgnoreCase("herosneak.sneak")) {
  218. if (canSneak != null) {
  219. Iterator<String> it = canSneak.iterator();
  220. while (it.hasNext()) {
  221. if (it.next().equalsIgnoreCase(p.getName()))
  222. return true;
  223. }
  224. }
  225. }
  226. }
  227. return false;
  228. }
  229. //Creates config.yml if it doesn't exist, initializes with default data
  230. //If config.yml does exist, writes back data that was read
  231. public void saveConfig() {
  232. this.config.setProperty("messages.sneakOn", sneakOnMessage.replace("\u00A7", "&"));
  233. this.config.setProperty("messages.sneakOff", sneakOffMessage.replace("\u00A7", "&"));
  234. this.config.setProperty("messages.sneakCooldown", sneakCooldownMessage.replace("\u00A7", "&"));
  235. this.config.setProperty("options.permissions", this.config.getString("options.permissions", permissionSystem));
  236. this.config.setProperty("options.opsAutoSneak", opsAutoSneak);
  237. this.config.setProperty("options.timers.refresh", refreshInterval);
  238. this.config.removeProperty("options.refreshInterval");
  239. this.config.setProperty("options.timers.duration", sneakDuration);
  240. this.config.setProperty("options.timers.cooldown", sneakCooldown);
  241. this.config.setProperty("permissions.sneak", canSneak);
  242. this.config.setProperty("permissions.autosneak", canAutosneak);
  243. this.config.save();
  244. }
  245. public static void debug(String message) {
  246. if (debugging) {
  247. log.info(message);
  248. }
  249. }
  250. public void onDisable()
  251. {
  252. String strDisable = "[" + name + "] " + version + " disabled.";
  253. log.info(strDisable);
  254. }
  255. public class SneakCooldown implements Runnable {
  256. private Player player;
  257. public SneakCooldown(Player player) {
  258. this.player = player;
  259. }
  260. public void run() {
  261. if (player.isSneaking())
  262. setSneak(player, false);
  263. }
  264. }
  265. }