10 out of 100 % chances??

Discussion in 'Plugin Development' started by BeastCraft3, Apr 17, 2015.

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

    1Rogue

    Exactly my point.
     
  2. Offline

    nverdier

    Then why are you using it...?
     
  3. @nverdier I didn't know it before but I tested it a few minutes ago
     
  4. Offline

    nverdier

    Ah.
     
  5. Offline

    BeastCraft3

    @FisheyLP @nverdier
    Sorry to all of you, its not easy for everyone to learn java that quickly, I'm very young and am trying to get good. I know am horrible at it, I know my english is rubish but please dont get mad. I'm really trying to understand what your saying during that am only 11. Please just please be more patient with me ;(

    If anyone is still interessted in helping me, am I close to the end?
    Code:
    @EventHandler
        public void onPlayerMoveEvent(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            Location loc = p.getLocation();
            loc.setY(loc.getY() -2);
            
            int block = loc.getWorld().getBlockTypeIdAt(loc);
            if (block == 19)
            {
                int x = 10;
                if (new Random().nextInt(100) <= x) {
                    String[] commands = {"/randomtp 1", "/randomtp 2", "/randomtp 3", "/randomtp 4", "/randomtp 5", "/randomtp 6", "/randomtp 7", "/randomtp 8", "/randomtp 9", "/randomtp 10"};
                    String random = commands[new Random().nextInt(commands.length)];
                   
                }
            }
        }
     
  6. Offline

    nverdier

  7. Offline

    ItsMattHogan

    If "randomtp" is the only command you're using, then there's no point in storing a list of that command with a different number as the first argument, it's just a waste of memory...
    Code:
    String variable = "/randomtp " + new Random().nextInt(9); // 9 because the index starts at zero
     
  8. Offline

    Zombie_Striker

    10/100 is the same as 1/10 , You could change x = 1; and set the .nextInt(10); , They both mean the same thing. Also, by creating two new Randoms(), you're using more memory.
     
  9. Offline

    BeastCraft3

    @nverdier @ItsMattHogan @Zombie_Striker
    so if I understand right, this will work?
    Code:
    @EventHandler
        public void onPlayerMoveEvent(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            Location loc = p.getLocation();
            loc.setY(loc.getY() -2);
            
            int block = loc.getWorld().getBlockTypeIdAt(loc);
            if (block == 19)
            {
                String commands = "/randomtp " + new Random().nextInt(9);
                p.performCommand(commands);
            } else {
                e.setCancelled(true);
            }
        }
     
  10. This will work. kind of...
    .nextInt(9) returns a number between 0 and 8, just sayin
    It will cancel any movement if the block id isnt 19. Block ids work, but you should switch to Materials because block ids are no longer supported in 1.8+
    Just remove:
    Code:
    int block = world.getBlockTypeIdAt(loc)
    if(block == 19) {
    And replace it with:

    (You can simply check its material like this:)
    Code:
    if (loc.getBlock().getType() == Material.SPONGE) {
     
    Last edited: Apr 17, 2015
  11. Offline

    stormneo7

    Why do I need to know what is it exactly? I have the general idea down... Sorry I'm not perfect sheesh
     
  12. Offline

    nverdier

    But you don't.
     
  13. Offline

    SuperOriginal

    @stormneo7 So you don't want to learn the right way to do something because you're "not perfect"?
     
  14. Offline

    BeastCraft3

    @SuperOriginal @nverdier @stormneo7 @FisheyLP
    ok guys, call me a noob but I used an easier solution. Not the best one but it works fine for me ;)
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            if(e.getTo().getBlock().getRelative(BlockFace.DOWN).getType() == Material.SPONGE) {
                Random object = new Random();
                int Beaxt;
               
                for(int counter =1; counter<=1; counter++){
                    Beaxt = 1+object.nextInt(10);
                   
                    if(Beaxt == 1){
                        p.performCommand("randomtp 1");
                    }else if(Beaxt == 2){
                        p.performCommand("randomtp 2");
                    }else if(Beaxt == 3){
                        p.performCommand("randomtp 3");
                    }else if(Beaxt == 4){
                        p.performCommand("randomtp 4");
                    }else if(Beaxt == 5){
                        p.performCommand("randomtp 5");
                    }else if(Beaxt == 6){
                        p.performCommand("randomtp 6");
                    }else if(Beaxt == 7){
                        p.performCommand("randomtp 7");
                    }else if(Beaxt == 8){
                        p.performCommand("randomtp 8");
                    }else if(Beaxt == 9){
                        p.performCommand("randomtp 9");
                    }else if(Beaxt == 10){
                        p.performCommand("randomtp 10");
                    }
                }
               
            }
        }
     
  15. Argh. My eyes burn when I see your code :eek:
     
  16. Offline

    nverdier

    Oh my dear lord.
     
  17. Why god ? xD
     
  18. Offline

    BeastCraft3

  19. Offline

    nverdier

    ...

    It is not the easiest way. It's brutal.
     
    FisheyLP and flash1110 like this.
  20. Isn't the easiest way xD

    Code:
    int random = new Random().nexInt(10);
    player.performCommand("randomtp " + random);
    
     
  21. Offline

    BrickBoy55

  22. Offline

    nverdier

    Noooooo.
     
  23. Offline

    BrickBoy55

  24. Offline

    nverdier

    @BrickBoy55 You can do everything much much simpler.
     
  25. Offline

    BrickBoy55

    @nverdier

    A switch case is the only easier way I can think of off the top of my head.
     
  26. Offline

    teej107

    @BeastCraft3 since I noticed your commands are randomtp <number here>, just do "randomtp " + random#nextInt(n) for your command. No need for switch or ifs.
     
  27. Offline

    1Rogue

    ffs...


    Code:java
    1. int rand = ThreadLocalRandom.current().nextInt(9); //stop making nonsense random instances
     
    Konato_K and ItsMattHogan like this.
  28. Offline

    nverdier

    What on Earth does that mean?
     
  29. Offline

    1Rogue

    It's the exact same function as "new Random()#nextInt" without creating an obscene amount of objects or potential RNG collisions
     
  30. Offline

    Lolmewn

    omg someone who actually knows how to code \o/
    Finally!
     
Thread Status:
Not open for further replies.

Share This Page