Plugin Help hasPLayedBefore() [SOLVED]

Discussion in 'Plugin Help/Development/Requests' started by liltattertot97, Dec 12, 2014.

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

    liltattertot97

    So i'm making a plugin that puts a protection on people when they first join. They have to type a command to remove this protection. My problem is when the players leave the game and rejoin they get the protection put back on them. I made two lists of UUID's to keep track of who removed the protection and who hasn't. I have some knowledge of Java. I took a class at my highschool that teaches java basics. Any help is appreciated

    Could it maybe be because im not saving the arraylists or something ?

    Code: http://pastie.org/9777588
     
  2. Offline

    teej107

    @liltattertot97 Here are some tips to reduce lines of code (and might fix your problem)
    • Unless you need to use the get() method for List, use a Set. Set doesn't allow duplicates so there is no need to check to see if the collection contains an object.
    • Instead of creating two Collections, just assume that every player that is not in the collection has the protection turned off. This will eliminate the need for 2 collections.
     
  3. Offline

    liltattertot97

    @teej107 Sorry if this is noobish but what do you mean by use a set and not get()?
     
  4. Offline

    HeadGam3z

  5. Offline

    liltattertot97

    ohh ok i think i undertand.. He's saying i should change my lists to a set. ok ok ill try that.

    But will that solve the problem of saving the players to the set when they log off?
     
  6. Offline

    teej107

    @liltattertot97 Let me re-explain. You are using a List. If you are not using the List#get() method, use a Set instead. Set does not allow duplicates. Using a Set will eliminate your need to call the Collection#contains() method before adding an object to the Collection.

    Try the below and see if it solves your problem

     
  7. Offline

    liltattertot97

    @teej107
    So i changed the list to a set and removed the second list but it still adds them back when they rejoin causing the player to have to type the command again. Now it could be something with my code but im not to sure.

    Thanks for the replies tho!



    Code:
    public class NewbTimer extends JavaPlugin implements Listener {
       
       
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            Bukkit.getServer().getLogger().info("Newb timer" + this.getDescription().getVersion() + "has been enabled");
        }
       
        Set<UUID> np = new HashSet<UUID>();
       
        @EventHandler
        public void PickupItem(PlayerPickupItemEvent event) {
                UUID uuid = event.getPlayer().getUniqueId();
                Player p = event.getPlayer();
                if(np.contains(uuid)){
                    p.sendMessage(ChatColor.RED + "Type '/newbtimerremove' to pick that up");
                    event.setCancelled(true);
                }
        }
       
        @EventHandler
        public void onPlayerLogin(final PlayerLoginEvent e) {
            Player p = e.getPlayer();
            UUID u = e.getPlayer().getUniqueId();
            if(!p.hasPlayedBefore()){
                np.add(u);
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            UUID uuid = ((Player) sender).getUniqueId();
            Player player = Bukkit.getPlayer(uuid);
            if(cmd.getName().equalsIgnoreCase("newbtimerremove")){
                    np.remove(uuid);
                    player.sendMessage("You are no longer protected");
                    return true;
                }else{
                    player.sendMessage("You are not on the List");
                    return true;
                }
        }
           
    }
     
  8. Offline

    liltattertot97

    @teej107 Thanks for the help!! I just needed to change PlayerLoginEvent to PlayerJoinEvent and that fixed the probelms
     
Thread Status:
Not open for further replies.

Share This Page