Solved Random Location inbetween two Locations

Discussion in 'Plugin Help/Development/Requests' started by Smootey, Feb 5, 2015.

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

    Smootey

    Hello guys!

    When I am trying to get a random location inbetween two locations, I get an error...

    Error (open)
    Code:
    [11:27:58 ERROR]: Could not pass event InventoryClickEvent to StickFight v0.0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:305) ~[spigot.jar:git-Spigot-47b1dff-f233e7d]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-47b1dff-f233e7d]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-47b1dff-f233e7d]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-47b1dff-f233e7d]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java
    :1586) [spigot.jar:git-Spigot-47b1dff-f233e7d]
            at net.minecraft.server.v1_8_R1.PacketPlayInWindowClick.a(SourceFile:31)
    [spigot.jar:git-Spigot-47b1dff-f233e7d]
            at net.minecraft.server.v1_8_R1.PacketPlayInWindowClick.a(SourceFile:9)
    [spigot.jar:git-Spigot-47b1dff-f233e7d]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spi
    got.jar:git-Spigot-47b1dff-f233e7d]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_25]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_25]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    83) [spigot.jar:git-Spigot-47b1dff-f233e7d]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    16) [spigot.jar:git-Spigot-47b1dff-f233e7d]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    23) [spigot.jar:git-Spigot-47b1dff-f233e7d]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :526) [spigot.jar:git-Spigot-47b1dff-f233e7d]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
    Caused by: java.lang.IllegalArgumentException: bound must be positive
            at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_25]
            at de.cagecoding.stickfight.utils.RandomLocation.getRandomLocation(Rando
    mLocation.java:17) ~[?:?]
            at de.cagecoding.stickfight.listeners.InventoryListener.onInventoryClick
    (InventoryListener.java:25) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _25]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _25]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_25]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301) ~[spigot.jar:git-Spigot-47b1dff-f233e7d]
            ... 14 more


    And here's the method:

    Method (open)
    Code:
        public static Location getRandomLocation() {
            World w = DataManager.getPos1().getWorld();
            int dx = DataManager.getPos1().getBlockX() - DataManager.getPos2().getBlockX();
            int dz = DataManager.getPos1().getBlockZ() - DataManager.getPos2().getBlockZ();
            Random random = new Random();
            int x = random.nextInt(dx) + DataManager.getPos1().getBlockX();
            int z = random.nextInt(dz) + DataManager.getPos1().getBlockZ();
            for(int y = 256; y > 0; y--) {
                if(!new Location(w, x, y, z, 0, 0).getBlock().getType().equals(Material.AIR) && !new Location(w, x, y, z, 0, 0).getBlock().getType().equals(Material.LAVA) && new Location(w, x, y, z, 0, 0).getBlock().getType().equals(Material.CACTUS)) {
                    return new Location(w, x, y+1, z, 0, 0);
                }
            }
            return getRandomLocation();
        }


    I hope you can help me

    Greets, Smootey ~
     
  2. Offline

    RW_Craft

    Code:
    [11:27:58 ERROR]: Could not pass event InventoryClickEvent to StickFight v0.0.1
    Show us your InventoryClickEvent
     
  3. Offline

    Smootey

    @RW_Craft

    IntenvoryClickEvent (open)

    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            e.setCancelled(true);
            if(e.getInventory().getName() == Inventories.getTutorialInventory().getName()) {
                if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("\u00a7aJa")){
                    e.getWhoClicked().closeInventory();
                    ((Player) e.getWhoClicked()).sendMessage("\u00a77Das Tutorial ist derzeit noch nicht verfügbar...");
                    Inventories.giveInventory((Player) e.getWhoClicked());
                    e.getWhoClicked().teleport(RandomLocation.getRandomLocation());
                } else if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("\u00a7cNein")) {
                    e.getWhoClicked().closeInventory();
                    ((Player) e.getWhoClicked()).sendMessage("\u00a77Das Tutorial ist derzeit noch nicht verfügbar...");
                    Inventories.giveInventory((Player) e.getWhoClicked());
                    e.getWhoClicked().teleport(RandomLocation.getRandomLocation());
                }
            }
        }
     
  4. Offline

    caderape

    1. Caused by: java.lang.IllegalArgumentException: bound must be positive
    2. at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_25

    I guess the pos2 - pos1 return a negative number. You have to check that.
    Random method always return a number >= 0

    @Smootey
     
  5. Offline

    Smootey

    @caderape

    Thats what I found out too, but I don't know how to fix it...
     
  6. Offline

    caderape

    @Smootey
    if (dx < 0) dx = - dx;
    This should remove the -
    then you get your random numbe.
    And after if the number was negative, u just do a dx = -dx;
    And that should be good
     
  7. Online

    timtower Moderator Moderator

    Moved to Bukkit alternatives
     
  8. Offline

    Smootey

    @caderape

    So, I reworked my complete code and it works fine, but when the location has negative coordinates, it teleports me out of the field...

    RandomLocation (open)

    Code:
        public static Location getRandomLocation() {
            World w = DataManager.getPos1().getWorld();
            Bukkit.broadcastMessage("Pos1: X: " + DataManager.getPos1().getBlockX() + " Z: " + DataManager.getPos1().getBlockZ() + " Pos2: X: " + DataManager.getPos2().getBlockX() + " X: " + DataManager.getPos2().getBlockZ());
            int maxX = Math.max(DataManager.getPos1().getBlockX(), DataManager.getPos2().getBlockX());
            int maxZ = Math.max(DataManager.getPos1().getBlockZ(), DataManager.getPos2().getBlockZ());
            int minX = Math.min(DataManager.getPos1().getBlockX(), DataManager.getPos2().getBlockX());
            int minZ = Math.min(DataManager.getPos1().getBlockZ(), DataManager.getPos2().getBlockZ());
            Bukkit.broadcastMessage("maxX: " + maxX + " minX: " + minX + " maxZ: " + maxZ + " minZ: " + minZ);
            int numX = maxX - minX;
            int numZ = maxZ - minZ;
            Bukkit.broadcastMessage("numX: " + numX + " numZ: " + numZ);
            boolean minusnumX = false;
            boolean minusnumZ = false;
            if(numX < 0) {
                numX = -numX;
                minusnumX = true;
            }
            if(numZ < 0) {
                numZ = -numZ;
                minusnumZ = true;
            }
            Bukkit.broadcastMessage("numX: " + numX + " numZ: " + numZ);
            Random rdm = new Random();
            int x = rdm.nextInt(numX);
            rdm = new Random();
            int z = rdm.nextInt(numZ);
            if(minusnumX) {
                x = -x;
            }
            if(minusnumZ) {
                z = -z;
            }
            Bukkit.broadcastMessage("x: " + x + " z: " + z);
            if(minX < 0) {
                x = x + maxX;
            } else {
                x = x + minX;
            }
            if(minZ < 0) {
                z = z + maxZ;
            } else {
                z = z + minX;
            }
            Bukkit.broadcastMessage("x: " + x + " z: " + z);
            for(int y = 256; y > 0; y--) {
                if(!new Location(w, x, y, z, 0, 0).getBlock().getType().equals(Material.AIR)) {
                    Bukkit.broadcastMessage("true2");
                    return new Location(w, x, y+1, z, 0, 0);
                }
            }
            return getRandomLocation();
        }


    I got it working now!

    For everybody who needs it:

    Code:
        public static Location getRandomLocation() {
            World w = DataManager.getPos1().getWorld();
            int maxX = Math.max(DataManager.getPos1().getBlockX(), DataManager.getPos2().getBlockX());
            int maxZ = Math.max(DataManager.getPos1().getBlockZ(), DataManager.getPos2().getBlockZ());
            int minX = Math.min(DataManager.getPos1().getBlockX(), DataManager.getPos2().getBlockX());
            int minZ = Math.min(DataManager.getPos1().getBlockZ(), DataManager.getPos2().getBlockZ());
            float factor = (float) Math.random();
            int x = (int) (minX + (maxX - minX)*factor);
            factor = (float) Math.random();
            int z = (int) (minZ +(maxZ - minZ) * factor);
            for(int y = 256; y > 0; y--) {
                if(!new Location(w, x, y, z, 0, 0).getBlock().getType().equals(Material.AIR)) {
                    Bukkit.broadcastMessage("true2");
                    return new Location(w, x, y+1, z, 0, 0);
                }
            }
            return getRandomLocation();
        }
    Greets!
     
    Last edited by a moderator: Feb 5, 2015
Thread Status:
Not open for further replies.

Share This Page