Solved TP to random location in a cuboid

Discussion in 'Plugin Development' started by flash1110, Feb 25, 2015.

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

    flash1110

    Basically, I have a cuboid and I want to tp to a random location. This is for a random tp plugin. I"m using desht's cuboid class to create the cuboid. I was wondering, how do I tp to a random location in the cuboid?
     
  2. Offline

    LordDarthBob

    @flash1110
    Code:
    Cuboid instance = new Cuboid(/*stuff*/);
    Player player = getServer().getPlayerExact("xXx_Billy_xXx");
    
    double x = (double) (Math.random() * (instance.getUpperX() - instance.getLowerX()) + instance.getLowerX();
    double z = (double) (Math.random() * (instance.getUpperZ() - instance.getLowerZ()) + instance.getLowerZ();
    World w = instance.getWorld();
    
    Location location = new Location(w, x, w.getHighestBlockAt(Math.round(x), Math.round(z)), z);
    
    player.teleport(location);
    
     
    Last edited: Feb 25, 2015
  3. Offline

    Gingerbreadman

    @LordDarthBob don't do Player player = getServer().getPlayer("xXx_Billy_xXx");
    instead do Player player = Bukkit.getServer().getPlayerExact("xXx_Billy_xXx");
     
  4. Offline

    LordDarthBob

    @Gingerbreadman
    That was just an example, but good point.

    @FisheyLP
    Don't get me wrong, spoonfeeding is definitely a problem when it gets in the way of experimentation, and providing the amount of code that I did was probably against better judgement, but I felt that providing a simple, complete example for such a basic function was more useful, in contrast to providing half a class or whole plugin and destroying the learning process. player.teleport() and math.random() aren't really concepts that needs to be taught by sending people on a method-quest imo.
     
  5. Offline

    flash1110

    @LordDarthBob

    Thanks, but there is one problem in this line:
    Code:
    Location location = new Location(w, x, w.getHighestBlockAt(Math.round(x), Math.round(z)), z);
    The method is getHighestBlockAt(int, int), not (long, long). How do I fix that? Sorry if this is asking to be spoonfed, but I'm just stumped.
     
    Last edited: Feb 26, 2015
  6. Offline

    LordDarthBob

    @flash1110
    After some quick reading, I realized that Math.round does not, in fact, have the capability to return int, therefore you'll have to cast the long that is returned from Math.round() to an int. This would look something like:
    Code:
    Location location = new Location( w, x, w.getHighestBlockAt((int)Math.round(x), (int)Math.round(z)), z);
    
    Although it's unlikely to occur in this context, unregulated casting from one type to another can cause major issues. If you're unclear on concepts like casting, the Java Tutorials are a helpful read.
     
    flash1110 likes this.
  7. Offline

    flash1110

    @LordDarthBob
    Thanks for all your help. I just get this error:
    The constructor Location(World, double, Block, double) is undefined
     
  8. Offline

    LordDarthBob

    flash1110 likes this.
Thread Status:
Not open for further replies.

Share This Page