Player Hider

Discussion in 'Spigot Plugin Help' started by YumaFX, Jan 10, 2020.

  1. Hello People,

    so i want to make a Player Hide tool and im trying like 4 hours now to do so but somehow im not able to do it the way i want...
    So i want to make a Item to Hide the Players and after hiding them i want the item to be replaced with another item used to show them again for example i have a stick that says "hide players" and when i rightclick it it hides the players and get changed into a blazerod that says "show players" and if you rightclick it it should change back to a stick and show all players again AND when all players are hidden and a new player joins the server he should be hidden aswell
     

  2. public class PlayerHider implements Listener {

    ArrayList<String> HideShow = new ArrayList<>();

    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();

    if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    if(e.getItem().getType() == Material.BLAZE_ROD) {
    if(HideShow.contains(p.getName())) {
    HideShow.remove(p.getName());
    for(Player all : Bukkit.getOnlinePlayers()) {
    p.showPlayer(all);
    }
    p.sendMessage("§7Alle Spieler sind nun §cUnsichtbar§7!");
    p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 1);
    }else{
    HideShow.add(p.getName());
    for(Player all : Bukkit.getOnlinePlayers()) {
    p.hidePlayer(all);
    }
    p.sendMessage("§7Alle Spieler sind nun wieder §aSichtbar§7!");
    p.playSound(p.getLocation(), Sound.ENDERDRAGON_GROWL, 1, 1);
    }
    }
    }
    }
    }

    This is my Code and it works fine but when i hide the player and a new one joins he is visible and also i have no clue how to change the items :/
     
  3. Hmmm, idk how the HideShow method you created work.. but... Create a new Listener, "PlayerJoinEvent" and then when a new player joins, test for each player on the server, and if that player is "HideShow HidePlayers" then hide the new player from that player
    Something like this
    Code (Text):

    @EventHandler
    public static void playerJoin(PlayerJoinEvent e){
    for(Player p : Bukkit.getOnlinePlayers()){
    if(ShowHide.contains(p.getName()){
    p.hidePlayer(e.getPlayer());
    }
    }
    }
     
    And for the item, then...
    Code (Text):

    e.getItem().setType(Material.STICK);
     
     

  4. Alright it finally worked thx for the help :)
     
  5. Strahan

    Benefactor

    What he said, but don't make the listener's PlayerJoinEvent method static. You'll also want to be sure your collection is visible to both classes. You should be declaring that List as List<String> not ArrayList<String> if you want to keep to SOLID design principles. You also may want to throw in some logic to whittle down the functionality, because as you have it written anyone with any old blaze rod can do this. I'd also use negative check and return to save some indentation and lastly, when posting code here use [CODE] blocks so our eyes don't bleed trying to read that lol