Clear inventory of OfflinePlayer

Discussion in 'Plugin Development' started by TerZer, Nov 14, 2017.

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

    TerZer

    Title says it all. How should I do that? Searched in google, but couldn't find anything that would work. Please help me in this ;(
     
  2. Offline

    timtower Moderator Moderator

    @TerZer Wait till he logs in, clear it.
     
  3. Offline

    Zombie_Striker

    @TerZer
    You can't. Instead, wait until they rejoin and then clear their inventory.
     
  4. Offline

    TerZer

    @timtower @Zombie_Striker
    Nah guys... What to clear it instantly.. Maybe I should take main world and delete file with player UUID? I saw some post with NBTtags, but that not work anymore..
     
  5. Offline

    timtower Moderator Moderator

    @TerZer Deleting those files removes more than just the inventory.
     
  6. Offline

    TerZer

    @timtower
    What more? His location, enderchest? That's even better for me. :D
     
  7. Offline

    timtower Moderator Moderator

    If you want to delete all data (that the server saves by itself) then you should remove that file.
     
  8. Offline

    Blares

    wouldn't this work!?

    if the command is /clear <player>

    do this:

    Code:
    for (Player p : Bukkit.getServer().getOfflinePlayers()) {
    
         if (p.getName().eqalsIgnoreCase(args[0]) {
          p.clearInventory();
    }
        }
    }
     
  9. Offline

    timtower Moderator Moderator

    @Blares Can't loop over offline players and getting online players out of them.
    Offline players don't have inventories either.
     
  10. Offline

    Blares

    o, you have a point their never tested this just assumed it would work :/
     
  11. When they quit store their UUID to a separate YML file, then in a PlayerLogin check if the file contains their UUID and it it does then remove it, clear their inventory, reset their location, etc...
     
  12. Offline

    Blares

    Yes that's a good idea. Maybe even a player name list?
    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void onJoin(PlayerJoinEvent e) {
    5. Player p = e.getPlayer();
    6. ArrayList<String> names = new ArrayList<>();
    7.  
    8. names.add(p.getName());
    9. }
    10. }
    11.  


    that's how I would store a player in an array list. Be careful doing the wrong one can cause memory leaks. You can do UUID like officerDeveloper said.
     
  13. @Blares ArrayList wouldn't work bc when the plugin reloads or resets the ArrayList would clear.
     
  14. Offline

    JustRendering

    How bout you do a config, set uuid in a config like
    uuid:
    clearonjoin: "yes"
    playerclearonjoinmethod:
    public void clearonjoin(OfflinePlayer player){
    this.getConfig().set(player.getPlayer().getUniqueId().toString() + ".clearonjoin", "yes");
    this.saveConfig();

    }
    then when it joins it does:

    Player player = event.getPlayer();
    String yes = getConfig().getString(player.getUniqueId().toString() + ".clearonjoin");
    if(yes != null){
    if(yes.equals("yes")){
    player.clearInventory();
    this.getConfig().set(player.getUniqueId().toString() + ".clearonjoin", "no");
    this.saveConfig();
    }
    }
     
  15. Offline

    timtower Moderator Moderator

    @OfficerDeveloper @Blares Not to forget the scope.
    This list only exists inside the onJoin, not outsude of it.
    New list every join.
     
    OfficerDeveloper likes this.
  16. Offline

    Blares

    btw if you want to get the invs of offline players you just make an arraylist of an ItemStack[] getting armor contents and inv contents.
     
  17. Offline

    timtower Moderator Moderator

    And how exactly do you want to get the inventory of an offline player?
     
  18. Offline

    Blares

    log the inventory when he leaves?
     
  19. Offline

    timtower Moderator Moderator

    Will not help clearing it though, what was the original issue.
     
  20. Offline

    Blares

    i guess you're right. Tim do you have a way to crack this?
     
  21. Offline

    timtower Moderator Moderator

    Define "crack this"
     
  22. Offline

    Blares

    OH YEAH LIKE YOU SAID:

    Code:
    add inv to array list
    
    and then onJoin
    if (array list contains this guy( CLEAR
     
  23. Offline

    timtower Moderator Moderator

    @Blares 1. Nice caps.
    2. No need to add the inventory to a list though, just the UUID is enough.
     
  24. Offline

    Blares

    Yep i was just in a rush sry dood lock this thread now we have solved it !
     
  25. Offline

    timtower Moderator Moderator

  26. Offline

    Blares

  27. OP as in original poster, the person who first made the post.
     
  28. Offline

    Shxke

    I don't see why ur arguing over this, all you need to do is clear his inventory when he joins. It won't look different from removing it immediately. Unless you want to do something with his inventory while he or she is offline, this will work fine. It is very simple to do, yet people are having a world war here about this. Also, you probably shouldn't argue with a moderator. Tim is much, much more experienced in Java (which is why he is a Bukkit moderator).
     
  29. Offline

    timtower Moderator Moderator

    Arguing with a moderator is fine when it is based on coding and not on policy.
    And being good at Java has nothing to do with why I am moderator.
     
Thread Status:
Not open for further replies.

Share This Page