Solved Can't get double from configuration

Discussion in 'Plugin Development' started by Kassestral, Feb 20, 2015.

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

    Kassestral

    So I have tried two different methods for creating a random Double and it doesn't seem to be that method that's causing the problem, the problem is getting the value for the double, I am pretty sure I have done it correctly but I can't seem to get it.

    Plugin.Java
    Code:
    package com.kassestral.plugins.imperium;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.EntityType;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.kassestral.plugins.imperium.events.PlayerVersusEntityForCash;
    
    public class Plugin extends JavaPlugin {
       
        public static Plugin plugin;
        private File economy_directory = new File(this.getDataFolder() + File.separator + "accounts");
        private File configuration_file = new File(this.getDataFolder() + File.separator + "config.yml");
        private YamlConfiguration configuration;
       
        public void onEnable() {
            plugin = this;
            try {
                onStart();
                createConfig();
                } catch (IOException e) {
                    e.printStackTrace();
                    }
            this.getServer().getPluginManager().registerEvents(new PlayerVersusEntityForCash(), this);
        }
       
        public void onDisable() {
           
        }
       
        private void onStart() throws IOException {
            if(!economy_directory.exists()) {
                economy_directory.mkdirs();
            }
            if(!configuration_file.exists()) {
                configuration_file.createNewFile();
                YamlConfiguration.loadConfiguration(configuration_file).save(configuration_file);
            }
        }
       
        @SuppressWarnings("deprecation")
        private void createConfig() throws IOException {
            YamlConfiguration configuration = YamlConfiguration.loadConfiguration(configuration_file);
            for(int i = 54; i > 53 && i < 67; i++) {
                EntityType hostile_type = EntityType.fromId(i);
                configuration.set("mobs."+hostile_type.getName().toLowerCase()+".reward.lowest", 10);
                configuration.set("mobs."+hostile_type.getName().toLowerCase()+".reward.highest", 20);
            }
            for(int x = 90; x > 89 && x < 101; x++) {
                EntityType passive_type = EntityType.fromId(x);
                configuration.set("mobs."+passive_type.getName().toLowerCase()+".reward.lowest", 10);
                configuration.set("mobs."+passive_type.getName().toLowerCase()+".reward.highest", 20);
            }
            configuration.save(configuration_file);
        }
    
        public YamlConfiguration getConfiguration() {
            this.configuration = YamlConfiguration.loadConfiguration(configuration_file);
            return configuration;
        }
    }
    PlayerVersusEntityForCash.Java
    Code:
    package com.kassestral.plugins.imperium.events;
    
    import java.util.Random;
    
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    
    import com.kassestral.plugins.imperium.Message;
    import com.kassestral.plugins.imperium.Plugin;
    
    public class PlayerVersusEntityForCash implements Listener {
       
        private Plugin plugin = Plugin.plugin;
        private Message message = new Message();
        private YamlConfiguration configuration = plugin.getConfiguration();
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void playerVersusEntity(EntityDeathEvent event) {
            if(event.getEntity().getKiller() instanceof Player) {
                Entity killer = event.getEntity().getKiller();
               
                if(killer instanceof Player) {
                    Player player = (Player) killer;
                    if(player.isOnline()) {
                        EntityType entity_type = event.getEntityType();
                        double lowest_amount = configuration.getDouble("mobs."+entity_type.toString().toLowerCase()+".reward.lowest");
                        double highest_amount = configuration.getDouble("mobs."+entity_type.toString().toLowerCase()+".reward.highest");
                        double amount = randomDouble(lowest_amount, highest_amount);
                        player.sendMessage(entity_type.toString());
                        player.sendMessage("" + amount);
                        player.sendMessage(message.playerGainsCash(entity_type.getName().toString(), amount));
                    }
                }
            }
        }
       
        private double randomDouble(double min, double max)
        {
            Random r = new Random();
            double randomValue = min + (max - min) * r.nextDouble();
            return randomValue;
            }
       
       
       
       
       
    }
     
  2. Offline

    xTrollxDudex

  3. Offline

    Kassestral

  4. @Kassestral You're having trouble getting a double from a config file? Here's a method for that:
    Code:
    public double getDouble() {
    return getConfig().getDouble("Path.Name");
    }
     
  5. Offline

    Kassestral

    @CodePlaysMinecraft
    I know how to do it, look through the code I have posted......
     
  6. @Kassestral
    well, `ve u check if config is saving everything?
    check too if the path when u`re storing the double is a double. If it`s a string, that could be the problem.
     
  7. Offline

    Kassestral

    @Juancomaster1998
    I've checked multiple times, and it is definitely a double, for some reason it just doesn't return a value

    I have also checked the path that it checks, and it's the exact same as where the value is in the config
     
  8. Offline

    mythbusterma

    @Kassestral

    Why aren't you using the default configuration methods? Those usually prevent issues like these.
     
  9. Offline

    FerusGrim

    Note; In any examples below, I've taken the liberty of changing your plugin's JavaPlugin implemented class' name to Main, rather than Plugin, as that seems crazily confusing with Bukkit's own Plugin class. I would change it to the intended name of your Plugin, as is our convention, but I'm not sure what that is.

    First, while static isn't necessarily a bad way to transfer singleton's in every use-case, it is here. Rather than using something like
    Code:java
    1. public static Main plugin;
    you should send the Main instance via construction.

    Code:java
    1. private final Main plugin;
    2.  
    3. public PlayerVersusEntityForCash(Main plugin) {
    4. this.plugin = plugin;
    5. }


    Secondly, "I am pretty sure I have done it correctly but I can't seem to get it." Unfortunately, however aggravating it may be, our programs always do exactly what we tell them to; not what we want them to.

    Thirdly, why aren't you using Bukkit's provided services for configuration?

    Code:java
    1. @Override
    2. public void onEnable() {
    3. this.saveDefaultConfig();
    4. this.getConfig()...;
    5. }


    Particularly: (Assuming you've stored the plugin instance in whatever class utilizes this)
    Code:java
    1.  
    2. double lowest = this.plugin.getConfig().getDouble("mobs."
    3. + event.getEntityType().toString().toLowerCase()
    4. + ".reward.lowest");
    5.  
     
  10. Offline

    _Filip

    @FerusGrim He wants to be unique, stop oppressing the poor guy!
     
    Kassestral and FerusGrim like this.
  11. Offline

    Kassestral

    @mythbusterma @FerusGrim
    Much appreciated, I have never had this problem before, as I have done it in another batch of code, but for some reason it's only in this particular piece of code that it doesn't work, I am further investigating this problem for myself, however I appreciate the help :) Thank you

    So I scanned through my code for the last 30 minutes to see where I went wrong, turns out I missed out the one mob ID that I was trying to get XD

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

Share This Page