Solved I Have No Idea What's Happening

Discussion in 'Plugin Development' started by krazytraynz, Nov 24, 2013.

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

    krazytraynz

    I'm not too good with custom config files, so there's probably an obvious error somewhere in here that I'm not picking up on, but I've spent about 5 hours trying to figure this out and haven't gotten anywhere. The code is supposed to write the location of a sign to the path 'locations', but instead of the location of the sign it writes 'true'.

    SignsHandler
    Code:java
    1. package com.github.KrazyTraynz;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.util.ArrayList;
    7. import java.util.List;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.Location;
    11. import org.bukkit.World;
    12. import org.bukkit.block.Sign;
    13. import org.bukkit.configuration.file.FileConfiguration;
    14. import org.bukkit.configuration.file.YamlConfiguration;
    15.  
    16. public class SignsHandler {
    17.  
    18. private AdminSign as;
    19.  
    20. private FileConfiguration yml = null;
    21. private File ymlFile = null;
    22.  
    23. public SignsHandler(AdminSign as){
    24. this.as = as;
    25. }
    26.  
    27. public void reloadYml() {
    28. if (ymlFile == null) {
    29. ymlFile = new File(as.getDataFolder(), "AdminSigns.yml");
    30. }
    31. yml = YamlConfiguration.loadConfiguration(ymlFile);
    32.  
    33. InputStream defConfigStream = as.getResource("AdminSigns.yml");
    34. if (defConfigStream != null) {
    35. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    36. yml.setDefaults(defConfig);
    37. }
    38. }
    39.  
    40. public FileConfiguration getYml() {
    41. if (yml == null) {
    42. this.reloadYml();
    43. }
    44. return yml;
    45. }
    46.  
    47. public void saveCustomConfig() {
    48. if (yml == null || ymlFile == null) {
    49. return;
    50. }
    51. try {
    52. getYml().save(ymlFile);
    53. } catch (IOException ex) {
    54. ex.printStackTrace();
    55. }
    56. }
    57.  
    58. public void addSign(Sign s){
    59. yml = getYml();
    60. Location loc = s.getLocation();
    61. String location = loc.getWorld().getName() + "," + loc.getX() + "," + loc.getY() + "," + loc.getZ();
    62. yml.createSection("locations");
    63. List<String> locs = new ArrayList<String>();
    64. if(yml.get("locations") == null){
    65. locs.add(location);
    66. yml.createSection("locations");
    67. yml.set("locations", locs);
    68. this.saveCustomConfig();
    69. }
    70. locs.add(location);
    71. yml.set("locations", locs);
    72. yml.set("locations", yml.getStringList("locations").add(location));
    73. this.saveCustomConfig();
    74. }
    75.  
    76. public void removeSign(Sign s){
    77. Location loc = s.getLocation();
    78. String location = loc.getWorld().getName() + "," + loc.getX() + "," + loc.getY() + "," + loc.getZ();
    79. if(yml.getStringList("locations").contains(location)){
    80. yml.getStringList("locations").remove(location);
    81. this.saveCustomConfig();
    82. }
    83. }
    84.  
    85. public Location getLocation() {
    86. String[] loc = yml.getString("locations").split("\\,");
    87. World w = Bukkit.getWorld(loc[0]);
    88. Double x = Double.parseDouble(loc[1]);
    89. Double y = Double.parseDouble(loc[2]);
    90. Double z = Double.parseDouble(loc[3]);
    91. Location location = new Location(w, x, y, z);
    92. return location;
    93. }
    94. }
    95.  


    AdminSignExecutor
    Code:java
    1. package com.github.KrazyTraynz;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Sign;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandExecutor;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.block.Action;
    16. import org.bukkit.event.player.AsyncPlayerChatEvent;
    17. import org.bukkit.event.player.PlayerInteractEvent;
    18.  
    19. public class AdminSignExecutor implements CommandExecutor, Listener{
    20.  
    21. List<String> add = new ArrayList<String>();
    22. List<String> del = new ArrayList<String>();
    23.  
    24. @SuppressWarnings("unused")
    25. private AdminSign as;
    26. private SignsHandler sh;
    27.  
    28. public AdminSignExecutor(AdminSign as){
    29. this.as = as;
    30. sh = new SignsHandler(as);
    31. }
    32.  
    33.  
    34. @Override
    35. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    36. if(cmd.getName().equalsIgnoreCase("adminsign")){
    37. if(sender instanceof Player){
    38. Player p = (Player)sender;
    39. if (args.length < 1) {
    40. p.sendMessage(ChatColor.RED + "[AdminSign] What do you want to do?");
    41. p.sendMessage(ChatColor.RED + "Usage: /adminsign <add/remove>");
    42. return true;
    43. }
    44. if (args.length > 1) {
    45. p.sendMessage(ChatColor.RED + "[AdminSign] Too many arguments!");
    46. return false;
    47. }
    48.  
    49. if(args[0].equalsIgnoreCase("add")){
    50. if(p.hasPermission("adminsign.add")){
    51. add.add(p.getName());
    52. p.sendMessage(ChatColor.GREEN + "[AdminSign] Right click the sign you want to make an AdminSign!");
    53. p.sendMessage(ChatColor.GREEN + "Type \"cancel\" to cancel the AdminSign creation.");
    54. return true;
    55. }
    56. }
    57. if(args[0].equalsIgnoreCase("remove")){
    58. if(p.hasPermission("adminsign.remove")){
    59. del.add(p.getName());
    60. p.sendMessage(ChatColor.GREEN + "[AdminSign] Right click the AdminSign you want to remove!");
    61. p.sendMessage(ChatColor.GREEN + "Type \"cancel\" to cancel the AdminSign deletion.");
    62. return true;
    63. }
    64. }
    65. }else{
    66. sender.sendMessage("You have to be a player to use /adminsign!");
    67. return true;
    68. }
    69. return true;
    70. }
    71. return false;
    72. }
    73.  
    74. @EventHandler (ignoreCancelled = true)
    75. public void signManage(PlayerInteractEvent e){
    76. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    77. if(e.getClickedBlock().getType() == Material.SIGN_POST || e.getClickedBlock().getType() == Material.WALL_SIGN || e.getClickedBlock().getType() == Material.SIGN){
    78. Player p = e.getPlayer();
    79. if(add.contains(p.getName())){
    80. Sign s = (Sign)e.getClickedBlock().getState();
    81. sh.addSign(s);
    82. add.remove(p.getName());
    83. p.sendMessage(ChatColor.GREEN + "[AdminSign] AdminSign successfully created!");
    84. }
    85. if(del.contains(p.getName())){
    86. System.out.println("cancel");
    87. Sign s = (Sign)e.getClickedBlock().getState();
    88. sh.removeSign(s);
    89. del.remove(p.getName());
    90. p.sendMessage(ChatColor.GREEN + "[AdminSign] AdminSign successfully removed!");
    91. }
    92. }
    93. }
    94. }
    95.  
    96. @EventHandler
    97. public void cancel(AsyncPlayerChatEvent e){
    98. if(e.getMessage().equalsIgnoreCase("cancel")){
    99. Player p = e.getPlayer();
    100. if(add.contains(p.getName())){
    101. add.remove(p.getName());
    102. p.sendMessage(ChatColor.GREEN + "[AdminSign] AdminSign creation cancelled.");
    103. e.setCancelled(true);
    104. }
    105. if(del.contains(p.getName())){
    106. del.remove(p.getName());
    107. p.sendMessage(ChatColor.GREEN + "[AdminSign] AdminSign deletion cancelled.");
    108. e.setCancelled(true);
    109. }
    110. }
    111. }
    112. }
    113.  
     
  2. Offline

    RealDope

    Code:JAVA
    1.  
    2. locs.add(location);
    3. yml.set("locations", locs);
    4. yml.set("locations", yml.getStringList("locations").add(location));
    5.  

    This code is redundant, unless you mean to add the location twice, and if so, why are you putting it in the config, only to immediately get it out to add the location again?

    Anyways, your problem is on the 3rd line of this code, List.add() returns a boolean for whether or not the list was changed, because it is changed in your case, it returns true, and then writes that to config.
     
  3. Offline

    krazytraynz


    Sorry about the redundant code, I was using that earlier to make sure I could write something to the config and I guess I forgot to delete it. Thanks for the help :D
     
Thread Status:
Not open for further replies.

Share This Page