Skip to content

Commit 2d93637

Browse files
authored
add new PSBreakProtectBlockEvent (#419)
* 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
1 parent 66b6b3e commit 2d93637

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

src/main/java/dev/espi/protectionstones/ListenerClass.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717

1818
import com.sk89q.worldedit.bukkit.BukkitAdapter;
1919
import com.sk89q.worldedit.math.BlockVector3;
20-
import com.sk89q.worldguard.WorldGuard;
2120
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
2221
import com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent;
2322
import com.sk89q.worldguard.protection.ApplicableRegionSet;
2423
import com.sk89q.worldguard.protection.flags.Flags;
25-
import com.sk89q.worldguard.protection.flags.StateFlag;
2624
import com.sk89q.worldguard.protection.managers.RegionManager;
2725
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
28-
import com.sk89q.worldguard.protection.regions.RegionContainer;
26+
import dev.espi.protectionstones.event.PSBreakProtectBlockEvent;
2927
import dev.espi.protectionstones.event.PSCreateEvent;
3028
import dev.espi.protectionstones.event.PSRemoveEvent;
3129
import dev.espi.protectionstones.utils.RecipeUtil;
@@ -41,7 +39,6 @@
4139
import org.bukkit.block.Furnace;
4240
import org.bukkit.command.CommandSender;
4341
import org.bukkit.enchantments.Enchantment;
44-
import org.bukkit.entity.EntityType;
4542
import org.bukkit.entity.Player;
4643
import org.bukkit.event.Event;
4744
import org.bukkit.event.EventHandler;
@@ -56,7 +53,6 @@
5653
import org.bukkit.event.player.PlayerJoinEvent;
5754
import org.bukkit.event.player.PlayerTeleportEvent;
5855
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
59-
import org.bukkit.inventory.GrindstoneInventory;
6056
import org.bukkit.inventory.ItemStack;
6157

6258
import java.util.List;
@@ -172,6 +168,12 @@ private boolean playerBreakProtection(Player p, PSRegion r) {
172168
return false;
173169
}
174170

171+
// Call PSBreakEvent
172+
PSBreakProtectBlockEvent event = new PSBreakProtectBlockEvent(r , p);
173+
Bukkit.getPluginManager().callEvent(event);
174+
// don't give ps block to player if the event is cancelled
175+
if (event.isCancelled()) return false;
176+
175177
// return protection stone if no drop option is off
176178
if (blockOptions != null && !blockOptions.noDrop) {
177179
if (!p.getInventory().addItem(blockOptions.createItem()).isEmpty()) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package dev.espi.protectionstones.event;
2+
3+
import dev.espi.protectionstones.PSRegion;
4+
import org.bukkit.entity.Player;
5+
import org.bukkit.event.Cancellable;
6+
import org.bukkit.event.Event;
7+
import org.bukkit.event.HandlerList;
8+
import org.bukkit.inventory.ItemStack;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
import java.util.Objects;
12+
13+
import static com.google.common.base.Preconditions.checkNotNull;
14+
15+
/**
16+
* Event that is called when a protection stones block is removed
17+
*/
18+
public class PSBreakProtectBlockEvent extends Event implements Cancellable {
19+
20+
private static final HandlerList HANDLERS = new HandlerList();
21+
22+
private PSRegion region;
23+
private Player player;
24+
private boolean isCancelled = false;
25+
26+
public PSBreakProtectBlockEvent(PSRegion psr, Player player) {
27+
this.region = checkNotNull(psr);
28+
this.player = player;
29+
}
30+
31+
/**
32+
* Gets the player who triggered the event.
33+
*
34+
* @return The player.
35+
*/
36+
public Player getPlayer() {
37+
return player;
38+
}
39+
40+
/**
41+
* Gets the ProtectionStones item associated with the region.
42+
*
43+
* @return The ProtectionStones item.
44+
*/
45+
public ItemStack getPSItem() {
46+
return Objects.requireNonNull(region.getTypeOptions()).createItem();
47+
}
48+
49+
/**
50+
* Gets the ProtectionStones region associated with the event.
51+
*
52+
* @return The ProtectionStones region.
53+
*/
54+
public PSRegion getRegion() {
55+
return region;
56+
}
57+
58+
@Override
59+
public boolean isCancelled() {
60+
return isCancelled;
61+
}
62+
63+
@Override
64+
public void setCancelled(boolean cancel) {
65+
isCancelled = cancel;
66+
}
67+
68+
@NotNull
69+
@Override
70+
public HandlerList getHandlers() {
71+
return HANDLERS;
72+
}
73+
74+
public static HandlerList getHandlerList() {
75+
return HANDLERS;
76+
}
77+
}

0 commit comments

Comments
 (0)