Skip to content

Commit 91acc42

Browse files
committed
feat: introduce player reset events for inventory, health, hunger, experience, money, and tamed entities
1 parent 2f4dea0 commit 91acc42

12 files changed

Lines changed: 812 additions & 1 deletion

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ paperweight.reobfArtifactConfiguration = io.papermc.paperweight.userdev.ReobfArt
4646
group = "world.bentobox" // From <groupId>
4747

4848
// Base properties from <properties>
49-
val buildVersion = "3.16.2"
49+
val buildVersion = "3.17.0"
5050
val buildNumberDefault = "-LOCAL" // Local build identifier
5151
val snapshotSuffix = "-SNAPSHOT" // Indicates development/snapshot version
5252

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package world.bentobox.bentobox.api.events.player;
2+
3+
import org.bukkit.World;
4+
import org.bukkit.event.Cancellable;
5+
import world.bentobox.bentobox.api.events.BentoBoxEvent;
6+
import world.bentobox.bentobox.database.objects.Island;
7+
8+
import java.util.UUID;
9+
10+
/**
11+
* @author tastybento
12+
*/
13+
public abstract class PlayerBaseEvent extends BentoBoxEvent implements Cancellable {
14+
private boolean cancelled;
15+
16+
protected final Island island;
17+
protected final UUID playerUUID;
18+
protected final World world;
19+
20+
public PlayerBaseEvent(UUID playerUUID, Island island, World world) {
21+
super();
22+
this.playerUUID = playerUUID;
23+
this.world = world;
24+
this.island = island;
25+
}
26+
27+
/**
28+
* @return the island involved in this event. This may be null if the event is not related to an island
29+
* or if the island has been deleted.
30+
*/
31+
public Island getIsland() {
32+
return island;
33+
}
34+
35+
/**
36+
* Get the world involved in this event.
37+
* @return world
38+
*/
39+
public World getWorld() {
40+
return world;
41+
}
42+
43+
/**
44+
* @return the playerUUID
45+
*/
46+
public UUID getPlayerUUID() {
47+
return playerUUID;
48+
}
49+
50+
@Override
51+
public boolean isCancelled() {
52+
return cancelled;
53+
}
54+
55+
@Override
56+
public void setCancelled(boolean cancel) {
57+
cancelled = cancel;
58+
}
59+
60+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package world.bentobox.bentobox.api.events.player;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.World;
5+
import world.bentobox.bentobox.database.objects.Island;
6+
7+
import java.util.UUID;
8+
9+
/**
10+
* Fired when a player event happens.
11+
*
12+
* @author tastybento
13+
*/
14+
public class PlayerEvent {
15+
16+
public enum Reason {
17+
INVENTORY_RESET,
18+
TAMED_REMOVAL,
19+
ENDERCHEST_RESET,
20+
MONEY_RESET,
21+
HEALTH_RESET,
22+
HUNGER_RESET,
23+
EXP_RESET,
24+
UNKNOWN
25+
}
26+
27+
public static PlayerEventBuilder builder() {
28+
return new PlayerEventBuilder();
29+
}
30+
31+
public static class PlayerEventBuilder {
32+
private World world;
33+
private UUID player;
34+
private Island island;
35+
private Reason reason = Reason.UNKNOWN;
36+
37+
public PlayerEventBuilder world(World world) {
38+
this.world = world;
39+
return this;
40+
}
41+
42+
public PlayerEventBuilder island(Island island) {
43+
this.island = island;
44+
return this;
45+
}
46+
47+
/**
48+
* @param reason for the event
49+
* @return TeamEventBuilder
50+
*/
51+
public PlayerEventBuilder reason(Reason reason) {
52+
this.reason = reason;
53+
return this;
54+
}
55+
56+
/**
57+
* @param player - the player involved in the event
58+
* @return TeamEventBuilder
59+
*/
60+
public PlayerEventBuilder involvedPlayer(UUID player) {
61+
this.player = player;
62+
return this;
63+
}
64+
65+
private PlayerBaseEvent getEvent() {
66+
return switch (reason) {
67+
case INVENTORY_RESET -> new PlayerResetInventoryEvent(world, island, player);
68+
case TAMED_REMOVAL -> new PlayerTamedRemovalEvent(world, island, player);
69+
case ENDERCHEST_RESET -> new PlayerResetEnderChestEvent(world, island, player);
70+
case MONEY_RESET -> new PlayerResetMoneyEvent(world, island, player);
71+
case HEALTH_RESET -> new PlayerResetHealthEvent(world, island, player);
72+
case HUNGER_RESET -> new PlayerResetHungerEvent(world, island, player);
73+
case EXP_RESET -> new PlayerResetExpEvent(world, island, player);
74+
case UNKNOWN -> new PlayerUnknownEvent(world, island, player);
75+
};
76+
}
77+
78+
/**
79+
* Build the event and call it
80+
* @return event
81+
*/
82+
public PlayerBaseEvent build() {
83+
// Generate new event
84+
PlayerBaseEvent newEvent = getEvent();
85+
Bukkit.getPluginManager().callEvent(newEvent);
86+
return newEvent;
87+
}
88+
}
89+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package world.bentobox.bentobox.api.events.player;
2+
3+
import org.bukkit.World;
4+
import world.bentobox.bentobox.database.objects.Island;
5+
6+
import java.util.UUID;
7+
8+
/**
9+
* Fired when a player's ender chest is cleared as part of an island reset.
10+
* <p>
11+
* This event is cancellable. If cancelled, the ender chest contents will not be cleared.
12+
* </p>
13+
*
14+
* @author tastybento
15+
* @see PlayerBaseEvent
16+
*/
17+
public class PlayerResetEnderChestEvent extends PlayerBaseEvent {
18+
19+
/**
20+
* Constructs a new PlayerResetEnderChestEvent.
21+
*
22+
* @param world the world in which the reset is occurring
23+
* @param island the island being reset, or {@code null} if not applicable
24+
* @param player the UUID of the player whose ender chest is being cleared
25+
*/
26+
public PlayerResetEnderChestEvent(World world, Island island, UUID player) {
27+
super(player, island, world);
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package world.bentobox.bentobox.api.events.player;
2+
3+
import org.bukkit.World;
4+
import world.bentobox.bentobox.database.objects.Island;
5+
6+
import java.util.UUID;
7+
8+
/**
9+
* Fired when a player's experience (XP) is reset as part of an island reset.
10+
* <p>
11+
* This event is cancellable. If cancelled, the player's experience will not be cleared.
12+
* </p>
13+
*
14+
* @author tastybento
15+
* @see PlayerBaseEvent
16+
*/
17+
public class PlayerResetExpEvent extends PlayerBaseEvent {
18+
19+
/**
20+
* Constructs a new PlayerResetExpEvent.
21+
*
22+
* @param world the world in which the reset is occurring
23+
* @param island the island being reset, or {@code null} if not applicable
24+
* @param player the UUID of the player whose experience is being reset
25+
*/
26+
public PlayerResetExpEvent(World world, Island island, UUID player) {
27+
super(player, island, world);
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package world.bentobox.bentobox.api.events.player;
2+
3+
import org.bukkit.World;
4+
import world.bentobox.bentobox.database.objects.Island;
5+
6+
import java.util.UUID;
7+
8+
/**
9+
* Fired when a player's health is reset to the maximum value as part of an island reset.
10+
* <p>
11+
* This event is cancellable. If cancelled, the player's health will not be reset.
12+
* </p>
13+
*
14+
* @author tastybento
15+
* @see PlayerBaseEvent
16+
*/
17+
public class PlayerResetHealthEvent extends PlayerBaseEvent {
18+
19+
/**
20+
* Constructs a new PlayerResetHealthEvent.
21+
*
22+
* @param world the world in which the reset is occurring
23+
* @param island the island being reset, or {@code null} if not applicable
24+
* @param player the UUID of the player whose health is being reset
25+
*/
26+
public PlayerResetHealthEvent(World world, Island island, UUID player) {
27+
super(player, island, world);
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package world.bentobox.bentobox.api.events.player;
2+
3+
import org.bukkit.World;
4+
import world.bentobox.bentobox.database.objects.Island;
5+
6+
import java.util.UUID;
7+
8+
/**
9+
* Fired when a player's hunger (food level) is reset to the maximum value as part of an island reset.
10+
* <p>
11+
* This event is cancellable. If cancelled, the player's hunger will not be reset.
12+
* </p>
13+
*
14+
* @author tastybento
15+
* @see PlayerBaseEvent
16+
*/
17+
public class PlayerResetHungerEvent extends PlayerBaseEvent {
18+
19+
/**
20+
* Constructs a new PlayerResetHungerEvent.
21+
*
22+
* @param world the world in which the reset is occurring
23+
* @param island the island being reset, or {@code null} if not applicable
24+
* @param player the UUID of the player whose hunger is being reset
25+
*/
26+
public PlayerResetHungerEvent(World world, Island island, UUID player) {
27+
super(player, island, world);
28+
}
29+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package world.bentobox.bentobox.api.events.player;
2+
3+
import org.bukkit.World;
4+
import org.bukkit.event.HandlerList;
5+
import org.eclipse.jdt.annotation.NonNull;
6+
import world.bentobox.bentobox.database.objects.Island;
7+
8+
import java.util.UUID;
9+
10+
/**
11+
* Fired when a player's inventory is cleared as part of an island reset.
12+
* <p>
13+
* This event is cancellable. If cancelled, the player's inventory will not be cleared.
14+
* This is the only reset event that exposes a {@link HandlerList}, making it the canonical
15+
* example for the reset-event family.
16+
* </p>
17+
*
18+
* @author tastybento
19+
* @see PlayerBaseEvent
20+
*/
21+
public class PlayerResetInventoryEvent extends PlayerBaseEvent {
22+
23+
private static final HandlerList handlers = new HandlerList();
24+
25+
/**
26+
* Returns the list of handlers registered for this event type.
27+
*
28+
* @return the handler list
29+
*/
30+
@Override
31+
public @NonNull HandlerList getHandlers() {
32+
return getHandlerList();
33+
}
34+
35+
/**
36+
* Returns the static handler list for this event type.
37+
* Required by Bukkit's event system so that handlers can be looked up by class.
38+
*
39+
* @return the static handler list
40+
*/
41+
public static HandlerList getHandlerList() {
42+
return handlers;
43+
}
44+
45+
/**
46+
* Constructs a new PlayerResetInventoryEvent.
47+
*
48+
* @param world the world in which the reset is occurring
49+
* @param island the island being reset, or {@code null} if not applicable
50+
* @param player the UUID of the player whose inventory is being cleared
51+
*/
52+
public PlayerResetInventoryEvent(World world, Island island, UUID player) {
53+
// Final variables have to be declared in the constructor
54+
super(player, island, world);
55+
}
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package world.bentobox.bentobox.api.events.player;
2+
3+
import org.bukkit.World;
4+
import world.bentobox.bentobox.database.objects.Island;
5+
6+
import java.util.UUID;
7+
8+
/**
9+
* Fired when a player's in-game money balance is reset to zero (or to the configured starting
10+
* amount) as part of an island reset.
11+
* <p>
12+
* This event is cancellable. If cancelled, the player's balance will not be reset.
13+
* Requires an economy plugin (e.g. Vault) to be present for the underlying action to take effect.
14+
* </p>
15+
*
16+
* @author tastybento
17+
* @see PlayerBaseEvent
18+
*/
19+
public class PlayerResetMoneyEvent extends PlayerBaseEvent {
20+
21+
/**
22+
* Constructs a new PlayerResetMoneyEvent.
23+
*
24+
* @param world the world in which the reset is occurring
25+
* @param island the island being reset, or {@code null} if not applicable
26+
* @param player the UUID of the player whose balance is being reset
27+
*/
28+
public PlayerResetMoneyEvent(World world, Island island, UUID player) {
29+
super(player, island, world);
30+
}
31+
}

0 commit comments

Comments
 (0)