diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index f2d853c25..39442cb01 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -1491,11 +1491,28 @@ public class Player implements PlayerHook, FieldFetch { } } + /** + * Applies a property change to this player. + * Checks the value against the property's min and max values for sanity. + * + * @param prop The property to change. + * @param value The new value. + * @param sendPacket Whether to send a packet to the client. + * @return Whether the property was changed. + */ private boolean setPropertyWithSanityCheck(PlayerProperty prop, int value, boolean sendPacket) { - int min = this.getPropertyMin(prop); - int max = this.getPropertyMax(prop); + var currentValue = this.properties.getOrDefault(prop.getId(), 0); + + // Call PlayerPropertyChangeEvent. + var event = new PlayerPropertyChangeEvent(this, prop, currentValue, value); + if (!event.call()) return false; + + prop = event.getProperty(); + value = event.getNewValue(); + + var min = this.getPropertyMin(prop); + var max = this.getPropertyMax(prop); if (min <= value && value <= max) { - int currentValue = this.properties.getOrDefault(prop.getId(), 0); this.properties.put(prop.getId(), value); if (sendPacket) { // Send property change reasons if needed. diff --git a/src/main/java/emu/grasscutter/server/event/player/PlayerPropertyChangeEvent.java b/src/main/java/emu/grasscutter/server/event/player/PlayerPropertyChangeEvent.java new file mode 100644 index 000000000..d1f1f1629 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/player/PlayerPropertyChangeEvent.java @@ -0,0 +1,23 @@ +package emu.grasscutter.server.event.player; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.game.props.PlayerProperty; +import emu.grasscutter.server.event.Cancellable; +import emu.grasscutter.server.event.types.PlayerEvent; +import lombok.*; + +@Getter +public final class PlayerPropertyChangeEvent extends PlayerEvent implements Cancellable { + @Setter private PlayerProperty property; + private final int oldValue; + @Setter private int newValue; + + public PlayerPropertyChangeEvent( + Player player, PlayerProperty property, int oldValue, int newValue) { + super(player); + + this.property = property; + this.oldValue = oldValue; + this.newValue = newValue; + } +}