Having a problem with loading an entity when the player leave the server

Discussion in 'Plugin Development' started by Heaved, Dec 4, 2021.

  1. Offline

    Heaved

    Hello, im working on a small project which can spawn a small floating lootbox on a hidden armor stand, but i faced an issue when the player leave the server and reconect the lootbox disappear, and will not spawn again on the place where i used the command to spawn it
    REMARK: the lootbox respawn every 15s when a player right click it + there is 3 type of it https://prnt.sc/20xomn7

    here the code i use to spawn the lootbox
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Only players command.");
                return true;
            }
            Player p = (Player)sender;
            if(p.hasPermission("lootbox.create")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes
                            ('&',"&c[!] &7Usage &c/lootbox help"));
                } else {
                    if (args[0].equals("help")) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes
                                ('&',"&8- &b/LootBox Create &8- &7To create a random lootbox"));
                        sender.sendMessage(ChatColor.translateAlternateColorCodes
                                ('&',"&8- &b/LootBox Remove &8- &7To remove a lootbox"));
                    }
                    if (args[0].equals("remove")) {
                        ItemStack remover = new ItemStack(Material.BLAZE_ROD);
                        ItemMeta Mremover = remover.getItemMeta();
                        Mremover.setDisplayName(ChatColor.translateAlternateColorCodes
                                ('&',"&cLootBox Remover"));
                        remover.setItemMeta(Mremover);
                        p.getInventory().addItem(remover);
                        p.sendMessage(ChatColor.translateAlternateColorCodes
                                ('&',"&a[!] &7Click a lootbox to remove it."));
                    }
                    if (args[0].equals("create")) {
                        List<String> messages = Main.Instance.getConfig().getStringList("Type");
                        Random random = new Random();
                        int index = random.nextInt(messages.size());
                        if ((messages.get(index)).equals("Epic"))
                            CreateEpic(p);
                        if ((messages.get(index)).equals("Rare"))
                            CreateRare(p);
                        if ((messages.get(index)).equals("Common"))
                            CreateCommon(p);
                    }
                }
            } else {
                p.sendMessage(ChatColor.translateAlternateColorCodes
                        ('&', Main.Instance.getConfig().getString("Messages.NoPerms")));
            }
            return true;
        }
        private Object CreateCommon(Player p) {
            ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
            SkullMeta meta = (SkullMeta)skull.getItemMeta();
            meta.setOwner("ZeroGravityMC");
            skull.setItemMeta(meta);
            World w = p.getWorld();
            double x = p.getLocation().getX();
            double y = p.getLocation().getY() - 1.0D;
            double z = p.getLocation().getZ();
            Location loccommon = new Location(w, x, y, z);
            ArmorStand holo = (ArmorStand)p.getWorld().spawnEntity(loccommon, EntityType.ARMOR_STAND);
            holo.setVisible(false);
            holo.setGravity(false);
            holo.setHelmet(skull);
            holo.setCustomNameVisible(true);
            holo.setCustomName(ChatColor.translateAlternateColorCodes
                    ('&',"&aCommon"));
            rooling(loccommon, holo);
            return null;
        }
        private Object CreateRare(Player p) {
            ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short)SkullType.PLAYER.ordinal());
            SkullMeta meta = (SkullMeta)skull.getItemMeta();
            meta.setOwner("Reziel");
            skull.setItemMeta(meta);
            World w = p.getWorld();
            double x = p.getLocation().getX();
            double y = p.getLocation().getY() - 1.0D;
            double z = p.getLocation().getZ();
            Location locrare = new Location(w, x, y, z);
            ArmorStand holo = (ArmorStand)p.getWorld().spawnEntity(locrare, EntityType.ARMOR_STAND);
            holo.setVisible(false);
            holo.setGravity(false);
            holo.setHelmet(skull);
            holo.setCustomNameVisible(true);
            holo.setCustomName(ChatColor.translateAlternateColorCodes
                    ('&',"&9Rare"));
            rooling(locrare, holo);
            return null;
        }
        private Object CreateEpic(Player p) {
            ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short)SkullType.PLAYER.ordinal());
            SkullMeta meta = (SkullMeta)skull.getItemMeta();
            meta.setOwner("XxAltha4xX");
            skull.setItemMeta(meta);
            World w = p.getWorld();
            ArrayList<ArmorStand> normal = new ArrayList<>();
            double x = p.getLocation().getX();
            double y = p.getLocation().getY() - 1.0D;
            double z = p.getLocation().getZ();
            Location locepic = new Location(w, x, y, z);
            ArmorStand holo = (ArmorStand)p.getWorld().spawnEntity(locepic, EntityType.ARMOR_STAND);
            normal.add(holo);
            holo.setVisible(false);
            holo.setGravity(false);
            holo.setHelmet(skull);
            holo.setCustomNameVisible(true);
            holo.setCustomName(ChatColor.translateAlternateColorCodes
                    ('&',"&5Epic"));
            rooling(locepic, holo);
            return null;
        }
        public void rooling(Location loc, final ArmorStand holo) {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.Instance, new Runnable() {
                public void run() {
                    holo.setHeadPose(holo.getHeadPose().add(0.0D, 0.1D, 0.0D));
                }
            },0L, 1L);
        }
    }
    im looking for when i run the command /lootbox create it will save the location of that lootbox in a data.yml

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 4, 2021

Share This Page