Solved How to save a location in config.yml

Discussion in 'Spigot Plugin Development' started by ImKuno_, Nov 25, 2021.

Thread Status:
Not open for further replies.
  1. So im coding a custom plugin for a custom lootbox that u can set on game using /lootbox create, but i have problem when i leave the server it disappear..

    Code (Text):
            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 void 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 loc = new Location(w, x, y, z);
            ArmorStand holo = (ArmorStand)p.getWorld().spawnEntity(loc, EntityType.ARMOR_STAND);
            holo.setVisible(false);
            holo.setGravity(false);
            holo.setHelmet(skull);
            holo.setCustomNameVisible(true);
            holo.setCustomName(ChatColor.translateAlternateColorCodes
                    ('&',"&aCommon"));
            rooling(loc, holo);
        }
        private void 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 loc = new Location(w, x, y, z);
            ArmorStand holo = (ArmorStand)p.getWorld().spawnEntity(loc, EntityType.ARMOR_STAND);
            holo.setVisible(false);
            holo.setGravity(false);
            holo.setHelmet(skull);
            holo.setCustomNameVisible(true);
            holo.setCustomName(ChatColor.translateAlternateColorCodes
                    ('&',"&9Rare"));
            rooling(loc, holo);
        }
        private void 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 loc = new Location(w, x, y, z);
            ArmorStand holo = (ArmorStand)p.getWorld().spawnEntity(loc, EntityType.ARMOR_STAND);
            normal.add(holo);
            holo.setVisible(false);
            holo.setGravity(false);
            holo.setHelmet(skull);
            holo.setCustomNameVisible(true);
            holo.setCustomName(ChatColor.translateAlternateColorCodes
                    ('&',"&5Epic"));
            rooling(loc, holo);
        }
        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);
        }
    }
    i hope someone can help me with it.
    thank you
     
  2. Im not sure this code will work fine in 1.8.8 too but try it :)

    Code (Text):
       
    public class PlayerConfiguration {
       static File cfile;
            static FileConfiguration config;
            static File folder = new File(plugin.getDataFolder(), "player data" + File.separator);
            static File df = plugin.getDataFolder();

            public static void create(Player p) {
                cfile = new File(df, "player data" + File.separator + p.getUniqueId() + ".yml");
                if (!df.exists()) df.mkdir();
                if (!cfile.exists()) {
                    try {
                        cfile.createNewFile();
                    } catch(Exception e) {
                        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error creating " + cfile.getName() + "!");
                    }
                }
                config = YamlConfiguration.loadConfiguration(cfile);
            }

            public static File getfolder() {
                return folder;
            }

            public static File getfile() {
                return cfile;
            }

            public static void load(Player p) {
                cfile = new File(df, "player data" + File.separator + p.getUniqueId() + ".yml");
                config = YamlConfiguration.loadConfiguration(cfile);
            }

            public static FileConfiguration get() {
                return config;
            }

            public static void save() {
                try {
                    config.save(cfile);
                } catch(Exception e) {
                    Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error saving " + cfile.getName() + "!");
                }
            }
     
    I use this.
    when you want to save something for that player just do this:

    Code (Text):
            PlayerConfiguration.load(player);
    PlayerConfiguration.get().set("location", player.getLocation());
            PlayerConfiguration.save();
     
  3. You can also get X, Y, Z, world, pitch & yaw. And save it to the Configuration section..
     
  4. thanks for the help, i really appreciate it ^^
     
    • Like Like x 1
  5. after reading the code and trying it i found that it's just saving the player location where he excuted the command not the box location, so it's still the same issue when he leave the server the loot box location will not load again when he join, it just disappear and u have to set it again using the command + (this lootbox have 15s to respawn when a player right click it) https://prnt.sc/20xomn7
     
  6. Code (Text):
    PlayerConfiguration.get().set("location", yourlootboxlocation);
    ?
     
  7. Also for disable or enabling your lootbox you can create the same class.
    Code (Text):
    public class LootBoxConfiguration {
       static File cfile;
            static FileConfiguration config;
            static File folder = new File(plugin.getDataFolder(), "lootbox" + File.separator);
            static File df = plugin.getDataFolder();

            public static void create(Integer lootboxid) {
                cfile = new File(df, "lootbox" + File.separator + lootboxid + ".yml");
                if (!df.exists()) df.mkdir();
                if (!cfile.exists()) {
                    try {
                        cfile.createNewFile();
                    } catch(Exception e) {
                        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error creating " + cfile.getName() + "!");
                    }
                }
                config = YamlConfiguration.loadConfiguration(cfile);
            }

            public static File getfolder() {
                return folder;
            }

            public static File getfile() {
                return cfile;
            }

            public static void load(Integer lootboxid) {
                cfile = new File(df, "lootbox" + File.separator + lootboxid + ".yml");
                config = YamlConfiguration.loadConfiguration(cfile);
            }

            public static FileConfiguration get() {
                return config;
            }

            public static void save() {
                try {
                    config.save(cfile);
                } catch(Exception e) {
                    Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error saving " + cfile.getName() + "!");
                }
            }
    Then. use integer and give your lootbox an lootbox id somehow
    After that add this to your code when someone creates a lootbox
    Code (Text):
    LootBoxConfiguration.create(someInteger);
    And then store every details you need for your lootbox
    also when you want to disapear that
    Code (Text):
    LootBoxConfiguration.getfile().delete();
     
    • Agree Agree x 1
  8. ^ This

    I would recommend learning how to use configuration files first https://www.spigotmc.org/wiki/config-files/
     
    • Like Like x 2
  9. i already use config files my issue was with saving a location of an entity
     
  10. Could always use
    Location#serialize
    Location#deserialize
     
  11. starting working on it, thank you :D
     
Thread Status:
Not open for further replies.