Solved Reading from config isn't working?

Discussion in 'Spigot Plugin Development' started by x1D, May 31, 2018.

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

    x1D

    I'm creating a plugin that will replace certain blocks whenever you register them. The command works fine and it goes into the config, but it when it comes to replacing the block itself, it doesn't work?

    onEnable() method in Main Class:
    Code (Java):
    @Override
        public void onEnable() {
           
            plugin = this;
            registerEvents(this);
            getCommand("portalprotection").setExecutor(new PortalProtection());
            plugin.getConfig().options().copyDefaults(true);
           
           
           
                Bukkit.getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("PortalProtection"), new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < plugin.getConfig().getInt("portals"); i++) {
                             /* ---
                             ** -X-
                             ** --- */

                               Bukkit.getServer().getWorld(world).getBlockAt(plugin.getConfig().getInt("portal-list." + i + ".X"),
                                       plugin.getConfig().getInt("portal-list." + i + ".Y"),
                                       plugin.getConfig().getInt("portal-list." + i + ".Z")).setType(Material.ENDER_PORTAL);
                            /* -X-
                            ** ---
                            ** --- */

                           Bukkit.getServer().getWorld(world).getBlockAt(plugin.getConfig().getInt("portal-list." + i + ".X"),
                                   plugin.getConfig().getInt("portal-list." + i + ".Y"),
                                   plugin.getConfig().getInt("portal-list." + i + ".Z") + 1).setType(Material.ENDER_PORTAL);
                         /* ---
                         ** ---
                         ** -X- */

                        Bukkit.getServer().getWorld(world).getBlockAt(plugin.getConfig().getInt("portal-list." + i + ".X"),
                                plugin.getConfig().getInt("portal-list." + i + ".Y"),
                                plugin.getConfig().getInt("portal-list." + i + ".Z") - 1).setType(Material.ENDER_PORTAL);
                         /* ---
                         ** X--
                         ** --- */

                        Bukkit.getServer().getWorld(world).getBlockAt(plugin.getConfig().getInt("portal-list." + i + ".X") - 1,
                                plugin.getConfig().getInt("portal-list." + i + ".Y"),
                                plugin.getConfig().getInt("portal-list." + i + ".Z")).setType(Material.ENDER_PORTAL);
                         /* ---
                         ** --X
                         ** --- */

                        Bukkit.getServer().getWorld(world).getBlockAt(plugin.getConfig().getInt("portal-list." + i + ".X") + 1,
                                plugin.getConfig().getInt("portal-list." + i + ".Y"),
                                plugin.getConfig().getInt("portal-list." + i + ".Z")).setType(Material.ENDER_PORTAL);
                         /* X--
                         ** ---
                         ** --- */

                        Bukkit.getServer().getWorld(world).getBlockAt(plugin.getConfig().getInt("portal-list." + i + ".X") - 1,
                                plugin.getConfig().getInt("portal-list." + i + ".Y"),
                                plugin.getConfig().getInt("portal-list." + i + ".Z") + 1).setType(Material.ENDER_PORTAL);
                         /* --X
                         ** ---
                         ** --- */

                        Bukkit.getServer().getWorld(world).getBlockAt(plugin.getConfig().getInt("portal-list." + i + ".X") + 1,
                                plugin.getConfig().getInt("portal-list." + i + ".Y"),
                                plugin.getConfig().getInt("portal-list." + i + ".Z") + 1).setType(Material.ENDER_PORTAL);
                         /* ---
                         ** ---
                         ** X-- */

                        Bukkit.getServer().getWorld(world).getBlockAt(plugin.getConfig().getInt("portal-list." + i + ".X") - 1,
                                plugin.getConfig().getInt("portal-list." + i + ".Y"),
                                plugin.getConfig().getInt("portal-list." + i + ".Z") - 1).setType(Material.ENDER_PORTAL);
                         /* ---
                         ** ---
                         ** --X */

                        Bukkit.getServer().getWorld(world).getBlockAt(plugin.getConfig().getInt("portal-list." + i + ".X") + 1,
                                plugin.getConfig().getInt("portal-list." + i + ".Y"),
                                plugin.getConfig().getInt("portal-list." + i + ".Z") - 1).setType(Material.ENDER_PORTAL);
                        }
                    }
                    // first long is to determine how often it occurs
                }, 1L, (long) 5 * 20);
               
        }
    If anymore information needs to be provided, I will gladly do so.
    Also any suggestions to help improve this code to make it look nicer and/or make it more efficient would be appreciated!
     
  2. Why is your
    Code (Text):
    Bukkit.getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("PortalProtection"), new Runnable() {
    indented?
     
  3. x1D

    x1D

    It's not indented anymore than it needs to be in Eclipse, I guess it messed up somewhere when I copied and pasted the code onto Spigot. Sorry for that, just disregard the indentions.
     
    • Like Like x 1
  4. in the plugin parameter you can use "this". The JavaPlugin implements the Plugin Interface.
     
  5. x1D

    x1D

    Can you elaborate? I'm still only mildly experienced with Java.
    (Please don't say "learn Java," since that doesn't help me at all with what I'm trying to accomplish, which is getting this plugin made for a server.)
     
  6. Code (Java):
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {

                @Override
                public void run() {
                    // Do something
                 
                }
             
            }, 1L, 5L * 20L);
    BTW. I hate those peoples. Nobody is helped if someone say "Learn Java.". I understand why they tell it but everyone was a beginner. Just experience and tips helps someone to code better.

    "this" means the class as instance. You needn't do Bukkit.getPluginManager().getPlugin() if you have an instance of your Mainclass.
     
  7. lEaRn JaVa

    Could you show your config, or where you save values into your config?
    (Also, it seems that you're setting the X, Y, and Z values of single blocks individually. This can easily clutter up a config, so I'd instead suggest using a method to serialize and deserialize these values for easy reading and general tidyness.)
     
  8. x1D

    x1D


    Thank you so much! I'm glad to hear that there are some people who will actually contribute. This definitely made the code look a lot cleaner, but didn't resolve my issue. (all in all however, great tip!)

    Here is my config:
    Code (Text):
    portal-list:
      '1':
        X: '260'
        Y: '74'
        Z: '288'
    portals: 1
     
     
  9. In your config are those values Strings. Remove the ' ' and try again
     
    • Agree Agree x 1
  10. x1D

    x1D

    I added a string before the integer just to make sure that it is a string, and it didn't seem to work. I'll send updated code:

    Using what Rabbitual said, I made the center block as a location, and de-serialize it into X, Y, and Z. It also made the code cleaner. Still on the road to fixing this issue!

    onEnable() method in Main.java: (indentions may still be messed up, please ignore it)
    Code (Java):
        @Override
        public void onEnable() {
         
            plugin = this;
            registerEvents(this);
            getCommand("portalprotection").setExecutor(new PortalProtection());
            plugin.getConfig().options().copyDefaults(true);
         
         
         
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                        for (int i = 0; i < plugin.getConfig().getInt("portals"); i++) {
                            Location loc = new Location(Bukkit.getWorld(world), plugin.getConfig().getInt("portal-list." + ("portal-" + i) + ".X"),
                                       plugin.getConfig().getInt("portal-list." + ("portal-" + i) + ".Y"),
                                       plugin.getConfig().getInt("portal-list." + ("portal-" + i) + ".Z"));
                             /* ---
                             ** -X-
                             ** --- */

                           Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()).setType(Material.ENDER_PORTAL);
                            /* -X-
                            ** ---
                            ** --- */

                           Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX() + 1, loc.getBlockY(), loc.getBlockZ()).setType(Material.ENDER_PORTAL);
                         /* ---
                         ** ---
                         ** -X- */

                           Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX() - 1, loc.getBlockY(), loc.getBlockZ()).setType(Material.ENDER_PORTAL);
                         /* ---
                         ** X--
                         ** --- */

                           Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ() - 1).setType(Material.ENDER_PORTAL);
                         /* ---
                         ** --X
                         ** --- */

                           Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ() + 1).setType(Material.ENDER_PORTAL);
                         /* X--
                         ** ---
                         ** --- */

                           Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX() + 1, loc.getBlockY(), loc.getBlockZ() - 1).setType(Material.ENDER_PORTAL);
                         /* --X
                         ** ---
                         ** --- */

                           Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX() + 1, loc.getBlockY(), loc.getBlockZ() + 1).setType(Material.ENDER_PORTAL);
                         /* ---
                         ** ---
                         ** X-- */

                           Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX() - 1, loc.getBlockY(), loc.getBlockZ() - 1).setType(Material.ENDER_PORTAL);
                         /* ---
                         ** ---
                         ** --X */

                           Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX() + 1, loc.getBlockY(), loc.getBlockZ() - 1).setType(Material.ENDER_PORTAL);
                        }
                    }
                    // first long is to determine how often it occurs
                }, 1L, (long) 5 * 20);
             
        }
    Config.yml:
    Code (Text):
    portal-list:
      portal-1:
        X: '260'
        Y: '74'
        Z: '288'
    portals: 1
     
    edit: forgot to add .setType() to each line, added it in edit. (still doesn't fix problem, sadly)
     
    #10 x1D, May 31, 2018
    Last edited: May 31, 2018
  11. x1D

    x1D

    After adding a few lines of code to find the issue, I've determined that the plugin works, but it is NOT getting the value set in the config. It sets the block at 0, 0, 0.

    [​IMG]
     
  12. This is your Problem. You have a String at the Position in this Config. You want to get an Integer with the Method Config#getInt().
    This don't work. you must change the Strings into Integers in your config and then it should work
     
  13. In the config.yml your X, Y and Z values are a string, but when loading them, you try to load them as an integer. Either set the values in the config.yml to an integer (recommended) or load them in as a string and then parse them to an integer.

    I'd also recommend using a for loop to loop through every possibility instead of typing it out manually.
    Code (Java):
    for (int x = -1; x <= 1; x++) {
      for (int z = -1; z <= 1; z++) {
       Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX() + x, loca.getBlockY(), loc.getBlockZ() + z);
      }
    }
    So your code inside the run method would end up looking like this:
    Code (Java):

    for (int i = 0; i < plugin.getConfig().getInt("portals"); i++) {
      Location loc = new Location(Bukkit.getWorld(world), plugin.getConfig().getInt("portal-list." + ("portal-" + i) + ".X"), plugin.getConfig().getInt("portal-list." + ("portal-" + i) + ".Y"), plugin.getConfig().getInt("portal-list." + ("portal-" + i) + ".Z"));

      for (int x = -1; x <= 1; x++) {
        for (int x = -1; z <= 1; z++) {
          Bukkit.getServer().getWorld(world).getBlockAt(loc.getBlockX() + x, loc.getBlockY(), loc.getBlockZ() + z).setType(Material.ENDER_PORTAL);
        }
      }
    }
     
     
  14. By serialize, I mean writing it in the config as something like
    Code (Text):
    portal-1: '[worldname, x, y, z]'
    Then breaking it down and putting it back together as a Location. I'm not sure what you think I meant, given that I don't see particularly noticeable change..
     
  15. x1D

    x1D

    My issue was the for loop starting at 0 and not adding 1 to the "portals" integer. Thank you all for your help, support, and ideas!

    [thread locked] - DM me if you have any more suggestions! :)
     
Thread Status:
Not open for further replies.