Solved Checking if a player is in a region

Discussion in 'Plugin Development' started by xpaintall, Feb 27, 2022.

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

    xpaintall

    Alright so I'm trying to check if a player is in a specific region of blocks, and I've attempted to do that with this: (I saw this on another post)
    Code:
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
    
            Player player = event.getPlayer();
    
            double plX = player.getLocation().getX();
            double plY = player.getLocation().getY();
            double plZ = player.getLocation().getZ();
    
            double minX = -129.988;
            double minY = 24;
            double minZ = -99.050;
    
            double maxX = -132.012;
            double maxY = 30.5;
            double maxZ = -100.950;
    
            if(f.getState() == GameState.RUNNING) {
    
                if ((plX <= maxX && plX >= minX) && (plY <= maxY && plY >= minY) && (plZ <= maxZ && plZ >= minZ)) {
    yes, I did register the event and the GameState was RUNNING.
    I have no idea on how to fix this but it's the most vital part of my plugin so I cannot skip that part.

    Also if you need additional info here's the entire class if I did anything wrong outside of it that could've caused the problem
    Code:
    package com.xpaintall.ShoppingCartMain.GameManager;
    
    import com.xpaintall.ShoppingCartMain.Cart.ShoppingCart;
    import com.xpaintall.ShoppingCartMain.Main;
    import net.minecraft.server.level.WorldServer;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import java.util.Collections;
    import java.util.List;
    
    
    public class GameRunnable extends BukkitRunnable implements Listener {
    
        Main f;
    
        public GameRunnable(Main f) {
            this.f = f;
        }
    
        @Override
        public void run() {
            if (f.getState() == GameState.WAITING) {
                if (f.getPlayers().size() == 2) {
                    f.startGame();
                }
            } else if(f.getState() == GameState.RUNNING) {
                if(f.getPlayers().size() != 2) {
                    f.getPlayers().clear();
                    f.getLappedPlayers().clear();
                    f.setState(GameState.WAITING);
                }
            }
        }
    
    
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
    
            Player player = event.getPlayer();
    
            double plX = player.getLocation().getX();
            double plY = player.getLocation().getY();
            double plZ = player.getLocation().getZ();
    
            double minX = -129.988;
            double minY = 24;
            double minZ = -99.050;
    
            double maxX = -132.012;
            double maxY = 30.5;
            double maxZ = -100.950;
    
            if(f.getState() == GameState.RUNNING) {
    
                if ((plX <= maxX && plX >= minX) && (plY <= maxY && plY >= minY) && (plZ <= maxZ && plZ >= minZ)) {
                    if(f.getLaps(player.getUniqueId()) == 3) {
                        if(f.getState() == GameState.RUNNING) {
                            f.endGame(player);
                        } else {
                            Bukkit.shutdown();
                        }
                    } else {
                        f.addLap(player.getUniqueId());
                        player.sendMessage(ChatColor.GREEN + "" + f.getLaps(player.getUniqueId()) + "/3 Laps!");
                        ShoppingCart s = new ShoppingCart(player.getLocation());
                        if(s.getPassengers().equals(Collections.singletonList(player))) {
                            s.killEntity();
                        }
                        WorldServer world = ((CraftWorld) Bukkit.getWorld("world")).getHandle();
                        ShoppingCart cart = new ShoppingCart(new Location(Bukkit.getWorld("world"), -134.500, 4, -115.500));
                        world.addEntity(cart);
                        player.teleport(new Location(Bukkit.getWorld("world"), -134.500, 4, -115.500));
                    }
                }
            }
        }
    
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent event) {
            Player player = event.getPlayer();
            f.getPlayers().remove(player.getUniqueId());
        }
    
        /*
         * locations
         * -129.988 24 -99.050 - min
         * -132.012 30.5 -100.950 - max
         *  */
    
    }
    
    please help me if you have an idea on how to fix this
     
  2. Offline

    Strahan

    Your code works fine for me. Well, using different coords but that is irrelevant to the detection logic. Are you sure the event is firing? Add a player.sendMessage("PME"); or something.

    Also, you really should filter PME. It will fire even if you twitch your mouse. e.g.
    Code:
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
      if (e.getFrom().getBlockX() == e.getTo().getBlockX() &&
          e.getFrom().getBlockY() == e.getTo().getBlockY() &&
          e.getFrom().getBlockZ() == e.getTo().getBlockZ()) return;
    
      Player player = event.getPlayer();
      ...etc
     
  3. Offline

    xpaintall

    @Strahan oh wow, I'm so stupid to forget that negative values with a lower apsolute value are actually a higher number and opposite (grade 6. math lmao) so basically I swapped the names of the variables (instead of the one on the y axis cuz it's a positive number)
     
    Strahan likes this.
Thread Status:
Not open for further replies.

Share This Page