Help with array list messages.

Discussion in 'Plugin Development' started by Mortal_Wombat, Dec 29, 2014.

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

    Mortal_Wombat

    Hi all! I'm mortal and i am coding a plugin and it is giving me some trouble.

    I want them to do the command /profile and it tells them their name (obviously) and then based on what array list they are on it will send them a different second message. At the moment I am unsure if it is a problem with the signs not adding them properly or the way i have it set up to send the messages is done wrong. Any help is appreciated! Thank you all!


    Code:
    package Me.Mortal;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Blood extends JavaPlugin implements Listener {
    
    
    
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    
        public void onDisable() {
            Bukkit.getServer().getLogger().info("Blood Disabled");
    
    
        }
    
    
        ArrayList<Player> half = new ArrayList<Player>();
        ArrayList<Player> pure = new ArrayList<Player>();
        ArrayList<Player> mug = new ArrayList<Player>();
    
    
    
    
    
    
    
    
        @EventHandler
        public void onPureSign(SignChangeEvent e){
    
    
    
            if (e.getLine(0).equalsIgnoreCase("[Pure]")) {
    
                e.setLine(0,  ChatColor.RED + "Click Me?");
    
    
            }
        }
    
        @EventHandler
        public void onMugSign(SignChangeEvent e){
    
    
    
            if (e.getLine(0).equalsIgnoreCase("[Mug]")) {
    
                e.setLine(0,  ChatColor.RED + "No, No!");
                e.setLine(1,  ChatColor.RED + "Click me!");
    
    
            }
        }
        @EventHandler
        public void onHalfSign(SignChangeEvent e){
    
    
    
            if (e.getLine(0).equalsIgnoreCase("[Half]")) {
    
                e.setLine(0,  ChatColor.RED + "Please!");
                e.setLine(2,  ChatColor.RED + "Click Me?");
    
    
            }
        }
    
        @EventHandler
        public void onPureClick(PlayerInteractEvent e) {
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            if (e.getClickedBlock().getState() instanceof Sign) {
                Sign s = (Sign) e.getClickedBlock().getState();
                if (s.getLine(1).equalsIgnoreCase(ChatColor.GREEN + "Click Me?")) {
    
                    pure.add(e.getPlayer());
    
                    mug.remove(e.getPlayer());
    
                    half.remove(e.getPlayer());
    
    
    
                }
            }
        }
    
        @EventHandler
        public void onMugClick(PlayerInteractEvent e) {
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            if (e.getClickedBlock().getState() instanceof Sign) {
                Sign s = (Sign) e.getClickedBlock().getState();
                if (s.getLine(1).equalsIgnoreCase(ChatColor.GREEN + "No, No!")) {
    
                    mug.add(e.getPlayer());
    
                    pure.remove(e.getPlayer());
    
                    half.remove(e.getPlayer());
    
    
    
                }
            }
        }
    
        @EventHandler
        public void onHalfClick(PlayerInteractEvent e) {
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            if (e.getClickedBlock().getState() instanceof Sign) {
                Sign s = (Sign) e.getClickedBlock().getState();
                if (s.getLine(1).equalsIgnoreCase(ChatColor.GREEN + "Please!")) {
    
                    half.add(e.getPlayer());
                   
                    mug.remove(e.getPlayer());
                   
                    pure.remove(e.getPlayer());
    
    
    
                }
            }
        }
    
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
    
    
            Player p = (Player)sender;
    
    
    
    
    
    
            if (cmd.getName().equalsIgnoreCase("profile")) {
    
                p.sendMessage(ChatColor.BLUE + "Name:" +ChatColor.GOLD+ p.getName());
    
                if (half.contains(p)) {
    
                    p.sendMessage(ChatColor.RED + "Blood Status: Half Blood");
    
    
    
                }
    
                if (pure.contains(p)) {
    
                    p.sendMessage(ChatColor.RED + "Blood Status: Pure Blood");
    
    
    
                }
    
                if (mug.contains(p)) {
    
                    p.sendMessage(ChatColor.RED + "Blood Status: Muggle Born");
    
    
    
                }
    
    
            }
    
    
    
            return true;
        }
    
    }
    
     
  2. @Mortal_Wombat (If any help is appreciated), use UUID's in the ArrayList instead of players to prevent memory leaks.
    Try using debug messages to check whether or not the player is being added to the array in the first place.
     
  3. Offline

    mine-care

    Dont cast without checking, read my signature...

    You dont need a seperate event for each sign create, you can add a nested if statement or a switch statement in one event.
    Clicked block can be null and give you a nice null pointer exception.

    And as
    @DJSkepter correctly sugested, a debug will guide you to the error.
     
Thread Status:
Not open for further replies.

Share This Page