Solved WorldGuard player placed a block.

Discussion in 'Spigot Plugin Development' started by chunza2542, Apr 30, 2017.

  1. I'm trying to make Protection Stone plugin on minecraft v1.9. I use sqlite to store protection list. When BlockPlaceEvent working I'm put the location, protection radius & owner in it. But when player placed a protection block on worldguard area. It add the protection data to database but the protection stone is not place.

    Here is my picture:

    How can I fix this problem. Thank :)
    sry for my English!
  2. So I get this straight:
    - Someone places a stone which protects an area of a certain radius
    - The issue is that when you do it in a worldguarded area, the location is saved but the block isn't placed?
  3. @nikmanG Yes. I want to make it not save the location. When the block isn't place in worldguard area
  4. Okay, so you'll need to import WorldGuard and WorldEdit into your project. On the block place event check if the player is in a region and if the region flags permit placing blocks. If not (and if they are not in the allowed list of people) then cancel the place event.
  5. Thank for solution!
  6. If you have trouble with code, paste it here, because I have worked with WorldGuard and it can be cumbersome.
    • Friendly Friendly x 1
  7. @nikmanG Could you example me a code. I never working with world guard before.
  8. Wait, to avoid future bugs, shouldn't you check to see if the block location is in the region? What if they we're standing outside it
  9. I did this a while back so the API may have better methodology but here it goes anyhow:
    Code (Text):

    //Pretend this isn't null
    Location l = (do whatever to see the location of placed block)
    for(ProtectedRegion r : WGBukkit.getRegionManager(l).getApplicableRegions(l)) {
    That basically iterates through a lit of regions where the set location is (because there can be multiple).
    Next check if the region has allowed the build flag
    Code (Text):

    State state;

    //Or build. All stuff regarding flags:
    if( (state = (State) r.getFlags().get(DefaultFlag.BLOCK_PLACE)) != null) {
        if(state == State.Deny) {
            //Cancel the event here
    Basically this checks if region has anything regarding block placement and if the state of it is to deny, if so cancel it.

    [I haven't tested this so you should play around with this and see if it works]

    Basically the code depends on the location so rather than testing with the player location use the block placed location.
  10. You could also set your event priority to a higher value like highest. And set your event to ignore cancelled events.

    @EventHandler (priority = EventPriority.HIGHEST, ignoreCancelled = false)

    Highest = processed last. Then if another event cancels it

    Ignore cancelled = true will make sure your event code does not run if another event cancels the event.

    Should avoid even needing to hook worldguard

    Sent from my iPhone using Tapatalk