Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.googlemail.rpaliwoda.motd;
- import java.util.logging.Logger;
- import java.io.*;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerLoginEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- // Have a node for motd.admins, that allow a user with that permission
- // to modify the motd: /motd New message!
- // Write and read the motd from a text file in the plugin directory.
- public class MotD extends JavaPlugin implements Listener
- {
- Logger log;
- String motd = "";
- File f;
- public void onEnable()
- {
- log = this.getLogger();
- getServer().getPluginManager().registerEvents(this, this);
- log.info("MotD plugin has been enabled.");
- f = new File("motd.txt");
- // read motd from file
- // stuff motd in a String variable
- motd = getMotD();
- }
- public void onDisable()
- {
- // write motd to file
- log.info("MotD plugin has been disabled.");
- }
- @EventHandler // EventPriority.NORMAL by default
- public void onPlayerLogin(PlayerLoginEvent event)
- {
- // Display a message on player login.
- // display motd String retrieved earlier
- Player player = event.getPlayer();
- player.sendMessage("MotD: " + motd);
- log.info(player.getName() + " logged in; MotD sent");
- // okay, so the player isn't actually getting the MotD.
- // possibilities include this failing, or string motd not being set to anything at the time
- // it seems this is what's failing and not doing anything, since log.info() isn't doing a thing.
- }
- public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
- {
- Player player = null;
- if (sender instanceof Player)
- {
- player = (Player) sender;
- }
- if(cmd.getName().equalsIgnoreCase("motd"))
- {
- if(player != null)
- {
- if(player.hasPermission("motd.admins"))
- {
- setMotD(args);
- // send message to player informing success - make setMotD return boolean?
- // should it inform currently-online players of changed MotD? Probably.
- }
- else
- {
- // error message re: permissions
- }
- }
- else
- {
- setMotD(args);
- }
- return true;
- }
- return false;
- }
- public String getMotD()
- {
- String temp = "";
- BufferedReader br = null;
- //File f = new File("motd.txt");
- if(!f.exists())
- {
- try
- {
- f.createNewFile();
- log.info("File motd.txt not detected; new motd.txt created in server folder.");
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- try
- {
- br = new BufferedReader(new FileReader("motd.txt"));
- temp = br.readLine();
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if (br != null)
- br.close();
- }
- catch (IOException ex)
- {
- ex.printStackTrace();
- }
- }
- return temp;
- }
- public void setMotD(String[] args)
- {
- // accept new motd
- for(int i = 0; i < args.length; i++)
- {
- if(i != 0)
- motd = motd + " " + args[i];
- else
- motd = args[i];
- }
- try
- {
- BufferedWriter bw = new BufferedWriter(new FileWriter(f,false));
- bw.write(motd);
- bw.close();
- log.info("New MotD set: " + motd); // show confirmation of new motd
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment