Solved Proper usage of player.hideplayer(plugin, player);

Discussion in 'Spigot Help' started by johncsuti, Jan 17, 2020.

  1. I need to know how i can use this inside its own plugin what would i put for plugin i have tried "this" "Plugin" and referencing my Stack_Cord.java file. Any help is appreciated!

    Stack_Cord.java file

    Code (Java):
    package com.stacks.stack_cord;

    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.ChatColor;

    import java.util.Objects;

    public final class Stack_Cord extends JavaPlugin {

    @Override
    public void onEnable() {
    getLogger().info(ChatColor.AQUA + "Starting up Stack Cord");
    Objects.requireNonNull(getCommand("god")).setExecutor(new god());
    Objects.requireNonNull(getCommand("vanish")).setExecutor(new vanish());
    }

    @Override
    public void onDisable() {
    getLogger().info(ChatColor.AQUA + "Shutting down Stack Cord");
    }
    }
    vanish.java

    Code (Java):
    package com.stacks.stack_cord;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.metadata.FixedMetadataValue;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    import static org.bukkit.Bukkit.getDefaultGameMode;
    import static org.bukkit.Bukkit.getLogger;

    public class vanish implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (command.getName().equalsIgnoreCase("vanish")) {
                    if (!sender.hasPermission("stacks.vanish")) {
                        sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to do that. stacks.vanish");
                        return true;
                    }
                    if (!player.hasMetadata("vanish")) {
                        for (Player ps : Bukkit.getOnlinePlayers()) {
                            ps.hidePlayer(this, player);
                        }
                        player.setMetadata("vanish", new FixedMetadataValue(this, true));
                        player.sendMessage(ChatColor.AQUA + "You are now in vanish.");
                        player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 1, true, false));
                        if (player.isInvulnerable()) {
                        player.sendMessage(ChatColor.AQUA + "You are no longer in god mode.");
                        player.setInvulnerable(false);
                        player.setAllowFlight(false);
                        player.setFlying(false);
                        player.setSilent(false);
                        }
                    } else {
                        for (Player ps : Bukkit.getOnlinePlayers()) {
                            ps.showPlayer(this, player);
                        }
                        player.removeMetadata("vanish", this);
                        player.sendMessage(ChatColor.AQUA + "You are no longer in vanish.");
                        player.setGameMode(getDefaultGameMode());
                        for (PotionEffect effect : player.getActivePotionEffects())
                            player.removePotionEffect(effect.getType());
                    }
                    return true;

                }
            } else {
                getLogger().info("You are already in spectator mode.");
                return true;
            }
            return true;
        }
    }
     
  2. you need to put an instance of your main class (the class that extends JavaPlugin)
     
  3. Hey thanks for the help! How would I go about doing that again? I'm sorry I'm very new to Java and learn better by being shown examples.
     
  4. There's a few ways to do it. The way I usually do it is by having a static method in the main class that returns an instance of itself:
    Code (Java):
    public class MainClass extends JavaPlugin {
        private static MainClass instance;
        public void onEnable() {
            // set the current instance of our MainClass ("this") to the instance variable. Don't forget this or else instance will be null!
            instance = this;
        }

        public static MainClass getInstance() {
            return instance;
        }
    }
    You can then use MainClass.getInstance() where you need the plugin.

    The next way is passing it through a constructor in your command class:
    Code (Java):
    public class YourCmd implements CommandExecutor {
        private Plugin plugin;
        // your constructor. This code runs when you do new YourCmd(this)
        public YourCmd(Plugin plugin) {
            this.plugin = plugin;
        }
    }
    You'll need to put "this" when you create the instance of the command (new YourCmd(this)), and then you can just use the plugin anywhere in that command class.

    The final way, I don't use very often. Only if I'm feeling lazy or just messing around and experimenting :p
    Wherever you need the plugin, you can do:
    Code (Java):
    MainClass.getPlugin(MainClass.class);
    You seem very new to Java, so I'd recommend looking at some basic Java before delving deep into the Spigot API, especially if what I wrote above is making little sense. Best of luck making plugins!
     
  5. No, it makes perfect sense to me now that I see how it can be implemented!
     
  6. Changed to solved thanks both of you for your help!
     
    • Friendly Friendly x 1