Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new PSBreakEvent #419

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading