Skip to content

Commit

Permalink
add new PSBreakProtectBlockEvent (#419)
Browse files Browse the repository at this point in the history
* breaking protection stones now trigger PSRemoveEvent

* PSBreakEvent will called if the player has broken a protectionstones.

* PSBreakEvent now called in playerBreakProtection function instead of BlockBreakEvent listener

* renaming PSBreakEvent to PSBreakProtectBlockEvent
  • Loading branch information
rinpr authored Jul 3, 2024
1 parent 66b6b3e commit 2d93637
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main/java/dev/espi/protectionstones/ListenerClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import dev.espi.protectionstones.event.PSBreakProtectBlockEvent;
import dev.espi.protectionstones.event.PSCreateEvent;
import dev.espi.protectionstones.event.PSRemoveEvent;
import dev.espi.protectionstones.utils.RecipeUtil;
Expand All @@ -41,7 +39,6 @@
import org.bukkit.block.Furnace;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
Expand All @@ -56,7 +53,6 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.GrindstoneInventory;
import org.bukkit.inventory.ItemStack;

import java.util.List;
Expand Down Expand Up @@ -172,6 +168,12 @@ private boolean playerBreakProtection(Player p, PSRegion r) {
return false;
}

// Call PSBreakEvent
PSBreakProtectBlockEvent event = new PSBreakProtectBlockEvent(r , p);
Bukkit.getPluginManager().callEvent(event);
// don't give ps block to player if the event is cancelled
if (event.isCancelled()) return false;

// return protection stone if no drop option is off
if (blockOptions != null && !blockOptions.noDrop) {
if (!p.getInventory().addItem(blockOptions.createItem()).isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dev.espi.protectionstones.event;

import dev.espi.protectionstones.PSRegion;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Event that is called when a protection stones block is removed
*/
public class PSBreakProtectBlockEvent extends Event implements Cancellable {

private static final HandlerList HANDLERS = new HandlerList();

private PSRegion region;
private Player player;
private boolean isCancelled = false;

public PSBreakProtectBlockEvent(PSRegion psr, Player player) {
this.region = checkNotNull(psr);
this.player = player;
}

/**
* Gets the player who triggered the event.
*
* @return The player.
*/
public Player getPlayer() {
return player;
}

/**
* Gets the ProtectionStones item associated with the region.
*
* @return The ProtectionStones item.
*/
public ItemStack getPSItem() {
return Objects.requireNonNull(region.getTypeOptions()).createItem();
}

/**
* Gets the ProtectionStones region associated with the event.
*
* @return The ProtectionStones region.
*/
public PSRegion getRegion() {
return region;
}

@Override
public boolean isCancelled() {
return isCancelled;
}

@Override
public void setCancelled(boolean cancel) {
isCancelled = cancel;
}

@NotNull
@Override
public HandlerList getHandlers() {
return HANDLERS;
}

public static HandlerList getHandlerList() {
return HANDLERS;
}
}

0 comments on commit 2d93637

Please sign in to comment.