Solved Cancelling placing or breaking blocks in certain cube/area problem.

Discussion in 'Plugin Development' started by PlayFriik, Dec 29, 2014.

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

    PlayFriik

    Hello, I am having a problem with cancelling the event for placing/breaking a block if it is in a certain cube/area.

    Listener:
    Code:
        if (isInside(loc, new Location(Bukkit.getWorld("World"), 0, 0, 0), new Location(Bukkit.getWorld("World"), 100, 100, 100))) {
            e.setCancelled(true);
        }
        public static boolean isInside(Location location, Location min, Location max) {
            return location.toVector().isInAABB(min.toVector(), max.toVector());
        }
    
    Yes, the events are registered. The problem is just that the if statement is always ignoring it or sth.
     
    Last edited: Dec 30, 2014
  2. Offline

    Gamesareme

    @PlayFriik Add some debugging into the script and see what is working. Also your method isInside return a boolean, add a debug, and see what it is returning.
     
  3. Offline

    Konato_K

    Why are you using "loc" instead of e.getLocation()?
     
    WarmakerT likes this.
  4. Offline

    WarmakerT

    That, and I think Server#getWorld(String) is case-sensitive, so it'd have to be getWorld("world") and not getWorld("World"), if your world name is world.
     
    Last edited: Dec 29, 2014
  5. Offline

    Konato_K

    @WarmakerT Yes, it's case sensitive, but maybe he changed the W to uppercase? I don't think you can do toVector with an invalid location, but I don't really know.
     
  6. Offline

    WarmakerT

    Thanks, clarified.

    Inside Location.java:
    Code:
        /**
         * Constructs a new {@link Vector} based on this Location
         *
         * @return New Vector containing the coordinates represented by this
         *     Location
         */
        public Vector toVector() {
            return new Vector(x, y, z);
        }
     
    Konato_K likes this.
  7. Offline

    PlayFriik

    @Konato_K @WarmakerT
    Yes, I renamed my world to World, so it is right.
    But what I forgot to copy is this line too:
    Code:
    Location loc = e.getBlock().getLocation();
    
    So the full code is:
    Code:
      @EventHandler
      public void onBlockPlace(BlockPlaceEvent e) {
         Location loc = e.getBlock().getLocation();
         if (isInside(loc, new Location(Bukkit.getWorld("World"), 0, 0, 0), new Location(Bukkit.getWorld("World"), 100, 100, 100))) {
            e.setCancelled(true);
         }
       }
       public static boolean isInside(Location location, Location min, Location max) {
           return location.toVector().isInAABB(min.toVector(), max.toVector());
       }
    FIXED!

    I just used this code from his thread, worked perfectly fine.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
Thread Status:
Not open for further replies.

Share This Page