Adding player to an Array

Discussion in 'Plugin Development' started by Treeline1, Jan 13, 2015.

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

    Treeline1

    So at the moment, when someone chooses the hopper kit (/hopper)when they right click a slimeball they are able to jump! Although at the moment it allows everyone to be able to jump with it and I am not sure why D: Can anyone see whats wrong with it?
    Code:
    package me.liam.commands;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import me.liam.main.Kitpvp;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.util.Vector;
    
    public class HopperCommand implements CommandExecutor, Listener {
    
        List<String> hopper = new ArrayList<String>();
        ArrayList<String> playersOnCooldown    = new ArrayList<String>();
    
        private Kitpvp plugin;
    
        public HopperCommand(Kitpvp instance)
        {
            this.plugin = instance;
        }
    
    
    
        @EventHandler
        public void onRightClick(PlayerInteractEvent e) {
            final Player p = e.getPlayer();
            if (!(this.hopper.contains(p.getName()))) {
    
                if (e.getPlayer().getItemInHand().getTypeId() == (341)) {
                    if(playersOnCooldown.contains(p.getName())) {
                        p.sendMessage(ChatColor.RED + "You cannot use that yet!");
                    }else
    
                        if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                            if (e.getPlayer().getItemInHand().getTypeId() == (341)) {
                                p.setVelocity(p.getLocation().getDirection().normalize().add(new Vector(0f, 2f, 0f).multiply(0.7)));
                                playersOnCooldown.add(p.getName());
                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                                    public void run() {
                                        playersOnCooldown.remove(p.getName());
                                    }
                                }, 5 * 20);
                                return;
                            }
    
                        }
                }
            }
        }
    
    
    
    
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(!(sender instanceof Player)){
                return false;
            }
    
            Player player = (Player)sender;
    
            if(cmd.getName().equalsIgnoreCase("hopper")){
                if(!(this.plugin.HopperCommand.contains(player.getName()))){
                    if(!(this.plugin.kitused.contains(player.getName()))){
                        this.plugin.HopperCommand.add(player.getName());
                        this.plugin.kitused.add(player.getName());   
                        this.hopper.add(sender.getName());
    
                        clearInventory(player.getInventory());
    
                        ItemStack sword = new ItemStack(Material.IRON_SWORD, 1);
                        sword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
    
                        ItemStack helmet = new ItemStack(Material.IRON_HELMET, 1);
                        helmet.addEnchantment(Enchantment.DURABILITY, 3);
                        ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE, 1);
                        chestplate.addEnchantment(Enchantment.DURABILITY, 3);
                        ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS, 1);
                        leggings.addEnchantment(Enchantment.DURABILITY, 3);
                        ItemStack boots = new ItemStack(Material.IRON_BOOTS, 1);
                        boots.addEnchantment(Enchantment.DURABILITY, 3);
    
                        player.getInventory().setHelmet(helmet);
                        player.getInventory().setChestplate(chestplate);
                        player.getInventory().setLeggings(leggings);
                        player.getInventory().setBoots(boots);
                        player.getInventory().addItem(sword);
                        player.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP, 32));
    
    
                        player.sendMessage(ChatColor.RED + "You have chosen the Hopper Kit!");
                        return true;
    
                    }
                }
            }
    
            return true;
        }
        public void clearInventory(PlayerInventory inv){
            inv.clear();
            inv.setHelmet(null);
            inv.setChestplate(null);
            inv.setLeggings(null);
            inv.setBoots(null);
        }
    
    }
    
     
  2. Offline

    MinecraftMart

    You mean everyone who has a slimeball in their hand? Because if you do /hopper you get added to the hopper list.
    And it checks if you are NOT in the list. So basicly everyone who didnt do the command can use it.

    @Treeline1
     
  3. Offline

    Treeline1

    Whoops that was a small mistake. Now I have removed the if(!(... but not even if you are in the hopper list, it doesnt let you or anyone use the ball anymore

    Code:
        @EventHandler
        public void onRightClick(PlayerInteractEvent e) {
            final Player p = e.getPlayer();
            if (this.hopper.contains(p.getName())) {
    
                if (e.getPlayer().getItemInHand().getTypeId() == (341)) {
                    if(playersOnCooldown.contains(p.getName())) {
                        p.sendMessage(ChatColor.RED + "You cannot use that yet!");
                    }else
    
                        if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
                            if (e.getPlayer().getItemInHand().getTypeId() == (341)) {
                                p.setVelocity(p.getLocation().getDirection().normalize().add(new Vector(0f, 2f, 0f).multiply(0.7)));
                                playersOnCooldown.add(p.getName());
                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                                    public void run() {
                                        playersOnCooldown.remove(p.getName());
                                    }
                                }, 5 * 20);
                                return;
                            }
    
                        }
                }
            }
        }
    
     
  4. Offline

    MinecraftMart

    You should try logging. Maybe see where it stops with running the code. If you have done that i could helpm you further because i cant find anything real quick.
     
  5. Offline

    Krizeh

    Using your code:
    Code:
    @EventHandler
    public void onClick(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if (p.getItemInHand().getType().equals(Material.SLIME_BALL) {
    if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
    if (this.hopper.contains(p.getName())) {
    e.setCancelled(true);
    if (this.playersOnCooldown.contains(p.getName()) {
    p.sendMessage(ChatColor.RED + "You are still on cooldown!");
    } else {
    p.setVelocity(p.getLocation().getDirection().normalize().add(new Vector(0f, 2f, 0f).multiply(0.7)));
    playersOnCooldown.add(p.getName());
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                                    public void run() {
                                        playersOnCooldown.remove(p.getName());
                                    }
                                }, 5 * 20);
    }
    It's not tested but it should work.
     
  6. Offline

    Treeline1

    Nope that does let anyone use it :/
     
  7. Offline

    mythbusterma

    @Krizeh

    Please, stop posting.


    @Treeline1

    Try debugging and printing the contents of that List, and try a little something called "Object Oriented Programming," your code is very difficult to decipher, and adds the player to a lot of random lists, not one of which seems to be the one you're checking.

    Also, you don't need to schedule a removal, use lazy data and just add the information to HashMap, the player and the time that they're allowed to use the item again, and then check that when they try to use it.
     
  8. Offline

    Treeline1

    I have object orienting in other parts of my plugin. To keep it simple I decided for my kits I would just make the events in the kits class itself.
     
  9. Offline

    mythbusterma

    @Treeline1

    That's not at all what I meant, I meant stop using public or protected members to access everything. Also, I told you what your issue was.
     
  10. Offline

    iReD_xBlaDeZ

    @Treeline1 treeline? your learning to code? :eek: didnt know that :p (i know you from mcpvp :p)
     
  11. Offline

    Treeline1

    @mythbusterma

    What lists I arnt checking?
    I still cant figure it out :/
     
  12. Offline

    reider45

    Please, debug the plugin to find out where the code doesn't work as intended.
     
  13. Offline

    Treeline1

    I've debugged it and it isnt adding me to the ArrayList. So then because it isnt adding me to the list, it then doesn't let me use the EventHandler...

    *EDIT* Nevermind everyone! Figured it out. Had to make the array static ;)
     
  14. Offline

    teej107

     
    Konato_K likes this.
  15. Offline

    Konato_K

    @Treeline1 I think you don't get the point of static :/
     
Thread Status:
Not open for further replies.

Share This Page