Vanish Plugin

Discussion in 'Plugin Development' started by DubstepPrins, Nov 20, 2014.

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

    DubstepPrins

    Hey!

    I am trying to code a Vanish plugin to make you invisible but I have a problem,
    so I've put this code:
    Code:java
    1. Player player = (Player) sender;
    2.  
    3. if (cmd.getName().equalsIgnoreCase("vanish")) {
    4. for (PotionEffect effect : player.getActivePotionEffects())
    5. player.removePotionEffect(effect.getType());
    6. sender.sendMessage(ChatColor.AQUA + "Unvanished!");
    7. }else{
    8. if (cmd.getName().equalsIgnoreCase("vanish")) {
    9. Potion potion = new Potion(PotionType.INVISIBILITY);
    10. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000 ,2));
    11. sender.sendMessage(ChatColor.AQUA + "Vanished!");
    12. return true;

    Now I'm wondering how I can make it so if I do /vanish again it will unvanish/vanish me.
    If it's something to do with ArrayLists can you send me a proper tutorial?
    Thanks.
     
  2. Offline

    mine-care

    This is not the only way to vanish, bukkit has methods player.hidePlayer and player.showplayer so essentially you'll need to loop through all the online players and hide the player who performed the command. If thy want to in vanish do the same loop but this time with showPlayer(player object);
     
  3. Offline

    DubstepPrins

    Is it possible to send me a bit of that code? c:
     
  4. Offline

    ItsMattHogan

    Yes, you can use an ArrayList but you should use a HashSet as it's faster and your list of players doesn't need to be ordered.

    Create a HashSet in your class's scope;
    Code:java
    1. private HashSet<Player> invisiblePlayers = new HashSet<>();


    Now, in your vanish command method's scope you need to check if the player is in the set, if they are; remove them and unvanish them, if they aren't; vanish them and add them to the set.
    Code:java
    1. if (invisiblePlayers.contains(player)) {
    2. // Unvanish them
    3. invisiblePlayers.remove(player);
    4. } else {
    5. // Vanish them
    6. invisiblePlayers.add(player);
    7. }


    Bare in mind that this is written for Java 1.8 so you may have to change some things if you're using an older version.
     
  5. Offline

    DubstepPrins

    I tried this method, got this: http://prntscr.com/58p4bp But it doesn't actually vanish me.
    I want it so that if I use /vanish I can still see myself but others can't see me.
    This plugin's too hard for me maybe but I'm trying to get it done.
     
  6. Offline

    Funergy

    DubstepPrins Okay xD.
    When you vanish him use a for loop and loop through all the players then use
    <the key you've defined in your for loop>.hidePlayer(the sender of the command)
    and add him to the HashSet or ArrayList
    to unvanish them you do the same but you use
    <the key you've defined in your for loop>.showPlayer(the sender of the command)
    and remove him from the HashSet or ArrayList

    now you'll only vanish them for the online players but not the players that will join after the command.
    so when you join loop through the players in the HashSet and hide the players.

    But if your HashSet object is a Player you should remove them from the the HashSet/ArrayList because when the hidden player left the Player session is deleted so when someone joins then in will loop through the List of hidden players and tries to hide a player that isn't online. so this will return a null pointer exception.

    Or if you don't want to have this problem, just save players' UUID and everything is fine. so if they join again then they will still get vanished until they do /vanish to unvanish.

    Sorry for this late reply tho.
     
  7. Offline

    DubstepPrins

    Already found how :)
     
  8. Offline

    timtower Moderator Moderator

    Locked per request
     
Thread Status:
Not open for further replies.

Share This Page