Getting a world from a config.

Discussion in 'Plugin Development' started by CraftCreeper6, May 28, 2014.

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

    CraftCreeper6

    Hai! So I need to get a world from a config but everytime I try I get this:
    Code:
    Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.
    bukkit.World
    Code (Little bit messy):
    Code:java
    1. package me.CraftCreeper6.main;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.block.Sign;
    10. import org.bukkit.command.Command;
    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.PlayerInteractEvent;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18. import org.bukkit.util.Vector;
    19.  
    20. public class Main extends JavaPlugin implements Listener {
    21.  
    22. public void onEnable() {
    23.  
    24. System.out.print("[HubblePort] Enabled!");
    25.  
    26. getServer().getPluginManager().registerEvents(this, this);
    27.  
    28. }
    29.  
    30. public void onDisable() {
    31.  
    32. System.out.print("[HubblePort] Disabled!");
    33.  
    34. }
    35.  
    36. public boolean onCommand(CommandSender sender, Command cmd,
    37. String commandLabel, String[] args) {
    38. Player p = (Player) sender;
    39.  
    40. if (commandLabel.equalsIgnoreCase("HP")
    41. && args[0].equalsIgnoreCase("Set") && args[1] != null
    42. && args[2] != null && args[3] != null && args[4] != null
    43. && args[5] != null) {
    44.  
    45. getConfig().set(args[1] + ".World", args[2]);
    46. getConfig().set(args[1] + ".X", args[3]);
    47. getConfig().set(args[1] + ".Y", args[4]);
    48. getConfig().set(args[1] + ".Z", args[5]);
    49.  
    50. saveConfig();
    51.  
    52. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.RED + "HP"
    53. + ChatColor.BLUE + "]" + ChatColor.AQUA
    54. + " Succesfully set location for: " + args[1]
    55. + " to - World: " + args[2] + " X: " + args[3] + " Y: "
    56. + args[4] + " Z: " + args[5]);
    57.  
    58. }
    59.  
    60. return true;
    61. }
    62.  
    63. public void tP(Player p, World world, int x, int y, int z) {
    64.  
    65. Location loc = new Location(world, x, y, z);
    66.  
    67. p.teleport(loc);
    68.  
    69. }
    70.  
    71. @EventHandler
    72. public void onPlayerInteract(PlayerInteractEvent event) {
    73. final Player p = event.getPlayer();
    74. Block block = event.getClickedBlock();
    75.  
    76. if (block != null && block.getType() == Material.SIGN_POST
    77. || block != null && block.getType() == Material.WALL_SIGN) {
    78. Sign sign = (Sign) block.getState();
    79. Action action = event.getAction();
    80. if (action == Action.RIGHT_CLICK_BLOCK) {
    81. if (sign.getLine(0).equalsIgnoreCase("[HP]")
    82. || sign.getLine(0).equalsIgnoreCase(
    83. ChatColor.BLUE + "[" + ChatColor.RED + "HP"
    84. + ChatColor.BLUE + "]")) {
    85.  
    86. sign.setLine(0, ChatColor.BLUE + "[" + ChatColor.RED + "HP"
    87. + ChatColor.BLUE + "]");
    88. sign.update();
    89.  
    90. if (p.hasPermission("HP.Create")) {
    91.  
    92. p.sendMessage(ChatColor.BLUE
    93. + "["
    94. + ChatColor.RED
    95. + "HP"
    96. + ChatColor.BLUE
    97. + "]"
    98. + ChatColor.AQUA
    99. + " Remember to set the destination using: /HP set [Name] [World, X , Y, Z]");
    100.  
    101. }
    102.  
    103. final int x = getConfig().getInt(sign.getLine(1) + ".X");
    104. final int y = getConfig().getInt(sign.getLine(1) + ".Y");
    105. final int z = getConfig().getInt(sign.getLine(1) + ".Z");
    106. final World world = (World) getConfig().get(sign.getLine(1) + ".World");
    107. // HERE IS WHERE IT GETS THE WORLD FROM THE CONFIG
    108.  
    109. p.setVelocity(p.getLocation().getDirection().multiply(-3));
    110. p.setVelocity(new Vector(p.getVelocity().getX(), 9.0D, p
    111. .getVelocity().getZ()));
    112.  
    113. Bukkit.getScheduler().scheduleSyncDelayedTask(this,
    114. new Runnable() {
    115.  
    116. @Override
    117. public void run() {
    118.  
    119. tP(p, world, x, y, z);
    120.  
    121. p.sendMessage(ChatColor.BLUE + "["
    122. + ChatColor.RED + "HP"
    123. + ChatColor.BLUE + "]"
    124. + ChatColor.AQUA
    125. + " You have been teleported!");
    126.  
    127. }
    128.  
    129. }, 40);
    130.  
    131. } else {
    132.  
    133. }
    134. }
    135. }
    136. }
    137.  
    138. }


    Any help appreciated :)
     
  2. Offline

    mythbusterma

    You are trying to cast two incompatible types on to each other, instead of going (World).... do Bukkit.getServer.getWorld(<name>).
     
  3. Offline

    CraftCreeper6

    mythbusterma
    Thanks! Works awesome! Exept for the fact that I get tp'ed into the void :( Any help? :)

    mythbusterma
    Figured out why but not sure how to fix it. http://prntscr.com/3nlq1y Can you help? :)

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

    mythbusterma

    Perhaps messing with the player's velocity is doing that, I can't see anything obvious that would cause that.
     
  5. Offline

    CraftCreeper6

  6. Offline

    AronTheGamer

    Javadocs said:
    World getServer().getWorld( String name );
     
  7. Offline

    CraftCreeper6

    mythbusterma AronTheGamer
    I found out why this is my config when I type the command:

    Code:
    TnT:
      World: Main
      X: '100'
      Y: '70'
      Z: '100'
    
    As you see the int has a ' at either side that is making it get a String rather than a int thus making me fall through the void. Does anyone know a fix for this?
     
  8. CraftCreeper6
    You're storing strings instead of integers. In your command where you have:
    Code:java
    1. getConfig().set(args[1] + ".World", args[2]);
    2. getConfig().set(args[1] + ".X", args[3]);
    3. getConfig().set(args[1] + ".Y", args[4]);
    4. getConfig().set(args[1] + ".Z", args[5]);

    Do
    Code:java
    1. getConfig().set(args[1] + ".World", args[2]);
    2. getConfig().set(args[1] + ".X", Integer.parseInt(args[3]));
    3. getConfig().set(args[1] + ".Y", Integer.parseInt(args[4]));
    4. getConfig().set(args[1] + ".Z", Integer.parseInt(args[5]));


    To avoid errors do a check on the arguments to make sure they're integers before changing them from string to integers.
    EDIT:
    Check this for the last thing I said
    http://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java
     
Thread Status:
Not open for further replies.

Share This Page