Skip to content

Commit d477d0f

Browse files
committed
get old value when updating DataEntry
1 parent d45a5c4 commit d477d0f

File tree

7 files changed

+18
-11
lines changed

7 files changed

+18
-11
lines changed

agent/core/src/main/java/me/hsgamer/topper/agent/core/DataEntryAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ default void onCreate(DataEntry<K, V> entry) {
77
// EMPTY
88
}
99

10-
default void onUpdate(DataEntry<K, V> entry) {
10+
default void onUpdate(DataEntry<K, V> entry, V oldValue) {
1111
// EMPTY
1212
}
1313

agent/holder/src/main/java/me/hsgamer/topper/agent/holder/AgentDataHolder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ protected final void onCreate(DataEntry<K, V> entry) {
3939
}
4040

4141
@Override
42-
protected final void onUpdate(DataEntry<K, V> entry) {
43-
entryAgentList.forEach(agent -> agent.onUpdate(entry));
42+
protected void onUpdate(DataEntry<K, V> entry, V oldValue) {
43+
entryAgentList.forEach(agent -> agent.onUpdate(entry, oldValue));
4444
}
4545

4646
@Override

agent/storage/src/main/java/me/hsgamer/topper/agent/storage/StorageAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void beforeStop() {
102102
}
103103

104104
@Override
105-
public void onUpdate(DataEntry<K, V> entry) {
105+
public void onUpdate(DataEntry<K, V> entry, V oldValue) {
106106
queue.add(new AbstractMap.SimpleImmutableEntry<>(entry.getKey(), entry.getValue()));
107107
}
108108

core/src/main/java/me/hsgamer/topper/core/DataEntry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public void setValue(UnaryOperator<V> operator) {
3232

3333
public void setValue(V value, boolean notify) {
3434
if (Objects.equals(this.value, value)) return;
35+
V oldValue = this.value;
3536
this.value = value;
36-
if (notify) holder.onUpdate(this);
37+
if (notify) holder.onUpdate(this, oldValue);
3738
}
3839

3940
public void setValue(UnaryOperator<V> operator, boolean notify) {

core/src/main/java/me/hsgamer/topper/core/DataHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected void onCreate(DataEntry<K, V> entry) {
2323
protected void onRemove(DataEntry<K, V> entry) {
2424
}
2525

26-
protected void onUpdate(DataEntry<K, V> entry) {
26+
protected void onUpdate(DataEntry<K, V> entry, V oldValue) {
2727
}
2828

2929
public final String getName() {

spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/event/GenericEntryUpdateEvent.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ public class GenericEntryUpdateEvent extends Event {
1313
private final String group;
1414
private final String holder;
1515
private final UUID uuid;
16+
private final @Nullable Double oldValue;
1617
private final @Nullable Double value;
1718

18-
public GenericEntryUpdateEvent(String group, String holder, UUID uuid, @Nullable Double value, boolean async) {
19+
public GenericEntryUpdateEvent(String group, String holder, UUID uuid, @Nullable Double oldValue, @Nullable Double value, boolean async) {
1920
super(async);
2021
this.group = group;
2122
this.holder = holder;
2223
this.uuid = uuid;
24+
this.oldValue = oldValue;
2325
this.value = value;
2426
}
2527

26-
public GenericEntryUpdateEvent(String holder, UUID uuid, @Nullable Double value, boolean async) {
27-
this(DEFAULT_GROUP, holder, uuid, value, async);
28+
public GenericEntryUpdateEvent(String holder, UUID uuid, @Nullable Double oldValue, @Nullable Double value, boolean async) {
29+
this(DEFAULT_GROUP, holder, uuid, oldValue, value, async);
2830
}
2931

3032
public static HandlerList getHandlerList() {
@@ -43,6 +45,10 @@ public UUID getUuid() {
4345
return uuid;
4446
}
4547

48+
public @Nullable Double getOldValue() {
49+
return oldValue;
50+
}
51+
4652
public @Nullable Double getValue() {
4753
return value;
4854
}

spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/holder/NumberTopHolder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public void start() {
7171
});
7272
addEntryAgent(new DataEntryAgent<UUID, Double>() {
7373
@Override
74-
public void onUpdate(DataEntry<UUID, Double> entry) {
75-
Bukkit.getPluginManager().callEvent(new GenericEntryUpdateEvent(name, entry.getKey(), entry.getValue(), true));
74+
public void onUpdate(DataEntry<UUID, Double> entry, Double oldValue) {
75+
Bukkit.getPluginManager().callEvent(new GenericEntryUpdateEvent(name, entry.getKey(), oldValue, entry.getValue(), true));
7676
}
7777
});
7878
}

0 commit comments

Comments
 (0)