From 0bfb3b23b67fad1785d562766a8ee3af4300dcb4 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Thu, 17 Sep 2020 18:23:01 -0700 Subject: [PATCH] DirtyMap (renamed from DirtyDataMap) treated as the map instead of a container --- .../{DirtyDataMap.java => DirtyMap.java} | 78 +++++++++++-------- .../nossr50/datatypes/player/McMMOPlayer.java | 2 +- .../experience/MMOExperienceBarManager.java | 9 +-- 3 files changed, 49 insertions(+), 40 deletions(-) rename src/main/java/com/gmail/nossr50/datatypes/dirtydata/{DirtyDataMap.java => DirtyMap.java} (64%) diff --git a/src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyDataMap.java b/src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyMap.java similarity index 64% rename from src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyDataMap.java rename to src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyMap.java index ada25a90a..1b3cd175f 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyDataMap.java +++ b/src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyMap.java @@ -11,13 +11,13 @@ import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; -public class DirtyDataMap implements Map { +public class DirtyMap implements Map { private final @NotNull MutableBoolean dirtyFlag; //Can be pointed at a reference - private @NotNull Map dataMap; + private @NotNull Map map; - public DirtyDataMap(@NotNull Map data, @NotNull MutableBoolean referenceFlag) { - this.dataMap = data; + public DirtyMap(@NotNull Map data, @NotNull MutableBoolean referenceFlag) { + this.map = data; this.dirtyFlag = referenceFlag; } @@ -29,154 +29,164 @@ public class DirtyDataMap implements Map { dirtyFlag.setBoolean(true); } - public void setData(@NotNull Map dataMap) { - this.dataMap = dataMap; + /** + * Change the map contained in this wrapper + * @param dataMap the map to wrap around instead of the current map + */ + public void setMap(@NotNull Map dataMap) { + this.map = dataMap; setDirty(); } - public @NotNull Map getDataMap() { + /** + * Get the inner map that this DirtyMap is wrapping + * @return the inner map of this DirtyMap + */ + public @NotNull Map unwrapMap() { setDirty(); - return dataMap; + return map; } /* Map Interface Delegates */ @Override public V get(Object key) { - return dataMap.get(key); + return map.get(key); } public int size() { - return dataMap.size(); + return map.size(); } public boolean isEmpty() { - return dataMap.isEmpty(); + return map.isEmpty(); } @Override public boolean containsKey(Object key) { - return dataMap.containsKey(key); + return map.containsKey(key); } @Override public boolean containsValue(Object value) { - return dataMap.containsValue(value); + return map.containsValue(value); } public V put(K key, V value) { setDirty(); - return dataMap.put(key, value); + return map.put(key, value); } public V remove(Object key) { setDirty(); - return dataMap.remove(key); + return map.remove(key); } public void putAll(@NotNull Map m) { setDirty(); - dataMap.putAll(m); + map.putAll(m); } public void clear() { setDirty(); - dataMap.clear(); + map.clear(); } @Override public @NotNull Set keySet() { setDirty(); - return dataMap.keySet(); + return map.keySet(); } @Override public @NotNull Collection values() { setDirty(); - return dataMap.values(); + return map.values(); } @Override public @NotNull Set> entrySet() { setDirty(); - return dataMap.entrySet(); + return map.entrySet(); } @Override public V getOrDefault(Object key, V defaultValue) { - return dataMap.getOrDefault(key, defaultValue); + return map.getOrDefault(key, defaultValue); } @Override public void forEach(BiConsumer action) { setDirty(); - dataMap.forEach(action); + map.forEach(action); } @Override public void replaceAll(BiFunction function) { setDirty(); - dataMap.replaceAll(function); + map.replaceAll(function); } @Override public V putIfAbsent(K key, V value) { setDirty(); - return dataMap.putIfAbsent(key, value); + return map.putIfAbsent(key, value); } @Override public boolean remove(Object key, Object value) { setDirty(); - return dataMap.remove(key, value); + return map.remove(key, value); } @Override public boolean replace(K key, V oldValue, V newValue) { setDirty(); - return dataMap.replace(key, oldValue, newValue); + return map.replace(key, oldValue, newValue); } @Override public V replace(K key, V value) { setDirty(); - return dataMap.replace(key, value); + return map.replace(key, value); } @Override public V computeIfAbsent(K key, @NotNull Function mappingFunction) { setDirty(); - return dataMap.computeIfAbsent(key, mappingFunction); + return map.computeIfAbsent(key, mappingFunction); } @Override public V computeIfPresent(K key, @NotNull BiFunction remappingFunction) { setDirty(); - return dataMap.computeIfPresent(key, remappingFunction); + return map.computeIfPresent(key, remappingFunction); } @Override public V compute(K key, @NotNull BiFunction remappingFunction) { setDirty(); - return dataMap.compute(key, remappingFunction); + return map.compute(key, remappingFunction); } @Override public V merge(K key, @NotNull V value, @NotNull BiFunction remappingFunction) { setDirty(); - return dataMap.merge(key, value, remappingFunction); + return map.merge(key, value, remappingFunction); } + /* Override for equals and hash */ + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - DirtyDataMap that = (DirtyDataMap) o; - return Objects.equal(getDataMap(), that.getDataMap()); + DirtyMap that = (DirtyMap) o; + return Objects.equal(map, that.map); } @Override public int hashCode() { - return Objects.hashCode(getDataMap()); + return Objects.hashCode(map); } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java index 83706df2b..991c6ddcc 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -120,7 +120,7 @@ public class McMMOPlayer extends PlayerProfile { superAbilityManager = new SuperAbilityManager(this); abilityActivationProcessor = new AbilityActivationProcessor(this); - experienceBarManager = new MMOExperienceBarManager(this, persistentPlayerData.getDirtyBarStateMap()); + experienceBarManager = new MMOExperienceBarManager(this, persistentPlayerData.getBarStateMap()); //Update last login updateLastLogin(); diff --git a/src/main/java/com/gmail/nossr50/util/experience/MMOExperienceBarManager.java b/src/main/java/com/gmail/nossr50/util/experience/MMOExperienceBarManager.java index 97dba5fcd..bff914c10 100644 --- a/src/main/java/com/gmail/nossr50/util/experience/MMOExperienceBarManager.java +++ b/src/main/java/com/gmail/nossr50/util/experience/MMOExperienceBarManager.java @@ -1,7 +1,6 @@ package com.gmail.nossr50.util.experience; import com.gmail.nossr50.config.experience.ExperienceConfig; -import com.gmail.nossr50.datatypes.dirtydata.DirtyDataMap; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.mcMMO; @@ -23,13 +22,13 @@ public class MMOExperienceBarManager { int delaySeconds = 3; - private @NotNull final DirtyDataMap barStateMapRef; + private @NotNull final Map barStateMapRef; - private @NotNull EnumMap experienceBars; - private @NotNull EnumMap experienceBarHideTaskHashMap; + private @NotNull final EnumMap experienceBars; + private @NotNull final EnumMap experienceBarHideTaskHashMap; - public MMOExperienceBarManager(@NotNull McMMOPlayer mmoPlayer, @NotNull DirtyDataMap barStateMapRef) + public MMOExperienceBarManager(@NotNull McMMOPlayer mmoPlayer, @NotNull Map barStateMapRef) { this.mmoPlayer = mmoPlayer; this.barStateMapRef = barStateMapRef;