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

/src/com/zachspong/ChangeGameMode/ChangeGameMode.java

https://bitbucket.org/zachman1221/gamemodechanger
Java | 436 lines | 397 code | 39 blank | 0 comment | 134 complexity | 4dc53af35b6900b8fa33a866bdd0a365 MD5 | raw file
  1. package com.zachspong.ChangeGameMode;
  2. import java.util.logging.Logger;
  3. import org.bukkit.ChatColor;
  4. import org.bukkit.GameMode;
  5. import org.bukkit.command.Command;
  6. import org.bukkit.command.CommandSender;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.permissions.Permissible;
  9. import org.bukkit.permissions.ServerOperator;
  10. import org.bukkit.plugin.Plugin;
  11. import org.bukkit.plugin.PluginDescriptionFile;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13. public class ChangeGameMode extends JavaPlugin {
  14. public int y;
  15. public Player playa;
  16. public Player checkname;
  17. public String matchednames = "";
  18. public int amt = 0;
  19. public String amtp;
  20. public int x;
  21. public String oplayer;
  22. public String namn = ChatColor.GOLD + " ";
  23. public Boolean found;
  24. public Player dude;
  25. public Plugin instance;
  26. static final Logger log = Logger.getLogger("Minecraft");
  27. public final Logger logger = Logger.getLogger("Minecraft");
  28. public void onDisable() {
  29. PluginDescriptionFile pdFile = this.getDescription();
  30. this.logger.info(pdFile.getName() + " version " + pdFile.getVersion() + " is Disabled");
  31. }
  32. public void onEnable() {
  33. PluginDescriptionFile pdFile = this.getDescription();
  34. this.logger.info(pdFile.getName() + " version " + pdFile.getVersion() + " is Enabled");
  35. }
  36. public boolean hasPerms(Player player, String perm) {
  37. String perms;
  38. if(!player.isOp()){
  39. if (!player.hasPermission("gm.*")) {
  40. if ("gm.creative".equals(perm)) {
  41. perms = perm;
  42. if (player.hasPermission(perms)) {
  43. return true;
  44. } else {
  45. NoPerms(player);
  46. return false;
  47. }
  48. }
  49. if ("gm.survival".equals(perm)) {
  50. perms = perm;
  51. if (player.hasPermission(perms)) {
  52. return true;
  53. } else {
  54. NoPerms(player);
  55. return false;
  56. }
  57. }
  58. if ("gm.creative.other".equals(perm)) {
  59. perms = perm;
  60. if (player.hasPermission(perms)) {
  61. return true;
  62. } else {
  63. NoPerms(player);
  64. return false;
  65. }
  66. }
  67. if ("gm.survival.other".equals(perm)) {
  68. perms = perm;
  69. if (player.hasPermission(perms)) {
  70. return true;
  71. } else {
  72. NoPerms(player);
  73. return false;
  74. }
  75. }
  76. if ("gm.toggle".equals(perm)) {
  77. perms = perm;
  78. if (player.hasPermission(perms) || player.hasPermission("gm.toggle.*")) {
  79. return true;
  80. } else {
  81. NoPerms(player);
  82. return false;
  83. }
  84. }
  85. if ("gm.toggle.other".equals(perm)) {
  86. perms = perm;
  87. if (player.hasPermission(perms) || player.hasPermission("gm.toggle.*")) {
  88. return true;
  89. } else {
  90. NoPerms(player);
  91. return false;
  92. }
  93. }
  94. if ("gm.help".equals(perm)) {
  95. perms = perm;
  96. if (player.hasPermission(perms)) {
  97. return true;
  98. } else {
  99. NoPerms(player);
  100. return false;
  101. }
  102. }
  103. NoPerms(player);
  104. return false;
  105. } else
  106. return true;
  107. } else
  108. return true;
  109. }
  110. public void Creative(CommandSender sender, String[] args) {
  111. Player player = (Player) sender;
  112. if (args.length < 1) {
  113. if (hasPerms(player, "gm.creative")) {
  114. player.setGameMode(GameMode.CREATIVE);
  115. sender.sendMessage(namn + "You changed your game mode to " + ChatColor.GREEN + "Creative" + ChatColor.GOLD + "!");
  116. log.info("[GameMode] " + player.getDisplayName() + " changed game mode to Creative!");
  117. }
  118. } else if (args.length == 1) {
  119. if (hasPerms(player, "gm.creative.other")) {
  120. if (isOnline(sender, args)) {
  121. dude.setGameMode(GameMode.CREATIVE);
  122. sender.sendMessage(namn + "You changed " + ChatColor.DARK_RED + dude.getDisplayName() + ChatColor.GOLD + "'s game mode to " + ChatColor.GREEN + "Creative" + ChatColor.GOLD + "!");
  123. dude.sendMessage(namn + ChatColor.DARK_RED + player.getDisplayName() + ChatColor.GOLD + " changed your game mode to " + ChatColor.GREEN + "Creative" + ChatColor.GOLD + "!");
  124. log.info("[GameMode] " + player.getDisplayName() + " changed " + dude.getDisplayName() + "'s game mode to Creative!");
  125. }
  126. }
  127. }
  128. }
  129. public boolean isOnline(CommandSender sender, String[] args) {
  130. Player list[] = sender.getServer().getOnlinePlayers();
  131. found = false;
  132. matchednames = "";
  133. checkname = null;
  134. amt = 0;
  135. if ("t".equals(args[0]) || "toggle".equals(args[0])) {
  136. x = 1;
  137. } else {
  138. x = 0;
  139. }
  140. for (Player checkname : list) {
  141. if (checkname.getDisplayName().toLowerCase().contains(args[x].toLowerCase())) {
  142. amt++;
  143. playa = checkname;
  144. matchednames += ChatColor.DARK_RED + checkname.getDisplayName() + ChatColor.GOLD + ", ";
  145. }
  146. }
  147. if (amt == 1) {
  148. dude = playa;
  149. found = true;
  150. }
  151. if (found) {
  152. return true;
  153. } else if (!found && amt == 0) {
  154. sender.sendMessage(namn + "Unable to find player " + ChatColor.DARK_RED + args[x] + ChatColor.GOLD + "!");
  155. return false;
  156. } else if (!found && amt > 1) {
  157. sender.sendMessage(namn + "Too many names matched!");
  158. sender.sendMessage(namn + "String entered: " + ChatColor.DARK_RED + args[x]);
  159. sender.sendMessage(namn + "Names matched: " + matchednames);
  160. return false;
  161. }
  162. return false;
  163. }
  164. public void NoPerms(CommandSender sender) {
  165. y++;
  166. if (y == 1) {
  167. sender.sendMessage(namn + ChatColor.RED + "You don't have permissions to do that!");
  168. }
  169. }
  170. public void Survival(CommandSender sender, String[] args) {
  171. Player player = (Player) sender;
  172. if (args.length < 1) {
  173. if (hasPerms(player, "gm.survival")) {
  174. player.setGameMode(GameMode.SURVIVAL);
  175. sender.sendMessage(namn + "You changed your game mode to " + ChatColor.GREEN + "Survival" + ChatColor.GOLD + "!");
  176. log.info("[GameMode] " + player.getDisplayName() + " changed game mode to Survival!");
  177. }
  178. } else if (args.length == 1) {
  179. if (hasPerms(player, "gm.survival.other")) {
  180. if (isOnline(sender, args)) {
  181. dude.setGameMode(GameMode.SURVIVAL);
  182. sender.sendMessage(namn + "You changed " + ChatColor.DARK_RED + dude.getDisplayName() + ChatColor.GOLD + "'s game mode to " + ChatColor.GREEN + "Survival" + ChatColor.GOLD + "!");
  183. dude.sendMessage(namn + ChatColor.DARK_RED + player.getDisplayName() + ChatColor.GOLD + " changed your game mode to " + ChatColor.GREEN + "Survival" + ChatColor.GOLD + "!");
  184. log.info("[GameMode] " + player.getDisplayName() + " changed " + dude.getDisplayName() + "'s game mode to Survival!");
  185. }
  186. }
  187. }
  188. }
  189. public void Console(CommandSender sender, Command command, String label, String[] args) {
  190. if (label.equalsIgnoreCase("gm") && args.length != 0) {
  191. if (label.equalsIgnoreCase("gm") && !args[0].equalsIgnoreCase("t") && !args[0].equalsIgnoreCase("toggle") && !args[0].equalsIgnoreCase("t") && !args[0].equalsIgnoreCase("h") && !args[0].equalsIgnoreCase("help")) {
  192. log.info("[GameMode] Unknown Command!");
  193. }
  194. }
  195. if (args.length > 2) {
  196. log.info("[GameMode] Too many arguments!");
  197. }
  198. if (args.length < 1) {
  199. sendCHelp();
  200. }
  201. if (args.length > 0 && label.equalsIgnoreCase("gm") && (args[0].equalsIgnoreCase("h") || args[0].equalsIgnoreCase("help"))) {
  202. sendCHelp();
  203. }
  204. if (label.equalsIgnoreCase("gm") && args.length > 0 && (args[0].equalsIgnoreCase("t") || args[0].equalsIgnoreCase("toggle"))) {
  205. if (args.length == 2) {
  206. if (isOnline(sender, args)) {
  207. Consoletoggle(sender, args);
  208. }
  209. }
  210. if (args.length == 1) {
  211. log.info("[GameMode] Too few arguments!");
  212. }
  213. }
  214. if (args.length == 1 && label.equalsIgnoreCase("gms")) {
  215. if (isOnline(sender, args)) {
  216. dude.setGameMode(GameMode.SURVIVAL);
  217. dude.sendMessage(namn + ChatColor.DARK_RED + "Console" + ChatColor.GOLD + " changed your game mode to " + ChatColor.GREEN + "Survival" + ChatColor.GOLD + "!");
  218. log.info("[GameMode] You changed " + dude.getDisplayName() + "'s game mode to Survival");
  219. }
  220. }
  221. if (args.length == 1 && label.equalsIgnoreCase("gmc")) {
  222. if (isOnline(sender, args)) {
  223. dude.setGameMode(GameMode.CREATIVE);
  224. dude.sendMessage(namn + ChatColor.DARK_RED + "Console" + ChatColor.GOLD + " changed your game mode to " + ChatColor.GREEN + "Creative" + ChatColor.GOLD + "!");
  225. log.info("[GameMode] You changed " + dude.getDisplayName() + "'s game mode to Creative");
  226. }
  227. }
  228. }
  229. public void toggle(CommandSender sender) {
  230. Player player = (Player) sender;
  231. if (player.getGameMode().toString().equals("SURVIVAL")) {
  232. player.setGameMode(GameMode.CREATIVE);
  233. sender.sendMessage(namn + "You toggled your game mode to " + ChatColor.GREEN + "Creative" + ChatColor.GOLD + "!");
  234. log.info("[GameMode] " + player.getDisplayName() + " toggled game mode to Creative!");
  235. } else {
  236. player.setGameMode(GameMode.SURVIVAL);
  237. sender.sendMessage(namn + "You toggled your game mode to " + ChatColor.GREEN + "Survival" + ChatColor.GOLD + "!");
  238. log.info("[GameMode] " + player.getDisplayName() + " toggled game mode to Survival!");
  239. }
  240. }
  241. public void toggleother(CommandSender sender, String[] args) {
  242. Player player = (Player) sender;
  243. if (dude.getGameMode().toString().equals("SURVIVAL")) {
  244. dude.setGameMode(GameMode.CREATIVE);
  245. sender.sendMessage(namn + "You changed " + dude.getDisplayName() + "'s game mode to " + ChatColor.GREEN + "Creative" + ChatColor.GOLD + "!");
  246. dude.sendMessage(namn + ChatColor.DARK_RED + player.getDisplayName() + ChatColor.GOLD + " toggled your game mode to " + ChatColor.GREEN + "Creative" + ChatColor.GOLD + "!");
  247. log.info("[GameMode] " + player.getDisplayName() + " toggled " + dude.getDisplayName() + "'s game mode to Creative!");
  248. } else {
  249. dude.setGameMode(GameMode.SURVIVAL);
  250. sender.sendMessage(namn + "You changed " + dude.getDisplayName() + "'s game mode to " + ChatColor.GREEN + "Survival" + ChatColor.GOLD + "!");
  251. dude.sendMessage(namn + ChatColor.DARK_RED + player.getDisplayName() + ChatColor.GOLD + " toggled your game mode to " + ChatColor.GREEN + "Survival" + ChatColor.GOLD + "!");
  252. log.info("[GameMode] " + player.getDisplayName() + " toggled " + dude.getDisplayName() + "'s game mode to Survival!");
  253. }
  254. }
  255. public void sendCHelp() {
  256. String name = "[GameMode] ";
  257. log.info("[GameMode] Help Page");
  258. log.info(name + "gm Commands:");
  259. log.info(name + "/gmc - Change to Creative mode");
  260. log.info(name + "/gms - Change to Survival mode");
  261. log.info(name + "/gmc <name> - Creative mode for <name>");
  262. log.info(name + "/gms <name> - Survival mode for <name>");
  263. log.info(name + "/gm t - Toggles between the two modes");
  264. log.info(name + "/gm t <name> - Toggles between the two modes for <name>");
  265. }
  266. public boolean sendHelp(Player player, String perm) {
  267. String perms;
  268. PluginDescriptionFile pdFile = this.getDescription();
  269. player.sendMessage("Game Mode Changer Help Version: " + ChatColor.RED + pdFile.getVersion() + ChatColor.WHITE + " By: " + ChatColor.RED + "zachman1221");
  270. if(!player.isOp()){
  271. if ("gm.creative".equals(perm)){
  272. player.sendMessage(namn + ChatColor.GREEN + "/gmc " + ChatColor.RED + "-" + ChatColor.GOLD + " Change to Creative mode");
  273. }
  274. if ("gm.survival".equals(perm)) {
  275. player.sendMessage(namn + ChatColor.GREEN + "/gms " + ChatColor.RED + "-" + ChatColor.GOLD + " Change to Survival mode");
  276. }
  277. if ("gm.creative.other".equals(perm)) {
  278. player.sendMessage(namn + ChatColor.GREEN + "/gmc <name> " + ChatColor.RED + "-" + ChatColor.GOLD + " Creative mode for <name>");
  279. }
  280. if ("gm.survival.other".equals(perm)) {
  281. player.sendMessage(namn + ChatColor.GREEN + "/gms <name> " + ChatColor.RED + "-" + ChatColor.GOLD + " Survival mode for <name>");
  282. }
  283. if ("gm.toggle".equals(perm)) {
  284. player.sendMessage(namn + ChatColor.GREEN + "/gm t " + ChatColor.RED + "-" + ChatColor.GOLD + " Toggles between the two modes");
  285. }
  286. if ("gm.toggle.other".equals(perm)) {
  287. player.sendMessage(namn + ChatColor.GREEN + "/gm t <name> " + ChatColor.RED + "-" + ChatColor.GOLD + " Toggles between the two modes for <name>");
  288. }
  289. }
  290. return true;
  291. }
  292. public void Help(CommandSender sender) {
  293. PluginDescriptionFile pdFile = this.getDescription();
  294. sender.sendMessage("Game Mode Changer Version: " + ChatColor.RED + pdFile.getVersion() + ChatColor.WHITE + " By: " + ChatColor.RED + "zachman1221");
  295. Object perm = null;
  296. if(!sender.isOp()){
  297. sender.sendMessage(namn + ChatColor.GREEN + "/gms " + ChatColor.RED + "-" + ChatColor.GOLD + " Change to Survival mode");
  298. sender.sendMessage(namn + ChatColor.GREEN + "/gmc <name> " + ChatColor.RED + "-" + ChatColor.GOLD + " Creative mode for <name>");
  299. sender.sendMessage(namn + ChatColor.GREEN + "/gms <name> " + ChatColor.RED + "-" + ChatColor.GOLD + " Survival mode for <name>");
  300. sender.sendMessage(namn + ChatColor.GREEN + "/gm t " + ChatColor.RED + "-" + ChatColor.GOLD + " Toggles between the two modes");
  301. sender.sendMessage(namn + ChatColor.GREEN + "/gm t <name> " + ChatColor.RED + "-" + ChatColor.GOLD + " Toggles between the two modes for <name>");
  302. sender.sendMessage(namn + ChatColor.GREEN + "/gm c " + ChatColor.RED + "-" + ChatColor.GOLD + " Shows the changelog");
  303. }
  304. else {
  305. if ("gm.creative".equals(perm)){
  306. sender.sendMessage(namn + ChatColor.GREEN + "/gmc " + ChatColor.RED + "-" + ChatColor.GOLD + " Change to Creative mode");
  307. }
  308. if ("gm.survival".equals(perm)) {
  309. sender.sendMessage(namn + ChatColor.GREEN + "/gms " + ChatColor.RED + "-" + ChatColor.GOLD + " Change to Survival mode");
  310. }
  311. if ("gm.creative.other".equals(perm)) {
  312. sender.sendMessage(namn + ChatColor.GREEN + "/gmc <name> " + ChatColor.RED + "-" + ChatColor.GOLD + " Creative mode for <name>");
  313. }
  314. if ("gm.survival.other".equals(perm)) {
  315. sender.sendMessage(namn + ChatColor.GREEN + "/gms <name> " + ChatColor.RED + "-" + ChatColor.GOLD + " Survival mode for <name>");
  316. }
  317. if ("gm.toggle".equals(perm)) {
  318. sender.sendMessage(namn + ChatColor.GREEN + "/gm t " + ChatColor.RED + "-" + ChatColor.GOLD + " Toggles between the two modes");
  319. }
  320. if ("gm.toggle.other".equals(perm)) {
  321. sender.sendMessage(namn + ChatColor.GREEN + "/gm t <name> " + ChatColor.RED + "-" + ChatColor.GOLD + " Toggles between the two modes for <name>");
  322. }
  323. }
  324. }
  325. public void ChangeLog(CommandSender sender) {
  326. PluginDescriptionFile pdFile = this.getDescription();
  327. sender.sendMessage("Game Mode Changer Version: " + ChatColor.RED + pdFile.getVersion() + ChatColor.WHITE + " By: " + ChatColor.RED + "zachman1221");
  328. sender.sendMessage("Changelog");
  329. sender.sendMessage(namn + "(+ Added)(- Removed)");
  330. sender.sendMessage(namn + ChatColor.GREEN + "3.20 " + ChatColor.RED + "-" + ChatColor.GOLD + " +Help only shows to people who have permission");
  331. sender.sendMessage(namn + ChatColor.GREEN + "3.16 " + ChatColor.RED + "-" + ChatColor.GOLD + " +Changlog -permission node help,change,change other");
  332. sender.sendMessage(namn + ChatColor.GREEN + "3.15 " + ChatColor.RED + "-" + ChatColor.GOLD + " +Change a players gamemode + toggle(t) gamemode");
  333. sender.sendMessage(namn + ChatColor.GREEN + "2.1 " + ChatColor.RED + "-" + ChatColor.GOLD + " Changed permission node");
  334. sender.sendMessage(namn + ChatColor.GREEN + "2.0 " + ChatColor.RED + "-" + ChatColor.GOLD + " +Permission Support");
  335. sender.sendMessage(namn + ChatColor.GREEN + "1.0 " + ChatColor.RED + "-" + ChatColor.GOLD + " Initial release!");
  336. }
  337. public void gm(CommandSender sender, String[] args) {
  338. Player player = (Player) sender;
  339. if (args.length == 0 && hasPerms(player, "gm.help")) {
  340. Help(sender);
  341. }
  342. if (args.length == 1) {
  343. if ((args[0].equalsIgnoreCase("t") || args[0].equalsIgnoreCase("toggle")) && hasPerms(player, "gm.toggle")) {
  344. toggle(sender);
  345. }
  346. if ((args[0].equalsIgnoreCase("h") || args[0].equalsIgnoreCase("help"))) {
  347. Help(sender);
  348. }
  349. if ((args[0].equalsIgnoreCase("c") || args[0].equalsIgnoreCase("changelog"))) {
  350. ChangeLog(sender);
  351. }
  352. }
  353. if (args.length == 2 && hasPerms(player, "gm.toggle.other")) {
  354. if (args[0].equalsIgnoreCase("t")) {
  355. if (isOnline(sender, args)) {
  356. toggleother(sender, args);
  357. }
  358. }
  359. }
  360. }
  361. public void Consoletoggle(CommandSender sender, String[] args) {
  362. if (dude.getGameMode().toString().equals("SURVIVAL")) {
  363. dude.setGameMode(GameMode.CREATIVE);
  364. dude.sendMessage(namn + ChatColor.DARK_RED + "Console" + ChatColor.GOLD + " toggled your game mode to " + ChatColor.GREEN + "Creative" + ChatColor.GOLD + "!");
  365. log.info("[GameMode] Console toggled " + dude.getDisplayName() + "'s game mode to Creative!");
  366. } else {
  367. dude.setGameMode(GameMode.SURVIVAL);
  368. dude.sendMessage(namn + ChatColor.DARK_RED + "Console" + ChatColor.GOLD + " toggled your game mode to " + ChatColor.GREEN + "Survival" + ChatColor.GOLD + "!");
  369. log.info("[GameMode] Console toggled " + dude.getDisplayName() + "'s game mode to Survival!");
  370. }
  371. }
  372. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  373. y = 0;
  374. x = 0;
  375. dude = null;
  376. found = false;
  377. if (!(sender instanceof Player)) {
  378. Console(sender, command, label, args);
  379. } else {
  380. if (args.length > 2) {
  381. sender.sendMessage(namn + "Ahhrrgg! Can't handle the amount of arguments!");
  382. } else {
  383. if (label.equalsIgnoreCase("gms")) {
  384. Survival(sender, args);
  385. }
  386. if (label.equalsIgnoreCase("gm")) {
  387. gm(sender, args);
  388. }
  389. if (label.equalsIgnoreCase("gmc")) {
  390. Creative(sender, args);
  391. }
  392. }
  393. return true;
  394. }
  395. return true;
  396. }
  397. }