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

/src/main/java/com/keithlawless/plugins/SimpleTips/SimpleTips.java

https://github.com/keithlawless/Tips--Bukkit-Plugin-
Java | 333 lines | 276 code | 34 blank | 23 comment | 59 complexity | 119f68b25f1b9523d1e0f26d25b2a98e MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. * SimpleTips (http://github.com/keithlawless/Tips--Bukkit-Plugin-)
  3. * Copyright (C) 2011 Keith Lawless
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package com.keithlawless.plugins.SimpleTips;
  19. import org.bukkit.Bukkit;
  20. import org.bukkit.command.Command;
  21. import org.bukkit.command.CommandSender;
  22. import org.bukkit.entity.Player;
  23. import org.bukkit.plugin.Plugin;
  24. import org.bukkit.plugin.java.JavaPlugin;
  25. import org.bukkit.scheduler.BukkitScheduler;
  26. import org.bukkit.configuration.file.YamlConfiguration;
  27. import org.bukkit.configuration.ConfigurationSection;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.util.*;
  31. import java.util.logging.Logger;
  32. public class SimpleTips extends JavaPlugin implements Runnable {
  33. private static int MSG_ORDER_SEQ = 0;
  34. private static int MSG_ORDER_RANDOM = 1;
  35. private static String version = "SimpleTips v1.1 by keithlawless";
  36. Logger log = Logger.getLogger("Minecraft");
  37. // Delay is measured in server ticks, which is 1/20th of a second.
  38. private Integer firstMsgDelay = 0;
  39. private Integer nextMsgDelay = 0;
  40. private boolean groupMsgEnabled = false;
  41. private List<String> msgs;
  42. private HashMap<String,List<String>> groupMsgs;
  43. private HashMap<String,Integer> groupMsgsCount;
  44. private int msgCount = 0;
  45. private int currentMsg = 0;
  46. private Random random = new Random();
  47. private File file;
  48. private YamlConfiguration config;
  49. private int msgOrder = MSG_ORDER_SEQ;
  50. public void onDisable() {
  51. log.info(version+" has been disabled.");
  52. }
  53. public void onEnable() {
  54. load();
  55. log.info(version+" has been enabled.");
  56. BukkitScheduler scheduler = this.getServer().getScheduler();
  57. int result = scheduler.scheduleAsyncRepeatingTask( this, this, firstMsgDelay, nextMsgDelay );
  58. if( -1 == result ) {
  59. log.info(version+" Error! Failed to schedule tip display.");
  60. }
  61. else {
  62. log.info(version+" Success! SimpleTips will be displayed on your schedule.");
  63. }
  64. }
  65. public void load() {
  66. // YAML configuration file.
  67. File mainDirectory = new File("plugins"+File.separator+"SimpleTips");
  68. file = new File(mainDirectory.getAbsolutePath()+File.separator+"config.yml");
  69. if(!file.exists()) {
  70. try {
  71. Vector<String> msgs = new Vector<String>();
  72. msgs.add("Put your messages here!");
  73. mainDirectory.mkdirs();
  74. file.createNewFile();
  75. config = new YamlConfiguration();
  76. config.set("firstMsgDelay", (30 * 20));
  77. config.set("nextMsgDelay", (30 * 20));
  78. config.set("msgOrder", "Sequential");
  79. config.set("msgList", msgs);
  80. config.set("groupMsgEnabled", false);
  81. config.save(file);
  82. }
  83. catch(Exception e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. else {
  88. try {
  89. config = YamlConfiguration.loadConfiguration(file);
  90. firstMsgDelay = config.getInt("firstMsgDelay", 0);
  91. nextMsgDelay = config.getInt("nextMsgDelay", 0);
  92. if((config.getString("msgOrder") != null ) && (config.getString("msgOrder").equalsIgnoreCase("Random"))) {
  93. msgOrder = MSG_ORDER_RANDOM;
  94. }
  95. else {
  96. msgOrder = MSG_ORDER_SEQ;
  97. }
  98. groupMsgEnabled = config.getBoolean( "groupMsgEnabled", false );
  99. msgs = config.getStringList("msgList");
  100. if(msgs != null) {
  101. msgCount = msgs.size();
  102. }
  103. else {
  104. msgCount = 0;
  105. msgs = new Vector<String>();
  106. }
  107. if(groupMsgEnabled) {
  108. groupMsgs = new HashMap<String,List<String>>();
  109. ConfigurationSection section = config.getConfigurationSection("groupMsgList");
  110. Set<String> keys = section.getKeys(false);
  111. if( keys != null ) {
  112. for ( String groupName : keys) {
  113. groupMsgs.put(groupName.toLowerCase(), section.getStringList(groupName));
  114. int count = section.getStringList(groupName).size();
  115. groupMsgsCount.put(groupName.toLowerCase(), new Integer(count));
  116. }
  117. }
  118. }
  119. }
  120. catch(Exception e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. }
  125. public void run() {
  126. if(groupMsgEnabled) {
  127. groupMessageDisplay();
  128. }
  129. else {
  130. simpleMessageDisplay();
  131. }
  132. }
  133. private void simpleMessageDisplay() {
  134. if( msgCount > 0 ) {
  135. String msg = ( msgOrder == MSG_ORDER_RANDOM ? msgs.get( random.nextInt( msgCount )) : msgs.get(currentMsg));
  136. this.getServer().broadcastMessage(escape_colors( msg ));
  137. currentMsg++;
  138. if( currentMsg >= msgCount ) {
  139. currentMsg = 0;
  140. }
  141. }
  142. }
  143. private void groupMessageDisplay() {
  144. for( Player player : Bukkit.getOnlinePlayers() ) {
  145. this.getServer().getLogger().warning("Showing messages to " + player.getName());
  146. Set<String> groups = groupMsgs.keySet();
  147. for( String group : groups ) {
  148. String permissionNode = "tip.show."+group;
  149. if( player.hasPermission(permissionNode)) {
  150. List<String> msgList = groupMsgs.get(group.toLowerCase());
  151. if( msgList != null ) {
  152. int c = msgList.size();
  153. if( c > 0 ) {
  154. String msg = msgList.get( random.nextInt( c ));
  155. player.sendMessage(escape_colors(msg));
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
  163. if(!(sender instanceof Player)) {
  164. return false;
  165. }
  166. Player player = (Player)sender;
  167. if(command.getName().equalsIgnoreCase("tip")) {
  168. // player entered just /tip by itself - return a random tip
  169. if( args.length == 0 ) {
  170. if (msgCount > 0) {
  171. int x = random.nextInt(msgCount);
  172. player.sendMessage(escape_colors(msgs.get(x)));
  173. } else {
  174. player.sendMessage("No tips have been defined.");
  175. }
  176. return true;
  177. }
  178. if( args.length == 1 ) {
  179. // player entered /tip list
  180. if( args[0].equalsIgnoreCase("list")) {
  181. if (( !player.hasPermission("tip.list")) && (!sender.isOp())) {
  182. player.sendMessage( "(SimpleTips) You don't have permission to run that command.");
  183. }
  184. else {
  185. for( int x = 0; x < msgCount; x++ ) {
  186. player.sendMessage( "(" + x + ") " + escape_colors(msgs.get(x)));
  187. }
  188. }
  189. return true;
  190. }
  191. }
  192. if( args.length >= 2 ) {
  193. // player entered /tip add [text]
  194. if( args[0].equalsIgnoreCase("add")) {
  195. if (( !player.hasPermission("tip.add")) && (!sender.isOp())) {
  196. player.sendMessage( "(SimpleTips) You don't have permission to run that command.");
  197. }
  198. else {
  199. StringBuffer sb = new StringBuffer();
  200. for( int x = 1; x < args.length; x++ ) {
  201. if( x > 1 ) {
  202. sb.append(" ");
  203. }
  204. sb.append( args[x] );
  205. }
  206. msgs.add(new String(sb));
  207. config.set("msgList", msgs);
  208. try {
  209. config.save(file);
  210. msgCount++;
  211. player.sendMessage("(SimpleTips) Tip has been added.");
  212. }
  213. catch( IOException e ) {
  214. player.sendMessage("(SimpleTips) Error while saving configuration.");
  215. }
  216. }
  217. return true;
  218. }
  219. //player entered /tip del [num]
  220. if( args[0].equalsIgnoreCase("del")) {
  221. if (( !player.hasPermission("tip.del")) && (!sender.isOp())) {
  222. player.sendMessage( "(SimpleTips) You don't have permission to run that command.");
  223. }
  224. else {
  225. try {
  226. int i = Integer.parseInt(args[1]);
  227. msgs.remove(i);
  228. config.set("msgList", msgs);
  229. try {
  230. config.save(file);
  231. msgCount--;
  232. currentMsg = 0; // Reset current message counter on each delete
  233. player.sendMessage("(SimpleTips) Tip has been deleted.");
  234. }
  235. catch( IOException e ) {
  236. player.sendMessage("(SimpleTips) Error while saving configuration.");
  237. }
  238. }
  239. catch(NumberFormatException nfe) {
  240. return false;
  241. }
  242. catch(IndexOutOfBoundsException e) {
  243. player.sendMessage("(SimpleTips) Tip was not found.");
  244. return true;
  245. }
  246. }
  247. return true;
  248. }
  249. if( args[0].equalsIgnoreCase("replace")) {
  250. if (( !player.hasPermission("tip.replace")) && (!sender.isOp())) {
  251. player.sendMessage( "(SimpleTips) You don't have permission to run that command.");
  252. }
  253. else {
  254. if( args.length < 3 ) {
  255. return false;
  256. }
  257. Integer msgIndex = 0;
  258. try {
  259. msgIndex = Integer.parseInt(args[1]);
  260. StringBuffer sb = new StringBuffer();
  261. for( int x = 2; x < args.length; x++ ) {
  262. if( x > 2 ) {
  263. sb.append(" ");
  264. }
  265. sb.append( args[x] );
  266. }
  267. msgs.set(msgIndex, new String(sb));
  268. config.set("msgList", msgs);
  269. try {
  270. config.save(file);
  271. player.sendMessage("(SimpleTips) Tip has been replaced.");
  272. }
  273. catch( IOException e ) {
  274. player.sendMessage("(SimpleTips) Error while saving configuration.");
  275. }
  276. }
  277. catch(NumberFormatException nfe) {
  278. return false;
  279. }
  280. catch(IndexOutOfBoundsException e) {
  281. player.sendMessage("(SimpleTips) Tip was not found.");
  282. return true;
  283. }
  284. }
  285. return true;
  286. }
  287. }
  288. }
  289. return false;
  290. }
  291. private String escape_colors(String input) {
  292. char[] color_codes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  293. String output = new String(input);
  294. for( int x = 0; x < color_codes.length; x++ ) {
  295. output = output.replace( "%"+color_codes[x], "\u00A7"+color_codes[x]);
  296. }
  297. return output;
  298. }
  299. }