/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/MotdHandler.java
ViewVC logotype

Contents of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/MotdHandler.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3201 - (show annotations) (download)
Wed May 31 08:56:00 2017 UTC (6 years, 11 months ago) by torben
File size: 2020 byte(s)
Code cleanup(still doesn't compile)
1 package dk.thoerup.bukkit.hoeruputils;
2
3
4 import java.io.BufferedReader;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.io.InputStreamReader;
9
10 import org.bukkit.ChatColor;
11 import org.bukkit.command.Command;
12 import org.bukkit.command.CommandExecutor;
13 import org.bukkit.command.CommandSender;
14 import org.bukkit.entity.Player;
15 import org.bukkit.event.EventHandler;
16 import org.bukkit.event.Listener;
17 import org.bukkit.event.player.PlayerJoinEvent;
18 import org.bukkit.plugin.Plugin;
19
20 public class MotdHandler implements Listener, CommandExecutor {
21
22 Plugin plugin;
23
24 public MotdHandler(Plugin plugin) {
25 this.plugin = plugin;
26 }
27
28 private String colors(String str) {
29 return ChatColor.translateAlternateColorCodes('&', str );
30 }
31
32 private void readMotdFile(CommandSender player) {
33 File motd = new File(plugin.getDataFolder(), "motd.txt");
34 if (motd.exists()) {
35 try {
36 BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( motd) ) );
37 String line;
38 while ( (line=in.readLine()) != null) {
39 line = line.trim();
40 if (line.startsWith("#")) {
41 continue;
42 }
43 if (line.length() > 0) {
44 line = colors(line);
45 player.sendMessage( line );
46 }
47 }
48 in.close();
49 } catch (IOException e) {
50 plugin.getLogger().severe("Could not read motd.txt:" + e.getMessage() );
51 }
52
53 }
54 }
55
56 @Override
57 public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
58 readMotdFile(sender);
59 return true;
60 }
61
62
63 @EventHandler
64 public void onPlayerLogin(PlayerJoinEvent event) {
65 readMotdFile( event.getPlayer() );
66 //////////////////////////////////////////////////////
67
68
69
70 StringBuilder sb = new StringBuilder();
71 for (Player player : plugin.getServer().getOnlinePlayers() ) {
72 sb.append(", ");
73 sb.append( player.getName() );
74 }
75 String playerlist = ChatColor.YELLOW + "Online players: " + sb.substring(2);
76 event.getPlayer().sendMessage( playerlist );
77
78 }
79
80 }

  ViewVC Help
Powered by ViewVC 1.1.20