Armor stand bugged

Discussion in 'Plugin Development' started by GKG0, Mar 5, 2021.

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

    GKG0

    So basically I'm working on a plugin rn that can summon an armor stand if stuff on it that follows you, but I'm running into a bug, whenever a player un-logs and re-logs, that player summons an armor stand that's invisible, HOWEVER, other players online CAN see it, so below there are screenshots of what it looks like. The first one if the one how un-logged, and you can see that both armor stands are invisible to the player unless you're in spectator, but on the second screenshot (taken by the person who watched the first player un-log and re-log ) the armor stand is there. I also checked whether or not mods affect it and it doesn't because my friend can see it (they don't use mods)

    upload_2021-3-5_10-58-43.png
    upload_2021-3-5_10-59-31.png
    Here's the code for the listener:
    Code:
    package de.ancash.pets.listeners;
    
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    
    import de.ancash.pets.pets.PlayerPet;
    
    public class JoinListener implements Listener {
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
             new PlayerPet(0, 0, null, null, null, e.getPlayer());
        }
       
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e) {
                PlayerPet.get(e.getPlayer().getUniqueId()).clear();
                PlayerPet.get(e.getPlayer().getUniqueId()).delete();
        }
       
    }
    
    and here's the "PlayerPet" it's referring to:
    Code:
    package de.ancash.pets.pets;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;
    
    import de.ancash.pets.Pets;
    import de.ancash.pets.utils.Chat;
    import de.ancash.pets.utils.Rarity;
    import de.ancash.pets.utils.datastructure.tuples.Duplet;
    import de.ancash.pets.utils.datastructure.tuples.ImmutableTriplet;
    import de.ancash.pets.utils.datastructure.tuples.Tuple;
    import de.ancash.pets.utils.Chat.ChatLevel;
    import de.tr7zw.nbtapi.NBTItem;
    
    public class PlayerPet extends Levelable{
    
        private final ArmorStand armorStand;
        private final ArmorStand nameTag;
        private final UUID playerUUID;
        private final Duplet<UUID, Pet> petInfo;
        private final BukkitRunnable runnable;
        private boolean running = false;
    
        private static HashMap<UUID, PlayerPet> registered = new HashMap<UUID, PlayerPet>();
    
        public PlayerPet(int currentLevel, double currentXP, double[] requirements, UUID petId, Pet template, Player p) {
            super(currentLevel, currentXP, requirements);
            this.playerUUID = p.getUniqueId();
            this.armorStand = (ArmorStand) p.getWorld().spawnEntity(p.getLocation(), EntityType.ARMOR_STAND);
            armorStand.setVisible(false);
            armorStand.setMarker(true);
            armorStand.setCustomNameVisible(false);
    
            armorStand.setSmall(true);
            nameTag = (ArmorStand) p.getWorld().spawnEntity(p.getLocation(), EntityType.ARMOR_STAND);
            nameTag.setVisible(false);
            nameTag.setMarker(true);
            nameTag.setCustomNameVisible(false);
    
            petInfo = Tuple.of(petId, template);
    
            runnable = getRunnable();
    
            registered.put(p.getUniqueId(), this);
            armorStand.setVisible(false);
    
            start();
        }
    
        private BukkitRunnable getRunnable() {
            return new BukkitRunnable() {
    
                Player player = Bukkit.getPlayer(playerUUID);
                boolean up = false;
                double bobbing = 0;
    
                @Override
                public void run() {
                    if(petInfo.getFirst() == null) {
                        up = false;
                        return;
                    }
                    ImmutableTriplet<Double, Double, Double> now  = Tuple.immutableOf(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());
                    double distance = Math.sqrt(player.getLocation().distanceSquared(armorStand.getLocation()));
                    Location test = armorStand.getLocation().clone().add(0, -0.5, 0);
                    if(distance > 2) {
                        test.add(new Vector(now.getFirst(), now.getSecond(), now.getThird()).subtract(test.toVector()).normalize().multiply(0.3));
                        float yaw = (float) Math
                                .toDegrees(Math.atan2(player.getLocation().getZ() - armorStand.getLocation().getZ(),
                                        player.getLocation().getX() - armorStand.getLocation().getX()))
                                - 90;
                        test.setYaw(yaw);
                    }
                    test.add(0, 0.5 + bobbing, 0);
    
                    if(!up) {
                        bobbing = bobbing + 0.015;
                    } else {
                        bobbing = bobbing - 0.015;
                    }
                    if(bobbing >= 0.125 || bobbing <= -0.125) up = !up;
    
                    armorStand.teleport(test);
                    updateName();
                }
            };
        }
    
        public String getName() {
            return petInfo.getSecond().getRarity().getPrefix() + petInfo.getSecond().getName();
        }
    
        public void addXP(double amount) {
            super.addXP(amount, Bukkit.getPlayer(playerUUID));
        }
    
        public UUID getActiveUUID() {
            return petInfo.getFirst();
        }
    
        public boolean isActive() {
            return getActiveUUID() != null;
        }
    
        private void stop() {
            try {runnable.cancel(); running = false;}catch(Exception e) {}
        }
    
        public void start() {
            runnable.runTaskTimer(Pets.getInstance(), 1, 1);
            running = true;
        }
    
        public ArmorStand getFirstrmorStand() {
            return armorStand;
        }
    
        public void updateName() {
            if(!nameTag.getCustomName().contains("" + getLevel())) nameTag.setCustomName("§8[§7Lv" + getLevel() + "§8] " + petInfo.getSecond().getRarity().getPrefix() + Bukkit.getPlayer(playerUUID).getDisplayName() + "'s " + petInfo.getSecond().getName());
    
            nameTag.teleport(armorStand.getLocation().clone().add(0, 1, 0));
        }
        public void clear() {
            save();
            petInfo.setFirst(null);
            petInfo.setSecond(null);
            nameTag.setCustomNameVisible(false);
            armorStand.setHelmet(null);
        }
    
        public void save() {
            if(petInfo.getFirst() == null) return;
            File playerFile = new File("plugins/Pets/player/" + playerUUID.toString());
            FileConfiguration fc = YamlConfiguration.loadConfiguration(playerFile);
            try {
                fc.load(playerFile);
                fc.set(petInfo.getFirst().toString() + ".level", getLevel());
                fc.set(petInfo.getFirst().toString() + ".currentXP", getCurrentXP());
                fc.save(playerFile);
            } catch (IOException | InvalidConfigurationException e) {
                Chat.sendMessage("Error While Saving Pet(" + petInfo.getSecond().getName() + ") For " + Bukkit.getOfflinePlayer(playerUUID).getName(), ChatLevel.WARN);
                e.printStackTrace();
            }
        }
        public void delete() {
            clear();
            stop();
            nameTag.remove();
            armorStand.remove();
            registered.remove(playerUUID);
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page