Trying to get armor stand with custom name

Discussion in 'Plugin Development' started by HerohollandYT, May 5, 2020.

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

    HerohollandYT

    Hello I have been working on a plugin where when players run the command it will create an armorstand where they were when they ran the command and puts the player into gamemode spectator. The way it does this is by getting the armorstand as but it is deleting the incorrect armor stand on disable. I have tried getting the armor stand with the players name too with a for loop with all the entities and making a hashmap where it stores the players name and UUID, but they keep giving a java.lang.NullPointerException for the teleport part before it is called. Here is my code for the class:
    Code:
    public class CommandHandler implements CommandExecutor {
        /*
          static String msg1 = main.plugin.getConfig().getString("messages.commands-disabled-message");
          msg1 = ChatColor.translateAlternateColorCodes('&', msg1);
         */
         static String msg1 = main.plugin.getConfig().getString("messages.fly-disabled-message");
         static String msg2 = main.plugin.getConfig().getString("messages.hotbar-notclear-message");
         static String msg3 = main.plugin.getConfig().getString("messages.noperms-message");
         static String msg4 = main.plugin.getConfig().getString("messages.reloaded-config-message");
         static String msg5 = main.plugin.getConfig().getString("messages.roam-enabled-message");
         static String msg6 = main.plugin.getConfig().getString("messages.roam-exit-message");
    static String st = main.getStarter();
    static ArmorStand as;
    static int cooltime = main.plugin.getConfig().getInt("options.cooldown-time") * 1000;
    static HashMap<Player, Long> cooldown = new HashMap<Player, Long>();
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (args.length == 0) {
                if (player.hasPermission(main.plugin.getConfig().getString("permissions.main-use"))) {
                    if (main.getMode(player) == false) {
                    if (player.getInventory().getItem(0) == null && player.getInventory().getItem(1) == null && player.getInventory().getItem(2) == null && player.getInventory().getItem(3) == null && player.getInventory().getItem(4) == null && player.getInventory().getItem(5) == null && player.getInventory().getItem(6) == null && player.getInventory().getItem(7) == null && player.getInventory().getItem(8) == null) {
                if (!player.isFlying() || player.getGameMode().equals(GameMode.SPECTATOR)) {
                    if (player.hasPermission(main.plugin.getConfig().getString("permissions.bypass-cooldown")) || cooldown.containsKey(player) && cooldown.get(player) <= System.currentTimeMillis()) {
                        setRoamMode(player);
                    } else if (cooldown.containsKey(player)){
                        long timeRemaining = cooldown.get(player) - System.currentTimeMillis();
                        double trs = (double) (timeRemaining / 1000);
                        player.sendMessage(st + ChatColor.DARK_RED + "You can't use that command for " + trs + " seconds!");
                    } else if (!cooldown.containsKey(player)) {
                        cooldown.put(player, System.currentTimeMillis() + cooltime);
                        setRoamMode(player);
                        if (!player.hasPermission(main.plugin.getConfig().getString("permissions.bypass-distance"))) {
                            if (main.containsRoamer(player.getName())) {
                        Distance.trackDistance(player, as);
                            }
                    }
                    }
                } else {
                    msg1 = ChatColor.translateAlternateColorCodes('&', msg1);
                    player.sendMessage(msg1);
                }
                    } else {
                        msg2 = ChatColor.translateAlternateColorCodes('&', msg2);
                        player.sendMessage(msg2);
                    }
                    } else {
                        disableRoamMode(player);
                    }
                } else {
                    msg3 = ChatColor.translateAlternateColorCodes('&', msg3);
                    player.sendMessage(msg3);
                }
            }
            if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
                main.plugin.reloadConfig();
                msg4 = ChatColor.translateAlternateColorCodes('&', msg4);
                player.sendMessage(msg4);
            }
            return false;
        }
    public static void setRoamMode(Player player) {
        if (player.hasPermission(main.plugin.getConfig().getString("permissions.bypass-distance"))) {
            Bukkit.getScheduler().cancelTask(Distance.getCounter());
        } else {
            Bukkit.getScheduler().cancelTask(Distance.getCounter());
            if (main.containsRoamer(player.getName())) {
        Distance.trackDistance(player, as);
            }
        }
        main.addRoamer(player.getName());
        msg5 = ChatColor.translateAlternateColorCodes('&', msg5);
        player.sendMessage(msg5);
        main.setMode("true", player);
        player.setGameMode(GameMode.SPECTATOR);
        if (!player.hasPermission(main.plugin.getConfig().getString("permissions.bypass-cooldown"))) {
        cooldown.put(player, System.currentTimeMillis() + cooltime);
        }
        as = (ArmorStand) player.getWorld().spawnEntity(player.getLocation(), EntityType.ARMOR_STAND);
        as.setGravity(main.plugin.getConfig().getBoolean("options.armorstand.gravity"));
        as.setHelmet(new ItemStack(Material.getMaterial(main.plugin.getConfig().getString("options.armorstand.helmet"))));
        as.setChestplate(new ItemStack(Material.getMaterial(main.plugin.getConfig().getString("options.armorstand.chestplate"))));
        as.setLeggings(new ItemStack(Material.getMaterial(main.plugin.getConfig().getString("options.armorstand.leggings"))));
        as.setBoots(new ItemStack(Material.getMaterial(main.plugin.getConfig().getString("options.armorstand.boots"))));
        as.setCustomName(player.getName());
        as.setArms(main.plugin.getConfig().getBoolean("options.armorstand.arms"));
        as.setCanPickupItems(main.plugin.getConfig().getBoolean("options.armorstand.canpickupitems"));
        as.setCustomNameVisible(main.plugin.getConfig().getBoolean("options.armorstand.namevisible"));
        as.setBasePlate(main.plugin.getConfig().getBoolean("options.armorstand.baseplate"));
    }
    public static void disableRoamMode(Player player) {
        //TODO disable roam mode when they run the command the second time!
        Bukkit.getScheduler().cancelTask(Distance.getCounter());
        main.removeRoamer(player.getName());
        main.setMode("false", player);
        player.setGameMode(GameMode.getByValue(main.plugin.getConfig().getInt("options.default-gamemode")));
        msg6 = ChatColor.translateAlternateColorCodes('&', msg6);
        player.sendMessage(msg6);
        if (main.plugin.getConfig().getBoolean("options.disable.delete-armorstand") == true) {
            player.teleport(as);
            CommandHandler.disableStand();
        } else {
            player.teleport(as);
        }
    }
    public static ArmorStand getStand() {
        return as;
    }
    public static void disableStand() {
        as.setInvulnerable(false);
        as.setHealth(0);
        as.remove();
    }
    }
    I am completely aware that this code is not player specific but I am using it as an example because it was the code that currently did not give me errors but it did teleport players to the latest armorstand rather than their armor stand. I am not sure how to get the armor stand with the players name and teleport them to it or remove it correctly. As you can see the variable for the armor stand is as and it keeps giving me this as the console error when I tried a hashmap:
    Code:
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at org.bukkit.craftbukkit.v1_14_R1.entity.CraftPlayer.teleport(CraftPlayer.java:640) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity.teleport(CraftEntity.java:451) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity.teleport(CraftEntity.java:483) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at com.holland.roam.CommandHandler.disableRoamMode(CommandHandler.java:125) ~[?:?]
    at com.holland.roam.CommandHandler.onCommand(CommandHandler.java:72) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1641) ~[?:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1481) ~[?:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:909) [Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:902) [Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeAll(SourceFile:103) [Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:885) [Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:819) [Spigot-1.14.4.jar:git-Spigot-56f8471-56118c6]
    at java.lang.Thread.run(Thread.java:834) [?:?]
    I have tried searching the forums here but to no success.
     
    Last edited by a moderator: May 5, 2020
  2. Offline

    JRL1004

    @HerohollandYT The armor stand you are referencing in the call to teleport is in the static scope. this means that its value is overridden every time the method to create the stand is called. A side effect of this is that you can only teleport to and delete the most recent armor stand.

    You could make a map to store an armor stand for each player in "roam mode." That would allow you to check if a player is in that mode, and always find their stand if they are. You just need to be sure to remove the player from the map when they exit the mode, and to delete the armor stand at that time if needed.

    Also, the stack trace you provided is no very useful because it does not say what caused the error. The most important part of a java stack trace is the part immediately following the words "Caused by"

    Also, just my two cents, but it's generally bad practice to make so much stuff static. Many of those methods could have been class-level and would have functioned just fine. I know this comment doesn't help with the problem, but I am sure that I am not the only person here who sees the word "static" on the first eleven lines and gets worried.
     
Thread Status:
Not open for further replies.

Share This Page