Solved Get array from ConfigurationSection

Discussion in 'Plugin Development' started by Xp10d3, Feb 3, 2020.

  1. Offline

    Xp10d3

    Hello,
    I want to get an array from a configuration section so that there can be an infinite amount of values in the config. This is an example of what I want to do.

    If the config is like this (yes I know it's not an array, but it's a bit similar):
    Code:
    commands:
      command:
        30, 75, 3:
          command: effect give @p slowness 500 3
        1, 300, 6:
          command: kill @p
    
    I want to get the location 30, 75, 3 and 1, 300, 6 and then get the string effect give @p slowness 500 3 and kill @p and then run the commands by getting the location. The basic premise of the plugin is to sense the location wanted (in this case 30, 75, 3 and 1, 300, 6) and then run a command at that location if the player is there. But it's hard to do that when you can't get the location xD Thanks for the help!
    -Xp10d3
     
  2. Online

    timtower Moderator Moderator

    @Xp10d3 Get it to store first, and be carefull with overriding values.
    Knowing how to read requires something written, and I am sure that the system won't make that out of it.
     
  3. Offline

    Xp10d3

    Whoops sorry I forgot to post my class xD Alright, thanks. I think I have stored it already though...? Forgive me if I'm mistaken :p

    PlayerListeners Class:
    Code:
    package xp10d3.replace.corelia;
    
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    
    public class PlayerListeners implements Listener {
        private Core core;
       
        @EventHandler
        public void savePlayerLocation(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            core.getConfig().set(player.getUniqueId().toString() + ".lastlocation.x", player.getLocation().getX());
            core.getConfig().set(player.getUniqueId().toString() + ".lastlocation.y", player.getLocation().getY());
            core.getConfig().set(player.getUniqueId().toString() + ".lastlocation.z", player.getLocation().getZ());
            core.getConfig().set(player.getUniqueId().toString() + ".lastlocation.world", player.getLocation().getWorld().getName());
        }
       
        @EventHandler
        public void checkLocation(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            int locationX = core.getConfig().getInt(player.getUniqueId().toString() + ".lastlocation.x");
            int locationY = core.getConfig().getInt(player.getUniqueId().toString() + ".lastlocation.y");
            int locationZ = core.getConfig().getInt(player.getUniqueId().toString() + ".lastlocation.z");
            String locationWorld = core.getConfig().getString(player.getUniqueId().toString() + ".lastlocation.world");
           
            Location loc = player.getLocation();
           
            String command = core.getConfig().getString("commands.here");
           
            if (loc.getBlockX() == locationX && loc.getBlockY() == locationY && loc.getBlockZ() == locationZ && player.getWorld().getName() == locationWorld) {
                core.getServer().dispatchCommand(core.getServer().getConsoleSender(), command.replace("%player%", player.getName()));
            }
        }
    }
    
     
  4. Online

    timtower Moderator Moderator

    @Xp10d3 You want to rely less of your config.
    And initialize the core variable.
    Use a map<uuid, location> so you are not working in the config all the time (time consuming)
    For the checks: you are not saving the configuation that you posted here.
     
  5. Offline

    Xp10d3

    Alright. If I'm using a HashMap what do I do with my config since I want all the data to be changed from there? For the config part I'll change that; thanks for the catch :)
     
  6. Online

    timtower Moderator Moderator

    @Xp10d3 You store the last location when the player leaves
     
  7. Offline

    Xp10d3

    But I want to store the current location and change it every time a player moves one block. Sorry, it might not be clear what I'm trying to achieve. Basically I want to store the location of a player every time the player walks, and then get that value and constantly check whether they are at the specified location. If they are, then run a console command that will act like you are walking on a command block with a pressure plate. I'm trying to replace the use of a command block and just run a command whenever the player is at the location specified in the config.
    Ex.
    If the config looks like this:
    Code:
    <player's UUID>
      lastlocation
        x: 50
        y: 80
        z: 3
        world: world_nether
    command:
      commands:
        50, 80, 3
          command: effect give @p slowness 5000 5
    
    Then the player's location gets updated in the x: y: z: and world. The player's x value get's stored in x: and the player's y in y: and the z in z: and the world name in world: . Finally the location that the owner wants to specify get's stored in command.commands.<location> which I'm having trouble with. I want to make it so that if the player is at that location (50, 80, 3 in this case), then run the command effect give @p slowness 5000 5. But once the the player moves past that location, the location will get updated and the command won't be run. Hope that clears things up.
     
  8. Online

    timtower Moderator Moderator

    @Xp10d3 Accessing the config takes time, hashmap less, you only need to update the config when the player leaves.
    How did you save the coordinates for the command? Thought about using a different file for it?
     
  9. Offline

    Xp10d3

    I haven't thought about using a different file :p But I want there to be infinite entries, and I thought a config section could work. But I don't know how I would loop there isn't an useful entry. Like if the config is like this:
    Code:
    command:
      commands:
        effect give @p slowness 500 50
          x: <x>
          y: <y>
          z: <y>
          world: <world>
    
    I don't know whether getting the config section will work with so many spaces...? Is it possible?
     
  10. Offline

    Strahan

    Just FYI, the config is cached. Config calls reading values are made against a HashMap internally, not disk so there is really no performance penalty there.
     
  11. Offline

    Xp10d3

    Alright. Thanks. So could I get the config section if it's "effect give @p slowness 500 50" and just get that string? How would I exactly do that?
     
  12. Online

    timtower Moderator Moderator

    @Xp10d3 ConfigurationSection#getKeys(false)
     
  13. Offline

    Xp10d3

    I don't know if I'm understanding the ConfigurationSection so sorry for all the problems. I'm trying to send a console command now, and the way I'm trying to do it uses this line:
    Code:
    core.getServer().dispatchCommand(core.getServer().getConsoleSender(), commandInLoop.replace("%player%", player.getName()));
    
    which requires the variable to be a String, not Set<String> or ConfigurationSection. This is currently what I have:
    Code:
            Player player = event.getPlayer();
            int locationX = core.getConfig().getInt(player.getUniqueId().toString() + ".lastlocation.x");
            int locationY = core.getConfig().getInt(player.getUniqueId().toString() + ".lastlocation.y");
            int locationZ = core.getConfig().getInt(player.getUniqueId().toString() + ".lastlocation.z");
            String locationWorld = core.getConfig().getString(player.getUniqueId().toString() + ".lastlocation.world");
           
            ConfigurationSection command = core.getConfig().getConfigurationSection("command.commands");
            for (String key : command.getKeys(false)) {
                int x = core.getConfig().getInt("command.commands." + key + ".x");
                int y = core.getConfig().getInt("command.commands." + key + ".y");
                int z = core.getConfig().getInt("command.commands." + key + ".z");
                Set<String> commandInLoop = core.getConfig().getConfigurationSection("command.commands").getKeys(false);
                String world = core.getConfig().getString("command.commands." + key + ".world");
                if (x == locationX && y == locationY && z == locationZ && world == locationWorld) {
                    core.getServer().dispatchCommand(core.getServer().getConsoleSender(), commandInLoop.replace("%player%", player.getName()));
                }
            }
    
     
  14. Online

    timtower Moderator Moderator

    @Xp10d3 commandinloop is not needed though.
    The command is key
     
  15. Offline

    Xp10d3

    Oh whoops lol. Okay, thanks! That was real helpful. Marking thread as solved :D
     
    timtower likes this.

Share This Page