Feldherren

Untitled

Apr 7th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. package com.googlemail.rpaliwoda.motd;
  2.  
  3. import java.util.logging.Logger;
  4. import java.io.*;
  5. import org.bukkit.command.Command;
  6. import org.bukkit.command.CommandSender;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.event.EventHandler;
  9. import org.bukkit.event.Listener;
  10. import org.bukkit.event.player.PlayerLoginEvent;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12.  
  13. // Have a node for motd.admins, that allow a user with that permission
  14. // to modify the motd: /motd New message!
  15. // Write and read the motd from a text file in the plugin directory.
  16.  
  17. public class MotD extends JavaPlugin implements Listener
  18. {
  19.     Logger log;
  20.     String motd = "";
  21.     File f;
  22.    
  23.     public void onEnable()
  24.     {
  25.         log = this.getLogger();
  26.         getServer().getPluginManager().registerEvents(this, this);
  27.         log.info("MotD plugin has been enabled.");
  28.         f = new File("motd.txt");
  29.         // read motd from file
  30.         // stuff motd in a String variable
  31.         motd = getMotD();
  32.     }
  33.    
  34.     public void onDisable()
  35.     {
  36.         // write motd to file
  37.         log.info("MotD plugin has been disabled.");
  38.     }
  39.    
  40.     @EventHandler // EventPriority.NORMAL by default
  41.     public void onPlayerLogin(PlayerLoginEvent event)
  42.     {
  43.         // Display a message on player login.
  44.         // display motd String retrieved earlier
  45.         Player player = event.getPlayer();
  46.         player.sendMessage("MotD: " + motd);
  47.         log.info(player.getName() + " logged in; MotD sent");
  48.        
  49.         // okay, so the player isn't actually getting the MotD.
  50.         // possibilities include this failing, or string motd not being set to anything at the time
  51.        
  52.         // it seems this is what's failing and not doing anything, since log.info() isn't doing a thing.
  53.     }
  54.    
  55.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
  56.     {
  57.         Player player = null;
  58.         if (sender instanceof Player)
  59.         {
  60.             player = (Player) sender;
  61.         }
  62.        
  63.         if(cmd.getName().equalsIgnoreCase("motd"))
  64.         {
  65.             if(player != null)
  66.             {
  67.                 if(player.hasPermission("motd.admins"))
  68.                 {
  69.                     setMotD(args);
  70.                     // send message to player informing success - make setMotD return boolean?
  71.                     // should it inform currently-online players of changed MotD? Probably.
  72.                 }
  73.                 else
  74.                 {
  75.                     // error message re: permissions
  76.                 }
  77.             }
  78.             else
  79.             {
  80.                 setMotD(args);
  81.             }
  82.  
  83.             return true;
  84.         }
  85.         return false;
  86.     }
  87.    
  88.     public String getMotD()
  89.     {
  90.         String temp = "";
  91.         BufferedReader br = null;
  92.         //File f = new File("motd.txt");
  93.         if(!f.exists())
  94.         {
  95.             try
  96.             {
  97.                 f.createNewFile();
  98.                 log.info("File motd.txt not detected; new motd.txt created in server folder.");
  99.             }
  100.             catch (IOException e)
  101.             {
  102.                 // TODO Auto-generated catch block
  103.                 e.printStackTrace();
  104.             }
  105.         }
  106.         try
  107.         {
  108.             br = new BufferedReader(new FileReader("motd.txt"));
  109.             temp = br.readLine();
  110.         }
  111.         catch(IOException e)
  112.         {
  113.             e.printStackTrace();
  114.         }
  115.         finally
  116.         {
  117.             try
  118.             {
  119.                 if (br != null)
  120.                     br.close();
  121.             }
  122.             catch (IOException ex)
  123.             {
  124.                 ex.printStackTrace();
  125.             }
  126.         }
  127.         return temp;
  128.     }
  129.    
  130.     public void setMotD(String[] args)
  131.     {
  132.  
  133.         // accept new motd
  134.         for(int i = 0; i < args.length; i++)
  135.         {
  136.             if(i != 0)
  137.                 motd = motd + " " + args[i];
  138.             else
  139.                 motd = args[i];
  140.         }
  141.        
  142.         try
  143.         {
  144.             BufferedWriter bw = new BufferedWriter(new FileWriter(f,false));
  145.             bw.write(motd);
  146.             bw.close();
  147.  
  148.             log.info("New MotD set: " + motd); // show confirmation of new motd
  149.         }
  150.         catch (IOException e)
  151.         {
  152.             // TODO Auto-generated catch block
  153.             e.printStackTrace();
  154.         }
  155.     }
  156.    
  157. }
Advertisement
Add Comment
Please, Sign In to add comment