Solved Get just the worlname [HELP]

Discussion in 'Plugin Development' started by kmccmk9, Jun 20, 2013.

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

    kmccmk9

    Hello, the goal here is to get the worlname but just the world name. Getting it right now says: craftworld: world. But I just want the world part. I tried doing a substring but that didn't work at all.

    This is the code I'm using just to try and get it to make a substring at all. Thanks for your help.

    Code:
    String worldname = player.getLocation().getWorld().getName().toString();
    while (i < total)
    {
    if (players[i].isOp() || players[i].hasPermission("helpmeadvanced.admin")) {
    players[i].sendMessage(ChatColor.DARK_RED + player.getDisplayName() + ChatColor.DARK_GREEN + " needs some help!" + " (" + worldname.substring(17,20) + player.getLocation().getBlockX() + ',' + player.getLocation().getBlockY() + ',' + player.getLocation().getBlockZ() + ") Description: " + description);  
    }
    i++;
    } 
    
     
  2. Offline

    Gonzxor

    No clue im new at plugins
     
  3. Offline

    kmccmk9


    That's okay thanks. Ya its weird I can get the world but that function returns: craftworld=world. So I thought to use a substring but that doesn't seem to work at all.
     
  4. Offline

    Gonzxor

    Ya sorry, I only commented to bump the post and show that someone is actually reading your post.
     
  5. Offline

    kmccmk9


    Thanks. I appreciate it...
     
  6. Offline

    Rocoty

    Err....whyever do you call .toString() on the name string that you've already gotten?

    It's like opening an envelope and then seal it again to try and retrieve the message
     
  7. Offline

    kmccmk9


    Ya that was my other attempt. My though was that doing .toString() would possibly remove the extra data that was coming through without it. But even so, shouldn't the substring have worked either way?
     
  8. Offline

    Rocoty

    Not the way you are doing it. You are trying to reach an index in the string that doesn't exist (17). I think if you did substring(12), it would have worked. But either way, getName() returns the name of the world, plainly, no more, no less.
     
  9. Offline

    kmccmk9


    Really? Cause when I did that origianlly it returned {CraftWorld = World} on my screen
     
  10. Offline

    Rocoty

    You sure you didn't do getWorld().toString();?

    try again please? getWorld().getName();
     
  11. Offline

    kmccmk9


    Sure no problem, compiling now.

    EDIT:
    Still did not work. Would you like pictures?


    Okay now this is really odd. I added this line of code and the server didn't broadcast anything

    Code:java
    1. String worldname = player.getLocation().getWorld().getName();
    2. server.broadcastMessage(worldname);


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

    Rocoty

    That's weird. What is your whole code?

    By the way, Entity has a getWorld() method, so you can change it to
    Code:
    player.getWorld().getName();
     
  13. Offline

    kmccmk9


    This is my code posted on pastebin
     
  14. Offline

    Rocoty

    And do you get an error?
     
  15. Offline

    kmccmk9


    None
     
  16. To divide "craftworld: world" into just "world" you could try this.(untested)
    Code:
    public String[] results = new String[2];
     
    results = player.getWorld().getName().split(":");
     
    results[1] = results[1].trim();
    
    Then results[1] would be "world"
     
  17. Offline

    kmccmk9


    Oh interesting way to do it. I can give that a shot.

    EDIT:
    Still not fixed. Why isn't this working, it doesn't make any sense.
     
  18. What isn't working about it?
     
  19. Offline

    kmccmk9

    It is still displaying the {CraftWorld=world}
     
  20. Oh well then try this.
    Code:
           
    String[] results = new String[2];
           
    results = player.getWorld().getName().split("=");
               
    results[1].replace("}", " ");
           
    results[1].trim();
    
    Results[1] should now be "world"
     
  21. Offline

    kmccmk9


    Nope note even that. The exact line is
    {Craftworld{name=world}}
     
  22. Okay. Someone already told you this but don't use toString on something that is already a string. But if player.getWorld().getName() returns {Craftworld{name=world}} then you need to just use some string methods like I showed before hand to get it to give you the world. Like this.
    Code:
            worldname.replaceAll("{Craftworld{name=", "");
            worldname.replaceAll("}}", "");
    If this doesn't work there is definelty something I'm not understanding.
    P.S.If it gives you an error for having "" and nothing inside just put a space in there and then do worldname.trim();
    P.P.S. you could also try player.getWorld().getWorldFolder().getName(); which would return the name of the world's folder which should be its ingame name. Actually that would probley be better than the other methods....
     
  23. Offline

    kmccmk9


    Okay, ya there is no error and there is no toString anymore. I will try and use your code now.

    EDIT:

    Still not working. This is exactly what I'm getting.
     
  24. Please try using player.getWorld().getWorldFolder().getName()

    If that doesn't work stilllllllllllll then could you post your code(If it's not too much) into here so I can see it all because I can't load your original paste bin post and my computer in general doesn't like loading paste bin stuff for some reason.
     
  25. Offline

    kmccmk9


    Sure here it is:

    Code:java
    1. package com.kmccmk9.HelpMeAdvanced;
    2.  
    3. // All the imports
    4. import java.io.BufferedWriter;
    5. import java.io.File;
    6. import java.io.FileWriter;
    7. import java.io.IOException;
    8. import java.util.ArrayList;
    9. import java.util.Hashtable;
    10. import java.util.Scanner;
    11.  
    12. import org.bukkit.ChatColor;
    13. import org.bukkit.Location;
    14. import org.bukkit.World;
    15. import org.bukkit.command.Command;
    16. import org.bukkit.command.CommandSender;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.plugin.PluginDescriptionFile;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. import org.bukkit.Server;
    22.  
    23. /**
    24. * HelpMeAdvanced for Bukkit
    25. *
    26. * @author kmccmk9
    27. */
    28.  
    29. //Starts the class
    30. public class HelpMeAdvanced extends JavaPlugin{
    31. Server server;
    32. String variable;
    33. Scanner msgs;
    34. String description;
    35. ArrayList<String> messagestoprint = new ArrayList<String>();
    36. Hashtable<String, String> playerdescriptions = new Hashtable<String, String>();
    37. Hashtable<String, World> playerlocations = new Hashtable<String, World>();
    38. ArrayList<String> playernames = new ArrayList<String>();
    39. Location gotolocation;
    40. Player gotoplayer;
    41. int counter = 1;
    42. int number;
    43. String numberstring;
    44. int arraylength;
    45.  
    46. //Define File Variables
    47. static String mainDirectory = "plugins/HelpMeAdvanced"; //sets the main directory for easy reference
    48. static File messages = new File(mainDirectory + File.separator + "messages.txt"); //the file separator is the / sign, this will create a new Zones.dat files in the mainDirectory variable listed above, if no Zones directory exists then it will automatically be made along with the file.
    49. // onDisable
    50. public void onDisable() {
    51. PluginDescriptionFile pdfFile = this.getDescription();
    52. System.out.println( pdfFile.getName() + " is disabled!" );
    53. }
    54. // onEnable
    55. public void onEnable() {
    56. server = this.getServer();
    57. PluginDescriptionFile pdfFile = this.getDescription();
    58. System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
    59. //Setup Files
    60. new File(mainDirectory).mkdir(); //makes the Zones directory/folder in the plugins directory
    61. if(!messages.exists()){ //Checks to see if the zones file exists, defined above, if it doesn't exist then it will do the following. the&nbsp;! turns the whole statement around, checking that the file doesn't exist instead of if it exists.
    62. try { //try catch clause explained below in tutorial
    63. messages.createNewFile(); //creates the file zones.dat
    64. FileWriter writer = new FileWriter(messages,true);
    65. BufferedWriter out = new BufferedWriter(writer);
    66. out.write("An Admin or SuperAdmin will come and help you soon as possible.");
    67. out.newLine();
    68. out.write("If no one come and helps you within 5 minutes, perform the command again.");
    69. out.newLine();
    70. out.write("Message sent to Admins");
    71. out.close();
    72. } catch (IOException ex) {
    73. ex.printStackTrace(); //explained below.
    74. }
    75.  
    76. } else {
    77.  
    78. openFile();
    79. readFile();
    80. closeFile();
    81.  
    82. }
    83. }
    84.  
    85. private void openFile() {
    86. try{
    87. msgs = new Scanner(messages);
    88. }catch (Exception ex){
    89. System.out.println("[HelpMeAdvanced] Could not find messages.txt, did you delete it?");
    90. }
    91. }
    92.  
    93. private void readFile() {
    94. if (!msgs.hasNextLine()){ //Check if there are no lines at all
    95. server.broadcastMessage("[HelpMeAdvanced] Messages.txt is empty.");
    96. }else{
    97. messagestoprint.clear(); //Clear the ArrayList (For using the reload command)
    98. while (msgs.hasNextLine()){
    99. String messagetofile = msgs.nextLine(); //Set the next line to a string
    100. messagetofile = messagetofile.replaceAll("(&([a-f0-9]))", "\u00A7$2"); //Replace colour codes
    101. messagestoprint.add(messagetofile); //Add to the ArrayList
    102. }
    103. }
    104. }
    105.  
    106. private void closeFile() {
    107. msgs.close();
    108. }
    109.  
    110. public String arrayToString(String[] array) {
    111. StringBuilder sb = new StringBuilder();
    112. for (int a = 1; a < array.length; a++) {
    113. sb.append(array[a]);
    114. sb.append(" ");
    115. }
    116. String newString = sb.toString();
    117. return newString;
    118. }
    119.  
    120. // Command system
    121. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    122. Player[] players;
    123. int total;
    124. if (sender instanceof Player) {
    125. Player player = (Player) sender;
    126. if(commandLabel.equalsIgnoreCase("helpme")) {
    127. if (args.length == 1)
    128. {
    129. variable = args[0];
    130. if (variable.equalsIgnoreCase("help"))
    131. {
    132. player.sendMessage("Typing in \"/helpme will alert Admins that you need help!\"");
    133. player.sendMessage("Typing in \"/helpme description \"Description here\" (Without quotes) will alert Admins that you need help and add a description");
    134. player.sendMessage("Typing in \"/helpme reload will reload the messages file\"");
    135. player.sendMessage("Typing in \"/helpme list will list the players that need help if you are the admin\"");
    136. player.sendMessage("Typing in \"/helpme goto \"Number from list here\" (Without quotes) will teleport you to the person in need and remove them from the list\"");
    137. player.sendMessage("Typing in \"/helpme remove \"Number from list here\" (Without quotes) will remove that player from the list\"");
    138. }
    139. if (variable.equalsIgnoreCase("description"))
    140. {
    141. player.sendMessage(ChatColor.DARK_RED + "Missing description. Please add a one word description.");
    142. }
    143. if (variable.equalsIgnoreCase("reload"))
    144. {
    145. readFile();
    146. }
    147. if (variable.equalsIgnoreCase("list"))
    148. {
    149. for (int i=0;i<playernames.size();i++)
    150. {
    151. player.sendMessage(ChatColor.GREEN + String.valueOf(counter) + '.' + " " + playernames.get(i).toString() + '-' + " " + playerlocations.get(playernames.get(i).toString()) + " " + '-' + " " + playerdescriptions.get(playernames.get(i).toString()));
    152. counter++;
    153. }
    154. counter = 1;
    155. }
    156. if (!variable.equalsIgnoreCase("help") && !variable.equalsIgnoreCase("description") && !variable.equalsIgnoreCase("reload") && !variable.equalsIgnoreCase("list"))
    157. {
    158. player.sendMessage(ChatColor.DARK_RED + "Unrecognized argument. Typing in /helpme will alert Admins that you need help!");
    159. }
    160. }
    161. else if (args.length > 1)
    162. {
    163. if (variable.equalsIgnoreCase("description"))
    164. {
    165. description = arrayToString(args);
    166. this.getServer().broadcastMessage(description);
    167. }
    168. //Add Players to List
    169. playerlocations.put(player.toString(), player.getLocation().getWorld());
    170. playerdescriptions.put(player.toString(), description);
    171. playernames.add(player.toString());
    172. //Degbug
    173. //Dispatch Message
    174. for (int j = 0;j < arraylength;j++)
    175. {
    176. player.sendMessage(messagestoprint.get(j));
    177. }
    178. players = server.getOnlinePlayers();
    179. total = players.length;
    180. int i = 0;
    181. String worldname = player.getWorld().getWorldFolder().getName();
    182.  
    183. worldname.replaceAll("{Craftworld{name=", "");
    184. worldname.replaceAll("}}", "");
    185.  
    186. server.broadcastMessage(worldname);
    187. while (i < total)
    188. {
    189. if (players[i].isOp() || players[i].hasPermission("helpmeadvanced.admin")) {
    190. players[i].sendMessage(ChatColor.DARK_RED + player.getDisplayName() + ChatColor.DARK_GREEN + " needs some help!" + " (" + worldname + player.getLocation().getBlockX() + ',' + player.getLocation().getBlockY() + ',' + player.getLocation().getBlockZ() + ") Description: " + description);
    191. }
    192. i++;
    193. }
    194. if (variable.equalsIgnoreCase("goto"))
    195. {
    196. numberstring = arrayToString(args);
    197. number = Integer.parseInt(numberstring);
    198. gotoplayer = server.getPlayer(playernames.get(number));
    199. gotolocation = gotoplayer.getLocation();
    200. playerlocations.remove(playernames.get(number));
    201. playerdescriptions.remove(playernames.get(number));
    202. player.teleport(gotolocation);
    203. }
    204. if (variable.equalsIgnoreCase("remove"))
    205. {
    206. numberstring = arrayToString(args);
    207. number = Integer.parseInt(numberstring);
    208. playerlocations.remove(playernames.get(number));
    209. }
    210. }
    211. else if (args.length == 0)
    212. {
    213. //Debug
    214. arraylength = messagestoprint.size();
    215. //Add Player to list
    216. playerlocations.put(player.toString(), player.getLocation().getWorld());
    217. playerdescriptions.put(player.toString(), " ");
    218. playernames.add(player.toString());
    219. for (int j = 0;j < arraylength;j++)
    220. {
    221. player.sendMessage(messagestoprint.get(j));
    222. }
    223. players = server.getOnlinePlayers();
    224. total = players.length;
    225. int i = 0;
    226. while (i < total)
    227. {
    228. if (/*HelpMeAdvanced.permissionHandler.has(players[i], "helpmeadvanced.admin") ||*/ players[i].isOp() || players[i].hasPermission("helpmeadvanced.admin")) {
    229. players[i].sendMessage(ChatColor.DARK_RED + player.getDisplayName() + ChatColor.DARK_GREEN + " needs some help!" + " (" + player.getLocation().getWorld() + player.getLocation().getBlockX() + ',' + player.getLocation().getBlockY() + ',' + player.getLocation().getBlockZ() + ")");
    230. }
    231. i++;
    232. }
    233. }
    234. }
    235. }
    236. return true;
    237. }
    238. }[/i][/i][/i][/i][/i][/i][/i]
     
  26. Offline

    ProtoTempus

    kmccmk9

    This line (161 )could be false...
    Code:java
    1. else if (args.length > 1)
    2. //maybe you're looking for "args.length >= 1" ??
    3.  


    and passing the check onto line (211)...
    Code:java
    1. else if (args.length == 0)


    And then that runs... (lines 226 - 232)
    Code:java
    1. while (i < total)
    2. {
    3. if (/*HelpMeAdvanced.permissionHandler.has(players[I], "helpmeadvanced.admin") ||*/ players[I].isOp() || players[I].hasPermission("helpmeadvanced.admin")) {[/I][/I][/I]
    4. [I]players[I].sendMessage(ChatColor.DARK_RED + player.getDisplayName() + ChatColor.DARK_GREEN + " needs some help!" + " (" + player.getLocation().getWorld() + player.getLocation().getBlockX() + ',' + player.getLocation().getBlockY() + ',' + player.getLocation().getBlockZ() + ")");[/I][/I]
    5. [I]}[/I]
    6. [I]}[/I]

    Which has the bad code on line 229:

    Code:java
    1. player.getLocation().getWorld()
    2.  
    3. //This will output "{CraftWorld=world}"


    Change that to
    Code:java
    1. player.getWorld().getName()


    And see what happens.
     
  27. Offline

    foodyling

    Use player.getWorld().getName() not player.getLocation().getWorld().getName().
     
  28. Offline

    ProtoTempus

    Oops... Changing that...
     
  29. Offline

    coobro123

    should be easy
    Code:
    p.sendMessage(Bukkit.getWorld("worldname"));
    
     
  30. Offline

    foodyling

    This returns that actual World object itself, which is not a string by any means and implies he already has the world name itself, while the goal is to obtain the worldname.
     
Thread Status:
Not open for further replies.

Share This Page