Solved Team Chat

Discussion in 'Plugin Development' started by killerzz1, Apr 10, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    killerzz1

    Hello,

    I am working on a gamemode for a server and i need it so if a player talks it will send it to the whole server and if they hold a blazerod it will send it to the team that they are in (There are 2 teams).

    But when i export the plugin it is not working here is my code can anyone see why this is happening and is able to help me fix it thanks.

    Code:
    package Utils;
    
    import me.killerzz1.Main.Main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    
    public class Chat implements Listener {
    
       
        //American Chat
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            String pn = p.getName();
            if(p.getInventory().getItemInHand().equals(Material.BLAZE_ROD)){
            if (Main.america.contains(pn)) {
                for (Player p1 : Bukkit.getOnlinePlayers()) {
                    if (Main.america.contains(pn)) {
                        e.setCancelled(true);
                        p1.sendMessage(ChatColor.GREEN + "[Team] " + ChatColor.GRAY + pn + ": " + ChatColor.GRAY + e.getMessage());
                      
                    }else if(!(Main.america.contains(pn))){
                        e.setCancelled(true);
                    }
                }
            }
            }else  if(!(p.getInventory().getItemInHand().equals(Material.BLAZE_ROD))){{
                 for (Player p1 : Bukkit.getOnlinePlayers()) {
                       
                            e.setCancelled(true);
                            p1.sendMessage(ChatColor.GREEN + "[Global] " + ChatColor.GRAY + pn + ": " + ChatColor.GRAY + e.getMessage());
                       
                 }
            }
       
    }
    }
    }
     
  2. Offline

    mine-care

    Please follow java naming conventions.
    Dont abuse static (Ex. lines 23,25,29...)
    This will always be false. Item in hand is ItemStack and cannot be equal to Material.
    Also you dont need a nested if here, because in the first if condition you check if it is a Blazerod and on the else if you check if it is not blaze rod, it is imposible to reach the else with the item being a blazerod so this is dead + inefficient code.

    Same aplys for:
     
  3. Offline

    killerzz1

    Aw 0kay i see so if i change it from Material to ItemStack then it would work?
     
  4. Offline

    mine-care

    @killerzz1 yes but this is inefficient, instead get the type material of the itemstack in hand and compare it with the other material. Also as i said fix the rest! (Statics, Naming Convention, nested if's and so on)
     
  5. Offline

    killerzz1

    Thanks for your help. I have made it work all i done was changed it from if(p.getInventory().getItemInHand().equals(Material.BLAZE_ROD)){
    to
    if (p.getItemInHand().getType() != Material.BLAZE_ROD) {
     
  6. Offline

    mine-care

    @killerzz1 Anytime! :- )
    Umm now that you changed it it is programing-wise correct but logicwise incorrect because the previous statement checked if it IS a blaze rod, whereas the new one checks if it is NOT a blaze rod. That should have inverted your code...
     
  7. Change != to ==
     
Thread Status:
Not open for further replies.

Share This Page