accessing the config from a second class

Discussion in 'Plugin Development' started by supercas240, Jun 13, 2018.

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

    supercas240

    hey I ran into this issue trying to access the config file from my second "commands" file
    Code:
    int amount = this.getConfig().getInt("amount");
    getConfig() gives me an error saying its undefined.

    I did link the config file in the main class and linked the commands class to my main.
    I've looked around and seen that you need to create a getconfig() method in the main class but that didn't resolve the issue for me

    code:

    main class:
    Code:
    package com.supercas240.MobSpawningLimiter;
    
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin {
       
        public static main plugin;
    
       
        @Override
        public void onEnable() {
           
            plugin = this;
            getConfig().options().copyDefaults(true);
            saveConfig();
           
            this.getCommand("msl").setExecutor(new commands(this));
           
            getLogger().info("MobSpawningLimiter has been Enabled");
        }
       
       
       
        @Override
        public void onDisable() {
           
           
            getLogger().info("MobSpawningLimiter has been Disabled");
        }}
       
    



    commands class:
    Code:
    package com.supercas240.MobSpawningLimiter;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    
    
    public class commands implements CommandExecutor {
    
        main plugin;
        public commands(main main) {
            plugin = main;
        }
       
       
    
        public String prefix = ChatColor.BOLD + "" + ChatColor.BLACK + "[" + ChatColor.RED + ChatColor.BOLD
                + "MobSpawningLimiter" + ChatColor.BLACK + ChatColor.BOLD + "]  " + ChatColor.RESET;
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            int amount = this.getConfig().getInt("amount");
           
            if (cmd.getName().equalsIgnoreCase("msl")) {
                if (args.length > 0) {
    
                    if (args[0].equalsIgnoreCase("on")) {
                        if (!sender.hasPermission("msl.use")) {
                            sender.sendMessage(prefix + ChatColor.RED + "you are not allowed to use this command.");
                        } else {
    
                            // code for /msl on
    
                            sender.sendMessage(prefix + "Maximum amount of mobs has been set to " + amount
                                    + " in the predetermind area.");
    
                            World world = Bukkit.getWorld(getConfig().getString("world"));
                            Location location = new Location(world,
                                    this.getConfig().getInt("x"),
                                    this.getConfig().getInt("y"),
                                    this.getConfig().getInt("z"));
                            saveConfig();
    
                        }
                        return true;
                    }
    
                    else if (args[0].equalsIgnoreCase("off")) {
    
                        // code for /msl off
                        sender.sendMessage(prefix + "msl off");
    
                        return true;
                    }
    
                    else
                        return false;
                } else {
    
                    // code for /msl
                    sender.sendMessage("============================");
                    sender.sendMessage("=   " + prefix + "   =");
                    sender.sendMessage("============================");
                    sender.sendMessage("");
                    sender.sendMessage("[1]   Use '/msl on' to enable the plugin.");
                    sender.sendMessage("[2]   Use '/msl off' to disable the plugin.");
                    sender.sendMessage("");
                    sender.sendMessage("");
    
                    return true;
                }
            }
    
            return true;
        }
    }
    
    config:
    Code:
    #####MobSpawningLimiter by supercas240#####
    
    
    #the amount of mobs that can be in the given area at ones
    
    amount: 20
    
    
    
    #to select the location you first need to specify the world that your locationĀ“is in, by default "world"
    
    world: world
    
    
    
    #now specify the center point of your area
    
    x: 0
    y: 62
    z: 0
    
    
    
    #the radius of the square
    
    radius: 50
    plugin.yml:
    Code:
    name: MobSpawningLimiter
    version: 1.0
    main: com.supercas240.MobSpawningLimiter.main
    description: Limits the amount of mobs spawnable in a given region.
    
    commands:
        msl:
           usage: /<command>
           description:  the MobSpawningLimiter base command.
    
           
    permissions:
        msl.use:
            description: allows player to use the MobSpawnLimiter commands.
            default: op
    I am fairly new to bukkit but I have some experience with java, don't be to hard on me .P

    I just realised I posted in the wrong subforum, please move?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2018
  2. Online

    timtower Administrator Administrator Moderator

    @supercas240 You need to use plugin.getConfig in the second class
     
  3. Offline

    supercas240

    yeh no I tried that before I tried:
    Code:
    plugin.getconfig()
    main.getconfig()
    main.plugin.config()
    ...
    
    
    none of them worked
     
  4. Online

    timtower Administrator Administrator Moderator

    @supercas240 Define not work.
    And in which class have you tried that?
     
  5. Offline

    supercas240

    update: I rewrote part of my main class, It must have been a wrong capitalization or something similar that eclipse didn't pick up because It works now ;) thx for the help though

    oh also, I have another question that you might know an awnser to. Im trying to set a maximum amount of a certain mob in a given area, and I'm having issues trying to remove mobs that are over the limit. the way I'm currently doing it:
    Code:
        double radius = plugin.getConfig().getInt("radius");
                            List<Entity> near = location.getWorld().getEntities();
                            for(Entity e : near) {
                                if(e.getLocation().distance(location) <= radius && e.getType() == org.bukkit.entity.EntityType.SNOWMAN) {
                                    for(int i = 0; i < near.size(); i++) {
                                          if( i >= amount) {
                                             
                                                Entity last = near.get(near.size() -1);
                                                last.remove();
    
                                             
                                          }
                                         }
                                        }
                                       } 
                                       
                            
    this doesn't work however, ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2018
Thread Status:
Not open for further replies.

Share This Page