diff --git a/build.gradle b/build.gradle index 09eec4b5c..3f6f3d0e4 100644 --- a/build.gradle +++ b/build.gradle @@ -565,7 +565,7 @@ dependencies { compileOnlyApi 'org.jetbrains:annotations:24.1.0' annotationProcessor 'org.jetbrains:annotations:24.1.0' - patchedMinecraft('net.minecraft:launchwrapper:1.17.2') { + patchedMinecraft('net.minecraft:launchwrapper:1.12') { transitive = false } diff --git a/src/main/java/betterquesting/api/properties/IPropertyListener.java b/src/main/java/betterquesting/api/properties/IPropertyListener.java deleted file mode 100644 index 3cc584f56..000000000 --- a/src/main/java/betterquesting/api/properties/IPropertyListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package betterquesting.api.properties; - -public interface IPropertyListener { - void propertyChanged(IPropertyType prop, T newValue); -} diff --git a/src/main/java/betterquesting/api/properties/IPropertyType.java b/src/main/java/betterquesting/api/properties/IPropertyType.java index 833423551..bf86e3a62 100644 --- a/src/main/java/betterquesting/api/properties/IPropertyType.java +++ b/src/main/java/betterquesting/api/properties/IPropertyType.java @@ -11,8 +11,4 @@ public interface IPropertyType { T readValue(NBTBase nbt); NBTBase writeValue(T value); - - void addListener(IPropertyListener listener); - - void notifyListeners(T newValue); } diff --git a/src/main/java/betterquesting/api/properties/basic/PropertyTypeBase.java b/src/main/java/betterquesting/api/properties/basic/PropertyTypeBase.java index 634863ce1..bdf25fb74 100644 --- a/src/main/java/betterquesting/api/properties/basic/PropertyTypeBase.java +++ b/src/main/java/betterquesting/api/properties/basic/PropertyTypeBase.java @@ -1,16 +1,11 @@ package betterquesting.api.properties.basic; -import java.util.ArrayList; -import java.util.List; - -import betterquesting.api.properties.IPropertyListener; import betterquesting.api.properties.IPropertyType; import net.minecraft.util.ResourceLocation; public abstract class PropertyTypeBase implements IPropertyType { private final ResourceLocation key; private final T def; - private final List> listeners = new ArrayList<>(); public PropertyTypeBase(ResourceLocation key, T def) { this.key = key; @@ -26,16 +21,4 @@ public ResourceLocation getKey() { public T getDefault() { return def; } - - @Override - public void addListener(IPropertyListener listener) { - listeners.add(listener); - } - - @Override - public void notifyListeners(T newValue) { - for (var listener : listeners) { - listener.propertyChanged(this, newValue); - } - } } diff --git a/src/main/java/betterquesting/questing/party/PartyManager.java b/src/main/java/betterquesting/questing/party/PartyManager.java index 936a995ee..fd3322e76 100644 --- a/src/main/java/betterquesting/questing/party/PartyManager.java +++ b/src/main/java/betterquesting/questing/party/PartyManager.java @@ -1,8 +1,6 @@ package betterquesting.questing.party; import betterquesting.api.enums.EnumPartyStatus; -import betterquesting.api.properties.IPropertyListener; -import betterquesting.api.properties.IPropertyType; import betterquesting.api.properties.NativeProps; import betterquesting.api.questing.party.IParty; import betterquesting.api.questing.party.IPartyDatabase; @@ -11,26 +9,25 @@ import betterquesting.storage.QuestSettings; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; -import net.minecraftforge.fml.common.FMLCommonHandler; -import net.minecraftforge.fml.relauncher.Side; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.HashMap; import java.util.List; import java.util.UUID; +import java.util.concurrent.atomic.AtomicBoolean; -public class PartyManager extends SimpleDatabase implements IPartyDatabase, IPropertyListener { +public class PartyManager extends SimpleDatabase implements IPartyDatabase { public static final PartyManager INSTANCE; static { INSTANCE = new PartyManager(); - NativeProps.PARTY_ENABLE.addListener(INSTANCE); + QuestSettings.INSTANCE.addPropertyListener(NativeProps.PARTY_ENABLE, PartyManager.INSTANCE.partyEnabled::set); } private final HashMap partyCache = new HashMap<>(); // Cache PARTY_ENABLED prop due to frequent checks when creating ParticipantInfo in tick handler. - private boolean partyEnabled; + private final AtomicBoolean partyEnabled = new AtomicBoolean(false); @Override public synchronized IParty createNew(int id) { @@ -42,7 +39,7 @@ public synchronized IParty createNew(int id) { @Nullable @Override public synchronized DBEntry getParty(@Nonnull UUID uuid) { - if (!partyEnabled) + if (!partyEnabled.get()) return null; // We're merely preventing access. Not erasing data Integer cachedID = partyCache.get(uuid); @@ -107,11 +104,4 @@ public synchronized void reset() { super.reset(); partyCache.clear(); } - - @Override - public void propertyChanged(IPropertyType prop, Boolean newValue) { - if (prop == NativeProps.PARTY_ENABLE && FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) { - partyEnabled = newValue; - } - } } diff --git a/src/main/java/betterquesting/storage/PropertyContainer.java b/src/main/java/betterquesting/storage/PropertyContainer.java index e939f3c84..a8959ea73 100644 --- a/src/main/java/betterquesting/storage/PropertyContainer.java +++ b/src/main/java/betterquesting/storage/PropertyContainer.java @@ -6,6 +6,9 @@ import betterquesting.api2.storage.INBTSaveLoad; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; +import com.google.common.collect.Multimap; +import com.google.common.collect.MultimapBuilder; + import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ResourceLocation; @@ -13,12 +16,16 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.function.Consumer; public class PropertyContainer implements IPropertyContainer, INBTSaveLoad { private final NBTTagCompound nbtInfo = new NBTTagCompound(); // For reducing nbt // To hold nbt values if the properties are not used (ex: the addon is temporarily removed), we cache and use only used properties to reduce nbt. private final BiMap> id2PropertyMap = HashBiMap.create(); // property.getKey() -> property + /* Key is property type's key, value is consumer of the property's new value. */ + @SuppressWarnings("UnstableApiUsage") + private final Multimap> propListeners = MultimapBuilder.hashKeys().arrayListValues().build(); @Override public synchronized T getProperty(IPropertyType prop) { @@ -59,13 +66,19 @@ public synchronized void removeProperty(IPropertyType prop) { if (jProp.isEmpty()) nbtInfo.removeTag(prop.getKey().getNamespace()); } + @SuppressWarnings("unchecked") @Override public synchronized void setProperty(IPropertyType prop, T value) { if (prop == null || value == null) return; id2PropertyMap.put(prop.getKey(), prop); NBTTagCompound dom = getDomain(prop.getKey()); - prop.notifyListeners(value); + if (propListeners.containsKey(prop.getKey())) { + for (Consumer listener : propListeners.get(prop.getKey())) { + ((Consumer) listener).accept(value); + } + } + dom.setTag(prop.getKey().getPath(), prop.writeValue(value)); nbtInfo.setTag(prop.getKey().getNamespace(), dom); } @@ -76,6 +89,10 @@ public synchronized void removeAllProps() { for (String key : keys) nbtInfo.removeTag(key); } + public synchronized void addPropertyListener(IPropertyType prop, Consumer listener) { + propListeners.put(prop.getKey(), listener); + } + @Deprecated @Override public synchronized NBTTagCompound writeToNBT(NBTTagCompound nbt) {