SHARE
TWEET

Untitled

a guest Mar 21st, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.jarkyc.janesq;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.event.EventHandler;
  7. import org.bukkit.event.Listener;
  8. import org.bukkit.event.player.AsyncPlayerChatEvent;
  9. import org.bukkit.event.player.PlayerJoinEvent;
  10. import org.bukkit.event.player.PlayerQuitEvent;
  11. import org.bukkit.plugin.RegisteredServiceProvider;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13.  
  14. import net.milkbowl.vault.chat.Chat;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18.  
  19. public class ChatChannels extends JavaPlugin implements Listener {
  20.  
  21.     private Chat chat;
  22.  
  23.     public HashMap<String, ArrayList<String>> channels = new HashMap<String, ArrayList<String>>();
  24.     public ArrayList<String> global = new ArrayList<String>();
  25.  
  26.     @Override
  27.     public void onEnable(){
  28.         getServer().getPluginManager().registerEvents(this, this);
  29.         System.out.println("Jane Online!");
  30.         setupChat();
  31.  
  32.  
  33.         //Add each player online to said list
  34.         for(Player player : Bukkit.getOnlinePlayers()) {
  35.             global.add(player.getDisplayName());
  36.         }
  37.  
  38.         //Put the list of players into channels under global
  39.         channels.put("Global", global);
  40.  
  41.  
  42.     }
  43.  
  44.  
  45.     private boolean setupChat() {
  46.         RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
  47.         chat = rsp.getProvider();
  48.         return chat != null;
  49.     }
  50.  
  51.     @EventHandler
  52.     public void onChat(AsyncPlayerChatEvent event){
  53.         event.setCancelled(true);
  54.  
  55.         System.out.println("a player has sent a message");
  56.  
  57.  
  58.  
  59.         Player player = event.getPlayer();
  60.  
  61.  
  62.         //String aprefix = getPrefix(player);
  63.         String aprefix = chat.getPlayerPrefix(player);
  64.  
  65.         String message = event.getMessage();
  66.         String in = getChannelIn(player);
  67.         String dispname = event.getPlayer().getDisplayName();
  68.  
  69.  
  70.  
  71.         if(in == "Global") {
  72.             Bukkit.broadcastMessage(ChatColor.GRAY + "[" + ChatColor.DARK_GREEN + in + ChatColor.GRAY + "]" + ChatColor.GRAY + "[" + aprefix + "]" + ChatColor.WHITE + dispname + "> " + message);
  73.         } else {
  74.             for(String people : channels.get(in)) {
  75.                 Bukkit.getPlayer(people).sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_GREEN + in  + ChatColor.GRAY + "]" + ChatColor.WHITE + dispname + message );
  76.             }
  77.             event.setCancelled(true);
  78.  
  79.         }
  80.     }
  81.  
  82.  
  83.  
  84.     public String getChannelIn(Player player){
  85.         for(String key : channels.keySet()) {
  86.             if(channels.get(key).contains(player.getDisplayName())){
  87.                 return key;
  88.             }
  89.  
  90.         }
  91.         return "global";
  92.     }
  93.  
  94.     public void setChannelIn(Player player, String channel){
  95.  
  96.         if(channels.containsKey(channel)){
  97.             String AlreadyIn = getChannelIn(player);
  98.             channels.get(AlreadyIn).remove(player.getDisplayName());
  99.             channels.get(channel).add(player.getDisplayName());
  100.             player.sendMessage("You have joined " + channel);
  101.             System.out.println(player + "has joined" + channel);
  102.         }
  103.  
  104.  
  105.     }
  106.  
  107.  
  108.     @EventHandler
  109.     public void onJoin(PlayerJoinEvent join){
  110.         setChannelIn(join.getPlayer(), "global");
  111.         System.out.println("Player joined!");
  112.  
  113.  
  114.     }
  115.     @EventHandler
  116.     public void onLeave(PlayerQuitEvent quit){
  117.         channels.get(getChannelIn(quit.getPlayer()));
  118.  
  119.     }
  120.  
  121. //May be issue?
  122.  /*   public String getPrefix(Player player) {
  123.         String prefix = chat.getPlayerPrefix(player);
  124.         if (prefix != null) {
  125.             return prefix;
  126.         } else {
  127.             return "";
  128.         }
  129.  
  130.     } */
  131. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top