Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2015
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.74 KB | None | 0 0
  1. package com.busterroni.HelloWorld;
  2.  
  3. //imports
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.command.Command;
  10. import org.bukkit.command.CommandSender;
  11. import org.bukkit.craftbukkit.libs.jline.internal.Log;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.EventHandler;
  14. import org.bukkit.event.Listener;
  15. import org.bukkit.event.player.AsyncPlayerChatEvent;
  16. import org.bukkit.plugin.java.JavaPlugin;
  17.  
  18. public final class Main extends JavaPlugin implements Listener{
  19.  
  20.     //hashmaps for saving whether the player is in a private chat or not
  21.     private HashMap<String, String> chatMap=new HashMap<String, String>();
  22.  
  23.  
  24.     public void onEnable(){
  25.         //check for sent chat messaes
  26.         getServer().getPluginManager().registerEvents(this, this);
  27.     }
  28.     public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
  29.         Player player = (Player) sender;
  30.         if(!(sender instanceof Player)){
  31.             sender.sendMessage(ChatColor.RED + "The console cannot change its chat!");
  32.         }
  33.         else{
  34.             //if the player enters the command 'ac' and has the staffchat.admin permission:
  35.             if(cmd.getName().equalsIgnoreCase("ac") && player.hasPermission("staffchat.admin")){
  36.  
  37.                 if(!chatMap.containsKey(player.getName())){
  38.                     chatMap.put(player.getDisplayName(), "default");
  39.                 }
  40.  
  41.                 //if the player typed in /ac and does not have admin chat enabled
  42.                 if(!chatMap.get(player.getName()).equals("admin")){
  43.                     player.sendMessage(ChatColor.RED + "Admin Chat enabled!");
  44.                     //enable admin chat
  45.                     chatMap.put(player.getDisplayName(), "admin");
  46.                 }
  47.                 //if the player typed in /ac and does have admin chat enabled
  48.                 else{
  49.                     player.sendMessage(ChatColor.RED + "Admin Chat disabled!");
  50.                     //disable admin chat
  51.                     chatMap.put(player.getName(), "default");
  52.                 }
  53.             }
  54.             else if(cmd.getName().equalsIgnoreCase("ac") && !player.hasPermission("staffchat.admin")){
  55.                 player.sendMessage(ChatColor.RED + "You do not have permission to use the admin chat!");
  56.             }
  57.  
  58.             //if the player enters the command 'mc' and has the staffchat.mod permission:
  59.             if(cmd.getName().equalsIgnoreCase("mc") && player.hasPermission("staffchat.mod")){
  60.  
  61.                 if(!chatMap.containsKey(player.getName())){
  62.                     chatMap.put(player.getDisplayName(), "default");
  63.                 }
  64.  
  65.                 //if the player typed in /mc and does not have mod chat enabled
  66.                 if(chatMap.containsKey(player.getName()) && !chatMap.get(player.getName()).equals("mod")){
  67.                     player.sendMessage(ChatColor.DARK_PURPLE + "Mod Chat enabled!");
  68.                     //enable mod chat
  69.                     chatMap.put(player.getDisplayName(), "mod");
  70.                 }
  71.                 //if the player typed in /mc and does have mod chat enabled
  72.                 else{
  73.                     player.sendMessage(ChatColor.DARK_PURPLE + "Mod Chat disabled!");
  74.                     //disable mod chat
  75.                     chatMap.put(player.getDisplayName(), "default");
  76.                 }
  77.             }
  78.             else if(cmd.getName().equalsIgnoreCase("mc") && !player.hasPermission("staffchat.mod")){
  79.                 player.sendMessage(ChatColor.RED + "You do not have permission to use the mod chat!");
  80.             }
  81.  
  82.  
  83.         }
  84.         return false;
  85.     }
  86.  
  87.     @EventHandler
  88.     public void onPlayerChat(AsyncPlayerChatEvent e){
  89.         Date currentDate=new Date();
  90.         SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm:ss");
  91.         Player player = e.getPlayer();
  92.  
  93.         if((chatMap.get(player.getName()).equals("admin") && !player.hasPermission("staffchat.admin")) ||
  94.                 (chatMap.get(player.getName()).equals("mod") && !player.hasPermission("staffchat.mod"))){
  95.             chatMap.remove(player.getName());
  96.         }
  97.  
  98.         //log the sent message
  99.         Log.info(dateFormat.format(currentDate) +" <" + player.getDisplayName() + "> " + e.getMessage());
  100.         //if a player sent a message using private admin chat:
  101.         if(chatMap.get(player.getName()).equals("admin")){
  102.             e.setCancelled(true);
  103.             //run through all the online players
  104.             for(Player p:Bukkit.getOnlinePlayers()){
  105.                 //if the player checked has the staffchat.admin permission:
  106.                 if(p.hasPermission("staffchat.admin")){
  107.                     //send them the private admin message
  108.                         p.sendMessage(ChatColor.RED + "[AC]" + ChatColor.WHITE + " <" + player.getDisplayName() + "> " + ChatColor.AQUA + e.getMessage());
  109.                 }
  110.             }
  111.         }
  112.         //if a player sent a message using private mod chat:
  113.         else if(chatMap.get(player.getDisplayName()).equals("mod")){
  114.             e.setCancelled(true);
  115.             //run through all the online players
  116.             for(Player p:Bukkit.getOnlinePlayers()){
  117.                 //if the player checked has the staffchat.mod permission:
  118.                 if(p.hasPermission("staffchat.mod")){
  119.                         //send them the private mod message
  120.                         p.sendMessage(ChatColor.DARK_PURPLE + "[MC]" + ChatColor.WHITE + " <" + player.getDisplayName() + "> " + ChatColor.LIGHT_PURPLE + e.getMessage());
  121.                 }
  122.             }
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement