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

    coobro123

    So, you want to get the name of the players world? Or a certain world? I am still not sure what u mean
    Code:
    p.sendMessage(Bukkit.getWorld("worldname").getName);
    p.sendMessage(p.getWorld().getName());
    They return string types
     
  2. Offline

    foodyling

    The goal is to get the name of the world that the player is in, which is simply just player.getWorld().getName();
     
    coobro123 likes this.
  3. Offline

    ProtoTempus

    Did you guys read the thread? He has tried that... I think some of his else if statements are wrong and he is running a different bit of code than he thinks he it (the last else if rather than the first).

    He was already told player.getWorld().getName() and apparently tried it.
     
  4. Offline

    kmccmk9





    Okay thanks everyone. I will go through this code step by step right now and try the changes mentioned.

    Okay I made all requested changes. New code can be found in this post. However, I now get null as the name. I have no idea why.

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


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

    Rprrr

    kmccmk9
    Can you please just post the relevant code?
     
  6. Offline

    kmccmk9


    Sorry all along everyone else asked for the whole code so I figured I would post the whole code again. Well the relevant lines would be

    The /i's shouldn't be there. They're then when I pasted for some reason.

    worldname is global and define as
    Code:
    player.getWorld().getName();
    Code:java
    1. while (i < total)
    2. {
    3. if (players[I].isOp() || players[I].hasPermission("helpmeadvanced.admin")) {[/I][/I]
    4. [I] 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);[/I][/I]
    5. [I] }[/I]
    6. [I] i++;[/I]
    7. [I] }[/I]
     
  7. Offline

    foodyling

    Well, one of those ways might work (last one should), but if one fails beforehand the next one wont be reached at all. Try each method individually.
     
  8. Offline

    Rprrr

    kmccmk9
    Ehm, you're trying to send every online OP a message?
    I wouldn't recommend using such a loop, it can be done way easier and cleaner:

    Code:
        for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()){
         
            if (onlinePlayer.isOp()){
             
                onlinePlayer.sendMessage(ChatColor.GOLD + "Hi! You're an operator and you're online. Awesome!");
             
            }
         
        }
    Also, for displaying the world's name:

    Code:
        if (args[0].equals("world")){
           
            String worldName = p.getWorld().getName();
           
            p.sendMessage(ChatColor.GOLD + "You are in world '" + ChatColor.GRAY + worldName + ChatColor.GOLD + "'. Have a nice day.");
                                   
        }
     
    foodyling likes this.
  9. Offline

    kmccmk9


    Aha I believe I have found it. Making quick change now.
     
  10. Offline

    foodyling

    Which method worked?
     
  11. Offline

    kmccmk9


    Thanks for the suggestion


    Well I found out that worldname was only being set to something in one of the methods. Stupid mistake that I made...that I just couldn't see.

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

    coldandtired

    Which is why you shouldn't use global variables unless you absolutely have to :)
     
  13. Offline

    ProtoTempus

    kmccmk9 Great so I was ish right... Just didn't notice worldname wasn't set.
     
Thread Status:
Not open for further replies.

Share This Page