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

  ViewVC Help
Powered by ViewVC 1.1.20