Vanish Plugin Help

Discussion in 'Plugin Development' started by rescue_BUTER, Jul 21, 2015.

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

    rescue_BUTER

    Ok, so I have recently started learning Java. I am working on custom coding my server to my best ability and whilst making a vanish plugin, I am pretty sure I have it all correct. The thing is, I can't quite figure out how to give potion effects. Here is my code:

    Code:
                        if (!vanished.contains(p)) {
                                for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                                        pl.hidePlayer(p);
                                }
                                vanished.add(p);
                                p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 100000, 2));
                                // p.sendMessage(ChatColor.AQUA ChatColor.BOLD + "Frost" + ChatColor.GREEN + "Success: " + ChatColor.BLUE + "You have been vanished!");
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', "blah blah blah"));
                                return true;
                        }
                        else {
                                for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                                        pl.showPlayer(p);
                                }
                                vanished.remove(p);
                                p.removePotionEffect(PotionEffectType.INVISIBILITY);
                                // p.sendMessage(ChatColor.GREEN + "Success: " + ChatColor.BLUE + "You have been un-vanished!");
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', "blah blah blah"));
                                return true;
    The potion effect parts I have added in don't work at all. I have tried in the remove vanish part to add in:
    if (p.hasPotionEffect(PotionEffectType.INVISIBILITY)) { and then the remove potion line....But that didn't work either. Any help, if so it is very much appreciated! Thank you.

    Also, the line that has the, I guess I call it the hidden text ( // )...That isn't needed, I know that. I just keep it there in case something happens to go wrong.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  2. Offline

    DoggyCode™

    @rescue_BUTER

    You're doing Player#addPotionEffect(...); wrong. You are doing:
    Code:
    p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 100000, 2));
    
    you have to switch around the ints:
    Code:
    p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 20*1000, 2 -1));
    //p.addPotionEffect(new PotionEffect(PotionEffectType.#TYPE, int ticks, int level));
    
    And I'm doing "20 times 1000" because 1 second ~~ 20 ticks (it's using ticks), so that's basically 1000 seconds. And I'm doing "2 minus 1" because: 0=1, 1=2, 2=3, etc... of course you could have just written 1 instead, but if you'd like to have "2" there I'd use minus and just add a note.

    Np :)
     
    Last edited: Jul 22, 2015
    rescue_BUTER likes this.
  3. Offline

    rescue_BUTER

    Thanks! I'll try it out to make sure it works :p I'm not able to fix it right now.
     
  4. Offline

    rescue_BUTER

    I tried it out, still doesnt work. I have essentials on my server because that's what it is going to have to work with....Is that maybe the reason it isn't giving me the potion effect?
     
  5. Offline

    DoggyCode™

    @rescue_BUTER It should still give you the potion effect... It worked for me.. Can I see your entire updated code?
     
  6. Offline

    webbhead

    Why not just hide the player from other players?
     
  7. Offline

    DoggyCode™

    @webbhead He does that, he just wants the player to have the potion effect as well.
     
  8. Offline

    webbhead

    Oh okay. Sorry, should've looked at the code before commenting. :p
     
    DoggyCode™ likes this.
  9. Offline

    rescue_BUTER

    Alright, my code is pretty simple but here it is:

    Code:
    package me.rescue_BUTTER.vanish;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Vanish extends JavaPlugin implements Listener {
       
        public void onEnable() {
                Bukkit.getServer().getLogger().info("Vanish by rescue_BUTTER has been enabled!");
                Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
      
        private ArrayList<Player> vanished = new ArrayList<Player>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
              
                if (!(sender instanceof Player)) {
                        sender.sendMessage(ChatColor.RED + "You cannot vanish!");
                        return true;
                }
               
                Player p = (Player) sender;
              
                if (cmd.getName().equalsIgnoreCase("vanish")) {
                    if (!(sender.hasPermission("vanish.vanish"))) {
                        sender.sendMessage(ChatColor.DARK_RED + "Error: " + ChatColor.RED + "You do not have permission to execute this command!");
                        return true;
                    }
                       
                      
                        if (!vanished.contains(p)) {
                                for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                                        pl.hidePlayer(p);
                                }
                                vanished.add(p);
                                p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 20*1000, 2 -1));
                                // Doesnt work: p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 20*1000, 2 -1));
                                // p.sendMessage(ChatColor.AQUA ChatColor.BOLD + "Frost" + ChatColor.GREEN + "Success: " + ChatColor.BLUE + "You have been vanished!");
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&b&lFrostPvP " + "&9&l>" + " &aSuccess: " + "&3You have been vanished!"));
                                return true;
                        }
                        else {
                                for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                                        pl.showPlayer(p);
                                }
                                vanished.remove(p);
                                p.removePotionEffect(PotionEffectType.INVISIBILITY);
                                // Doesnt work: p.removePotionEffect(PotionEffectType.INVISIBILITY);
                                // p.sendMessage(ChatColor.GREEN + "Success: " + ChatColor.BLUE + "You have been un-vanished!");
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&b&lFrostPvP " + "&9&l>" + " &aSuccess: " + "&3You have been un-vanished!"));
                                return true;
                        }
                        }
                return true;
        }
      
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
                for (Player p : vanished) {
                        e.getPlayer().hidePlayer(p);
                }
        }
      
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent e) {
                vanished.remove(e.getPlayer());
        }
       
        public void onDisable() {
            Bukkit.getServer().getLogger().info("Vanish by rescue_BUTTER has been disabled!");   
        }
    }
    Thanks guys soo much! I got it to work somehow :p But, I do have 1 more question if you don't mind.....Because I am working with essentials and overriding the /vanish command for it, will people still be able to see the vanished players via TAB & /online?
     
    Last edited by a moderator: Jul 25, 2015
  10. Offline

    webbhead

    I am pretty sure they will still see in /online but I do not think they will see them in TAB.
     
  11. Offline

    rescue_BUTER

    Thanks, do you know if there is anyway to override it with TAB? Maybe an EventHandler for it? You guys are helping me out soo much!
     
  12. Offline

    Agentleader1

    If you continue to have problems reaching your potion effect, it's possible that you've never added them to the array list.
     
  13. Offline

    Monollyth

    You shouldn't use potion effects when vanishing players, it will display particle effects from players, and the player can still be hit. However, bukkit has a method to make the player completely invisible.

    Code:
    player.hidePlayer(playerThatWillBeHidden);
     
  14. Offline

    rescue_BUTER

    Oh, I didn't think of that. Thanks, I will put that into the code. Thanks!

    Acutally, nevermind. My code has that line that you have just posted @Monollyth :p

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jul 26, 2015
Thread Status:
Not open for further replies.

Share This Page