mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-29 02:27:37 +00:00
Add PlayerPropertyChangeEvent
This commit is contained in:
parent
4f2d3f9b30
commit
48439e7e5d
@ -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.
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user