[Solved] How do you define a new Location with a set X, Y and Z?

Discussion in 'Plugin Development' started by macwinexpert, Jun 10, 2012.

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

    macwinexpert

    Hey Guys!

    This is probably really noobish, but how do you define a new Location with a given X, Y, and Z in a world??

    I tried first defining them as null, but that doesn't work :(

    This is what I have so far:

    Code:
    Location cube1 = null;
    Location cube2 = null;
     
    cube1.setX(123); cube1.setY(105); cube1.setZ(-118);
    cube2.setX(130); cube2.setY(105); cube2.setZ(-137);
    You might have guessed, but I'm very new to Java :p Any help??


    Many thanks in advance!

    Hamish (macwinexpert)
     
  2. Offline

    V10lator

  3. Offline

    macwinexpert

    That is exactly what I was hoping people wouldn't reply with! I saw this before I posted this thread, but didn't understand how to implement it! Could you just show me an example?


    Thanks again!

    Hamish (macwinexpert)
     
  4. Offline

    I_am_not_funny

    Yeah, you should really learn more java.
     
  5. Offline

    r0306

    Code:
    Location location = "your location";
    int x = location.getBlockX();
    int y = location.getBlockY();
    int z = location.getBlockZ();
    World w = location.getWorld();
    Location loc = new Location(w, x, y, z); //defines new location
     
  6. Offline

    macwinexpert

    Ok thanks, I understand all of that except for the first line, what is "your location"??
     
  7. Offline

    Suprem20

    The location of the thing you want, for example, the location of a player, block, etc...

    So:

    Code:
    Location location = player.getlocation();
     
  8. Offline

    macwinexpert

    That's not what I want to do though, I want to define a new location with a set X, Y and Z, not the location of something already defined like a player!
     
  9. Offline

    Suprem20

    Code:
        public void tpPlayerToSetLocation (Player p) {
           
          Block b = getServer().getWorld("world").getBlockAt(120, 100, -65);
           
            if (b != null) {
               
                p.teleport(b.getLocation());
            }
        }
     
  10. Offline

    V10lator

    macwinexpert All you need is in this thread,even a code example that shows exactly how to do it.
    All you need now is to understand the very simplest java basics to be able to pick out the infomation you need.
     
    MCForger likes this.
  11. Offline

    MCForger

    I'm not going to lie, I know a ton of Java and bukkit but I am also struggling in this area. I'm trying to make an arena plugin but I'm having trouble with how should a player select four points and then how do I make that cube and save it so when the server turns off it still remembers?
    I have tried to do it myself but I don't have enough experience with making cubiods.
     
  12. Offline

    macwinexpert

    Thanks very much, that's exactly what I was looking for! :)
     
  13. Offline

    kumpelblase2

    There's a lot easier way - and probably also less cpu expensive:
    Code:
    public void tpPlayerToSetLocation(Player p)
    {
        Location l = new Location(Bukkit.getWorld("world"), 120, 100, -65);
        p.teleport(l);
    }
     
    gaspy likes this.
  14. Offline

    macwinexpert

    Thanks, that's much better :) But you should probably do player.getWorld() instead, so it works no matter which world the player is in!
     
  15. Offline

    V10lator

    Which is exactly what I told in the first reply:
    And what was even shown in a example:
    Hence why I sayed:
    It's not always good to give copy&pasteable code.

    MCForger Cuboid creation/using/saving/loading is another topic and was disused many times, please use the search function.
     
  16. Offline

    macwinexpert

    Thanks for your help, but I just didn't understand how to implement the example on the Bukkit api page!

    But I understand now, thank you :)
     
  17. Offline

    kumpelblase2

    I didn't read the whole thread which I should probably have done. The reason I just posted that, without reading the other replies, is because I saw the complicated code by Suprem20 and if someone already provided code, it doesn't make that much of a difference if you now post a different approach.
    True, but is there a problem 'correcting' the code already posted? I'm trying not to give just code, but also try to explain the things that should be done. And in most cases no code at all. The problem is most people just wait until someone posts a piece of code, they copy it and see if it works. It doesn't matter how good you made an explanation in an earlier post.
     
    V10lator likes this.
  18. Offline

    Adamki11s

    Ok well if you're going to plan on making a cube, you don't want to allow them to set 4 points, unless you want to add polygon support which gets tricky. All you want to do is get them to set two locations, teh top and bottom corners on opposite corners and then you can link them up manually.

    For my regios plugin I monitor the interaction event and then if the player left clicks I store that location in one hashmap, if they right click i store that in a separate one, then when they create the region I check both the hashmaps and link them together so to speak. If you have any problems 'creating' cubes such as structures in the world as such feel free to send me a PM. I have loads of experience with that kind of stuff :p
     
  19. Offline

    macwinexpert

    Yea, sorry guys! I do understand quite a bit of Java, and may of made myself sound a bit more noobish than I actually am, I just realised now that I completely mis-understood the bukkit api page, and tried to make things more complex then they actually are :L But thanks for helping me :)

    It's just the way that Java handles variables that confuses me, becuase to me saying:

    Location location = null;
    location.setX(x); location.setY(y); location.setZ(z);

    To me that means:

    1) Create a blank variable called "location"
    2) Set its x,y,z to be the following...


    But Java doesn't do it that way!
     
  20. Offline

    Norbo11

    No, it doesnt mean that. You are creating a Location object and setting it to null. Then you are trying to access a location object that is null, and then trying use it's methods! You need to actually MAKE the object first, so:

    Location location = new Location(x, y, z);

    Then you can use the object's methods if you need to.
     
  21. Offline

    Digi

    Norbo11
    Location location = new Location(world, x, y, z);

    macwinexpert
    To get the location you must have it from where you got the x/y/z in the first place... you should store the world's name and then retrieve it by using Bukkit.getWorld(worldName); to be used in the world arg at the Location object.
     
  22. Offline

    MCForger

    I pmed you adam
     
  23. Offline

    Adamki11s

    Got it

    Nope, because what you are actually doing is saying that location = 'nothing'

    And then you are setting location.setX(x), but location still = 'nothing', as V10later pointed out you need to create an instance before you can use the variable using the constructor.

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

    messageofdeath

    PHP:
    Location location = new Location(String worldint xint yint z);
    // I think those are the correct params
     
  25. Offline

    kumpelblase2

    Nope, first parameter isn't a string, it's a world.
     
  26. Offline

    V10lator

    Means:
    You think you understand quite a bit of Java, and you think you are less noobish than you sound like. You just realized that you mis-understood the bukkit API page, but what you still need to realize is that this was just because of you're lack of basic Java knowledge. I really don't want to sound rude, but if you had googled for the terms in my first reply ("create new instance". "create/use a constructor", ...) I'll bet you had found tutorials where you learn many thinks you still don't know. What if you need to write (and of course use) constructors for your own classes? You still don't know how...
    That's completely wrong and shows again that you're seriously lacking Java (and programming in general, which language allows to work on empty (null) variables/references?) basics.
    null = nothing, nada, niente, ...
    Now with Location location = null;
    you say the reference to a Location called "location" should point to nothing.
    Then: location.setX(x); - Now set X on the Location called "location" ... at this point you should realize that it's impossible to set X at nothing.
    Surely not. That would be just crazy.

    Also, you learned that
    is better than
    while both teleport the player to the same coordinates the first one is less CPU intensive, but you can't answer why, do you? Now ask yourself why you can't answer it: You have to learn! So please, please read (or watch if you like videos more) tutorials, maybe search yourself somebody who's willing to teach Java to you, ...
    Again: I don't want to sound rude, I just want you to open your eyes and realize that you have to learn more before you start to write (productive) code.
     
    ferrybig likes this.
  27. Offline

    macwinexpert

    Ok guys, used the constructor to set up a new object called cube1 and cube2, but when I try to change the block type, it doesn't work :/

    If you are wondering, I am in the middle of making a custom map, that will have a plugin that creates emancipation grills using iron bars, which I have got to work so far :) But in one particular test, if the player walks through the grill with cubes (sand) in their inventory, I need the piston cube dispensers to be re-filled.

    The if statements in the code works correctly as I get sent the messages "Did get activated" and "no.1 and no.2", however when the block tries to get changed, on cube1, the block of sand gets placed one block away from the defined location! With cube2, no block of sand is placed :/

    What am I doing wrong?

    Code:
    void replenish(Player player) {
       
            if(player.getInventory().contains(Material.SAND) && player.getLocation().getX() > 139 && player.getLocation().getX() < 170 && player.getLocation().getZ() < -114 && player.getLocation().getZ() > -135) {
           
                Location cube1 = new Location(player.getWorld(), 123, 105, -118);
                Location cube2 = new Location(player.getWorld(), 130, 105, -137);
           
                player.sendMessage("Did get activated");
           
                    if(player.getInventory().contains(Material.SAND, 2)) {
                   
                        cube1.getBlock().setType(Material.SAND);
                        cube2.getBlock().setType(Material.SAND);
                   
                        player.sendMessage("no.1 and no.2");
                    }
               
                    else if(player.getInventory().contains(Material.SAND, 1)) {
                   
                        if(cube1.getBlock().getType() == Material.AIR) {cube1.getBlock().setType(Material.SAND); player.sendMessage("no.1");}
                        else if(cube2.getBlock().getType() == Material.AIR) {cube2.getBlock().setType(Material.SAND); player.sendMessage("no.2");}
                }
            }
       
        }

    Thanks for any help in advance!

    Hamish (macwinexpert)

    Again, thanks for all you guys help!

    I actually was really close to having it correct, I guessed: Location cube1 = Location(world, x, y, z), but that chucked up an error saying to create a method for it, which isn't what I wanted to do!

    All I was missing was the "new"!


    Also, I do understand why:

    Code:
    public void tpPlayerToSetLocation(Player p)
    {
        Location l = new Location(Bukkit.getWorld("world"), 120, 100, -65);
        p.teleport(l);
    }
    
    is better than:

    Code:
    public void tpPlayerToSetLocation (Player p) {
     
          Block b = getServer().getWorld("world").getBlockAt(120, 100, -65);
     
            if (b != null) {
         
                p.teleport(b.getLocation());
            }
        }
    As the first method is simply defining a location and teleporting the player to it, while the second is asking the server to get the block at that specific location, then get the location of the block and teleport the player to it!

    Hope this demonstrates to you that I do know a small bit of Java :)


    Hamish (macwinexpert)

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

    V10lator

    It that case getBlockAt() would be less resource intensive, also it may fix your bug (or your coordinates are wrong), so:
    Block block1 = player.getWorld().getBlockAt(123, 105, -118);
    Block block2 = player.getWorld().getBlockAt(130, 105, -137);

    Cause you don't wanted to access a method called Location but the constructor of the object Location. Bukkits docs clearly tell that this is a constructor, no function.
    Cause you didn't even know what a constructor is, hence you didn't know that it's used to create a new instance of the object.
    Well, no.
    world.getBlockAt(x, y, z); is getting the block information directly out of the chunk based on the coordinates.
    block.getLocation() takes the location and the world of the block and returns new Location(world, x, y, z); internally.
    So using new Location(world, x, y, z); takes out the one step, hence it's faster.
    You don't have to demonstrate (prove) me anything.
     
  29. Offline

    macwinexpert

    Still not working, I'm afraid :/

    This is what I have at the moment:

    Code:
    void replenish(Player player) {
         
            if(player.getInventory().contains(Material.SAND) && player.getLocation().getX() > 139 && player.getLocation().getX() < 170 && player.getLocation().getZ() < -114 && player.getLocation().getZ() > -135) {
             
         
                Block cube1 = player.getWorld().getBlockAt(123, 105, -118);
                Block cube2 = player.getWorld().getBlockAt(130, 105, -137);
             
                player.sendMessage("Did get activated");
             
                    if(player.getInventory().contains(Material.SAND, 2)) {
                     
                        cube1.setType(Material.SAND);
                        cube2.setType(Material.SAND);
                     
                        player.sendMessage("no.1 and no.2");
                    }
                 
                    else if(player.getInventory().contains(Material.SAND, 1)) {
                     
                        if(cube1.getType() == Material.AIR) {cube1.setType(Material.SAND); player.sendMessage("no.1");}
                        else if(cube2.getType() == Material.AIR) {cube2.setType(Material.SAND); player.sendMessage("no.2");}
                }
            }
         
        }
    Another weird thing that I have noticed, is that cube2 is placing a sand block one Z- as well!

    Also, the coords are not incorrect, here is the location for cube1, and you can see the sand block that had spawned below!

    [​IMG]


    Thanks for all the help so far!


    Hamish (macwinexpert)
     
  30. Offline

    V10lator

    Sure that it spawned there? Sand tends to fall down... ;)
     
Thread Status:
Not open for further replies.

Share This Page