1.9.4 Checking y position of player script is wrong

Discussion in 'Spigot Plugin Development' started by Lolmaster5000, Jan 9, 2021.

  1. this is not working. Can anybody tell me why this is wrong
    Code (Text):
                do {
                    Location l = p.getLocation();
                    if(l.getY() == 59) {
                        p.getInventory().clear();
                        p.getInventory().addItem(Stick);
                        p.sendMessage(ChatColor.RED + "Lets go!");
                        Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable() {
                            public void run() {
                               
                            }
                            }, 0, 20L);
                    }
                }while(p.getWorld().equals(w));
     
  2. When the player is in the world you want, it will basically block the main server thread. You should listen to the PlayerMoveEvent instead.
     
  3. JanTuck

    Supporter

    getY will very unlikely be exactly 59, you should use getBlockY instead.

    Or refer to using greater than and less than.

    Also what @Plochem said
     
    • Agree Agree x 1
  4. So now I make this but It also doesn't work

    Code (Text):
        @EventHandler
        public void ChangeWorld(PlayerChangedWorldEvent e) {
            Player p = e.getPlayer();
            World w = Bukkit.getWorld("KnockBackFFA");
            String nl = "\n";
            if (p.getWorld().equals(w)) {
            p.sendMessage(ChatColor.GOLD + "KnockBackFFA:" +
    nl + ChatColor.GREEN + "The Minigame is really simple!" +
    nl + "When you jump off the platform" +
    nl + "you get a knockbackstick and with this" +
    nl + "stick you can knock other from the platforms" +
    nl + "and you get kills and points." +
    nl + "With these you can buy things in the shop.");
            }
    }  
        public void PlayerMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            World w = Bukkit.getWorld("KnockBackFFA");
            ItemStack Stick = new ItemStack(Material.STICK, 1);
            ItemMeta stickmeta = Stick.getItemMeta();
            stickmeta.addEnchant(Enchantment.KNOCKBACK, 3, true);
            Stick.setItemMeta(stickmeta);
           
            do {
                Location l = p.getLocation();
                if(l.getBlockY() == 59) {
                    p.getInventory().clear();
                    p.getInventory().addItem(Stick);
                    p.sendMessage(ChatColor.RED + "Lets go!");
                    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable() {
                        public void run() {
                           
                        }
                        }, 0, 20L);
                }
            }while(p.getWorld().equals(w));
        }
     
    • Optimistic Optimistic x 1
  5. omg... this code is so wrong. the whole thing. using a loop inside PlayerMoveEvent just to give an item doesnt make sense.
     
  6. it doesn't work because the code is not inside of your run method number one, and number two, you don't want to do this in a loop, it is going to schedule a new task every tick, just so long as the player is in the world.
    Be careful, if you actually had code in run method the server would bog down and probably crash in a real big hurry.
    Code (Java):
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable() {
                            public void run() {
                               if(p.getWorld().equals(w) && p.getLocation().getY() == 59) {
                                   p.getInventory().clear();
                                   p.getInventory().addItem(Stick);
                                   p.sendMessage(ChatColor.RED + "Lets go!");
                            }
                        }, 0, 20L);
    EDIT and don't put it in the player move event, or anywhere else that would schedule the task more than once either.
     
    #6 ForbiddenSoul, Jan 9, 2021
    Last edited: Jan 9, 2021
  7. + Add @EventHandler