Setting location

Discussion in 'Spigot Plugin Development' started by MadMaxCookie, May 31, 2016.

  1. Hello how do I done this thing

    first set the first location Left click at the block , Second Location Use right click at the block

    and how do I cancel them to break block if the location is setted between the two location for example.
     
  2. Here's an idea on how to approach your problem:
    This event is called when a player left clicks a block. This event gets called when a player right clicks a block. Add 2 listeners for those events, check things like what the players have clicked (e.g. the second event also gets called when a player right clicks a zombie).
    You'll also need something like a HashMap where you use player UUIDs as keys and save two locations as a value. You could use a class like this for the values:
    Code (Text):
    class LocationPair() {
      Location leftClickLocation;
      Location rightClickLocation;
      // Constructors
      // Getter and setter methods
      // etc.
    }
    That way you can save 2 locations for every player.

    Finally a quick example of possible scenarios for the BlockDamageEvent (so a left click):
    • Map doesn't contain any LocationPairs for the player: add a new LocationPair to the map with the left location of the clicked block
    • Map already contains LocationPair for the player with a right location: add the left one and then do whatever you want to do after a player has left and right clicked
    • Map already contains a LocationPair for the player with a left location: override the left location with the new one
     
  3. I know that already but for some reason I posted this thing here I don't know if the player is breaking the block between the first and second location how do you compare them if it's necessary.
     
  4. What do you mean by breaking the block between the locations? Can you give me a quick example?^^
     
  5. ohhh my bad so an example I'm setting first location to the top of the building and second at the bottom then, between those locations the event BlockBreakEvent would be cancelled if they are not permissible.
     
  6. So you mean that another plugin is disabling the BlockBreakEvent for the player at that location because he's not allowed to mine there?
    Maybe setting your event listener to a higher priority helps^^
     
  7. Ummm... Just do what you are saying? :D
    Code (Text):

    Location loc1, loc2;
    @EventHandler
    public void onBreak(BlockBreakEvent e) {
      Location fixedLoc1 = loc1.clone(); // Creating an "actual cube" so we can compare
      Location fixedLoc2 = loc2.clone(); // Maybe put this part outside the BlockBreak, to be more efficent
      if (loc1.getX() > loc2.getX()) {
        fixedLoc1.setX(loc2.getX());
        fixedLoc2.setX(loc1.getX());
      }
      if (loc1.getY() > loc2.getY()) {
        fixedLoc1.setY(loc2.getY());
        fixedLoc2.setY(loc1.getY());
      }
      if (loc1.getZ() > loc2.getZ()) {
        fixedLoc1.setZ(loc2.getZ());
        fixedLoc2.setZ(loc1.getZ());
      }
     
      Block b = e.getBlock();
      if (b.getLocation().getX >= fixedLoc1.getX() || b.getLocation().getX <= fixedLoc2.getX()
        || b.getLocation().getY >= fixedLoc1.getX() || b.getLocation().getY <= fixedLoc2.getX()
        || b.getLocation().getZ >= fixedLoc1.getX() || b.getLocation().getZ <= fixedLoc2.getX()) {
        if (!e.getPlayer().hasPermission("perm")) {
          e.setCancelled(true);
        }
      }
    }
     
     
  8. Put 2 Event in a Listeners Class and pass the location with a Map<Player, Location[]>
     
  9. that's what already I did.
    so if they broke any blocks inside the 2 location it would be cancel the event if so its really imformative, thanks alot.