Add PlayerPropertyChangeEvent

This commit is contained in:
KingRainbow44 2023-08-29 17:25:48 -04:00
parent 4f2d3f9b30
commit 48439e7e5d
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
2 changed files with 43 additions and 3 deletions

View File

@ -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.

View File

@ -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;
}
}