1.17.1 CONFIG.YML

Discussion in 'Spigot Plugin Development' started by peti173, Nov 23, 2021.

  1. Hello ! Im trying to make a plugin where you get nuggets instead of raw ore from mining gold and iron ores, and i want the nugget drops amount to be configurable in the config.yml

    what do i need to edit in this code ?

    if (blockBroken.getType() == Material.GOLD_ORE) {
    event.setCancelled(true);
    blockBroken.setType(Material.AIR);
    ItemStack gold = new ItemStack(Material.GOLD_NUGGET, 5);
    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), gold);

    i suppose i need to edit the GOLD_NUGGET, 5); part right?
     
  2. You do indeed. The 5 represents the amount within the ItemStack.

    https://hub.spigotmc.org/javadocs/s...temStack.html#<init>(org.bukkit.Material,int)
     
  3. Get an instance of your config and then use that instance to get an integer from the file.
     
  4. Here you can use simple class..
    Creation:

    public static config SYSCONFIG config;

    onload() {
    config = new SYSCONFIG("config.yml", this, false);
    }

    (and put config.yml into src..)

    Code (Text):
    package org.syCore.Syl3nt_.java;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    public class SYSCONFIG {

        // Values
        public static int configCount = 0;
        public String configsName;
        public FileConfiguration var;
        public File varFile;
        public Plugin plug;

        // Creating config
        public SYSCONFIG(String configName, Plugin pl, boolean debug) {
            plug = pl;
            configsName = configName;
            if (!pl.getDataFolder().exists()) {
                pl.getDataFolder().mkdir();
            }
            ++configCount;
            varFile = new File(pl.getDataFolder(), configName);
            boolean exists = false;
            for (File var : pl.getDataFolder().listFiles()) {
                if (var.getName().equals(varFile.getName())) {
                    exists = true;
                }
            }
            if (!exists) {
                pl.saveResource(configName, true);
            }
            if (debug) {
                pl.getLogger().info("[SCONFIG] >> " + configName + " was loaded!");
            }
            if (configName.contains(".txt")) {
                return;
            }
            var = YamlConfiguration.loadConfiguration(varFile);
        }

        // Getting single values
        public String getString(String path) {
            return var.getString(path).replace("&", "§");
        }
        public boolean getBoolean(String path) {
            return var.getBoolean(path);
        }
        public int getInt(String path) {
            return var.getInt(path);
        }
        public double getDouble(String path) {
            return var.getDouble(path);
        }
        public float getFloat(String path) {
            return Float.parseFloat(var.getString(path));
        }

        // Getting list values
        public List<String> getStringList(String path) {
            List<String> is = new ArrayList<>();
            for (String l : var.getStringList(path)) {
                is.add(l.replace("&", "§"));
            }
            return is;
        }
        public List<Boolean> getBooleanList(String path) {
            return var.getBooleanList(path);
        }
        public List<Integer> getIntList(String path) {
            return var.getIntegerList(path);
        }
        public List<Double> getDoubleList(String path) {
            return var.getDoubleList(path);
        }
        public List<Float> getFloatList(String path) {
            return var.getFloatList(path);
        }
        public List<String> getKeyList(String path) {
            List<String> is = new ArrayList<>();
            for (String loop : var.getConfigurationSection(path).getKeys(false)) {
                is.add(loop);
            }
            return is;
        }

        // Checking path values
        public boolean isString(String path, String equals) {
            if (var.getString(path).equals(equals)) {
                return true;
            }
            return false;
        }
        public boolean isBoolean(String path, boolean equals) {
            if (Boolean.toString(var.getBoolean(path)).equals(Boolean.toString(equals))) {
                return true;
            }
            return false;
        }
        public boolean isInt(String path, int equals) {
            if (var.getInt(path)==equals) {
                return true;
            }
            return false;
        }
        public boolean isDouble(String path, double equals) {
            if (var.getDouble(path)==equals) {
                return true;
            }
            return false;
        }
        public boolean isFloat(String path, float equals) {
            if (Float.parseFloat(var.getString(path))==equals) {
                return true;
            }
            return false;
        }
        public boolean isKey(String path, String equals) {
            List<String> keyList = new ArrayList<>();
            for (String loop : var.getConfigurationSection(path).getKeys(false)) {
                keyList.add(loop);
            }
            if (keyList.contains(equals)) {
                return true;
            }
            return false;
        }

        // Setting values
        public void setValue(String path, String value) {var.set(path, value);}
        public void setValue(String path, boolean value) {var.set(path, value);}
        public void setValue(String path, int value) {var.set(path, value);}
        public void setValue(String path, double value) {var.set(path, value);}
        public void setValue(String path, float value) {var.set(path, value);}

        // Reload
        public void reload() {
            varFile = new File(plug.getDataFolder(), configsName);
            var = YamlConfiguration.loadConfiguration(varFile);
        }

        // Save
        public void save() {
            try {
                var.save(varFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
     
    And use config.getString(string path);
     
  5. Strahan

    Benefactor

    That's way overdone, it'd be a lot easier to just use config.yml and the built in methods. No need to reinvent the wheel.
     
  6. Once you create the configuration file it is as simple as doing the following
    Code (Java):
    public void blockBreak(BlockBreakEvent event){
            Player player = event.getPlayer();
            Block block = event.getBlock();
            if(block.getType() != Material.GOLD_ORE){
                return;
            }
            event.setCancelled(true);
            block.setType(Material.AIR);
            ItemStack item = new ItemStack(Material.GOLD_NUGGET,config.getInt("settings.drop-nugget"));
            player.getWorld().dropItemNaturally(block.getLocation(),item);
        }
    Or if you want to make it so that everything can be modified in your configuration file it would be


    Code (Java):
    public void blockBreak(BlockBreakEvent event){
            Player player = event.getPlayer();
            Block block = event.getBlock();
            if(block.getType() != Material.matchMaterial(config.getString("settings.if-drop-material"))){
                return;
            }
            event.setCancelled(true);
            block.setType(Material.AIR);
            ItemStack item = new ItemStack(Material.matchMaterial(config.getString("settings.if-drop-material"),config.getInt("settings.drop-nugget"));
            player.getWorld().dropItemNaturally(block.getLocation(),item);
        }
    In the event you must already have the instance of your configuration file
     
  7. Strahan

    Benefactor

    You really should validate your input. You're drawing from a source the admin can touch, so it should be treated as suspect.
     
  8. There’s a FallenMoons12 tutorial on this. Not a lot of people watch his videos but they’re some of the best tutorials out there.