Reading through config and getting sub keys?

Discussion in 'Plugin Development' started by Chrusty45, Aug 15, 2014.

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

    Chrusty45

    So I'm working on the team aspect of my plugin and for some reason this doesn't work. First I have the loop search for all the team names, then the player names in the sub-key list of the team name.

    This is what a config entry looks like:
    Teams:
    TeamName1:
    Players:
    - Player1
    - Player2
    - Player3

    My problem is, is that it won't for whatever reason, find the players in the sub keys and add them to the team. Here is my code:
    Code:java
    1. ConfigurationSection teamNames = teamConfig.getConfigurationSection("Teams");
    2. for(String key : teamNames.getKeys(false)) {
    3. TeamManager.registerTeam(key);
    4.  
    5. for(String playerName : teamConfig.getStringList(key + ".Players")) {
    6. Player player = Bukkit.getPlayerExact(playerName);
    7. Team.addPlayer(player, Team.getTeam(key));
    8. }
    9. }
    10. }


    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    SpaceManiac

    You're looking for values in the wrong place: `teamNames.getKeys(false)` vs `teamConfig.getStringList(key + ".Players")`
     
    Chrusty45 likes this.
  3. Offline

    Chrusty45

    SpaceManiac ... I can't believe I didn't see that earlier. I've spent hours being frustrated to hell. Thanks alot. xD
     
  4. Offline

    Chrusty45

    SpaceManiac I seem to have another problem. It now reads through the config fine, but I get a npe on ChatColor.valueof. But the wierd thing is, is that it still changes the player's chatcolor color.
    Here is the code
    Code:java
    1. public void registerTeams() {
    2. ConfigurationSection teamNames = teamConfig.getConfigurationSection("Teams");
    3. for(String key : teamNames.getKeys(true)) {
    4. String color = teamNames.getString(key + ".Color");
    5. ChatColor displayColor = ChatColor.valueOf(color);
    6. TeamManager.registerTeam(key, displayColor);
    7.  
    8.  
    9. for(String playerName : teamNames.getStringList(key + ".Players")) {
    10. Player player = Bukkit.getPlayerExact(playerName);
    11. if(player != null) {
    12. Team.addPlayer(player, Team.getTeam(key));
    13. }
    14. }
    15. }
    16. }


    Here's the stacktrace:
    Code:
    [23:40:39] [Server thread/ERROR]: Error occurred while enabling UHC v1.0 (Is it up to date?)
    java.lang.NullPointerException: Name is null
        at java.lang.Enum.valueOf(Enum.java:236) ~[?:1.8.0_05]
        at org.bukkit.ChatColor.valueOf(ChatColor.java:13) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at com.chrisreading.uhc.Main.registerTeams(Main.java:114) ~[?:?]
        at com.chrisreading.uhc.Main.onEnable(Main.java:57) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.java:446) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.java:380) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.reload(CraftServer.java:799) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.Bukkit.reload(Bukkit.java:288) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchServerCommand(CraftServer.java:688) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.aB(DedicatedServer.java:296) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:261) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    
     
  5. Offline

    AronTheGamer

    Use ChatColor#translateAlternateColorChar
     
  6. Offline

    Chrusty45

    AronTheGamer Hmm, this is super strange. I still get a npe using ChatColor.translateAlternateColorChar, but it still changes the player's chatcolor.

    The updated code:
    Code:java
    1. public void registerTeams() {
    2. ConfigurationSection teamNames = teamConfig.getConfigurationSection("Teams");
    3. for(String key : teamNames.getKeys(true)) {
    4. String color = teamNames.getString(key + ".Color");
    5. String displayColor = ChatColor.translateAlternateColorCodes('&', color);
    6. TeamManager.registerTeam(key, displayColor);
    7.  
    8. for(String playerName : teamNames.getStringList(key + ".Players")) {
    9. Player player = Bukkit.getPlayerExact(playerName);
    10. if(player != null) {
    11. Team.addPlayer(player, Team.getTeam(key));
    12. }
    13. }
    14. }
    15. }
     
  7. Offline

    SpaceManiac

    Replace getKeys(true) with getKeys(false) - getKeys(true) will return a list like ["TeamName1", "TeamName1.Color", "TeamName1.Players"] while getKeys(false) will just return the list of team names (take a look at the javadoc for getKeys for more detail).

    The use of translateAlternateColorCodes or ChatColor.valueOf is a matter of preference and how you want your config to be formatted.
     
  8. Offline

    Chrusty45

    SpaceManiac yes! that works! Also... (Sorry for all these problems.) It isn't reading through the config correctly. For example:
    Here is the config:
    Teams:
    team1:
    Players:
    - player1
    Color: '&a'
    team2:
    Players:
    - player2
    Color: '&e'

    It registers team2 but adds player2 to team1 instead of team2. I'm sorry but problems keep ocurring whenever I figure something out! haha.

    Bump

    anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Offline

    Chrusty45

Thread Status:
Not open for further replies.

Share This Page