Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package betterquesting.api.properties;

/**
* A listener to run when a property's value changes.
* @param <T> The property type
*/
@FunctionalInterface
public interface IPropertyListener<T> {
void propertyChanged(IPropertyType<T> prop, T newValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ public interface IPropertyType<T> {
T readValue(NBTBase nbt);

NBTBase writeValue(T value);

void addListener(IPropertyListener<T> listener);

void notifyListeners(T newValue);
}
Original file line number Diff line number Diff line change
@@ -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<T> implements IPropertyType<T> {
private final ResourceLocation key;
private final T def;
private final List<IPropertyListener<T>> listeners = new ArrayList<>();

public PropertyTypeBase(ResourceLocation key, T def) {
this.key = key;
Expand All @@ -26,16 +21,4 @@ public ResourceLocation getKey() {
public T getDefault() {
return def;
}

@Override
public void addListener(IPropertyListener<T> listener) {
listeners.add(listener);
}

@Override
public void notifyListeners(T newValue) {
for (var listener : listeners) {
listener.propertyChanged(this, newValue);
}
}
}
21 changes: 6 additions & 15 deletions src/main/java/betterquesting/questing/party/PartyManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,26 +9,26 @@
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<IParty> implements IPartyDatabase, IPropertyListener<Boolean> {
public class PartyManager extends SimpleDatabase<IParty> implements IPartyDatabase {
public static final PartyManager INSTANCE;

static {
INSTANCE = new PartyManager();
NativeProps.PARTY_ENABLE.addListener(INSTANCE);
QuestSettings.INSTANCE.addPropertyListener(NativeProps.PARTY_ENABLE,
(partyEnabledProp, isEnabled) -> PartyManager.INSTANCE.partyEnabled.set(isEnabled));
Comment thread
jchung01 marked this conversation as resolved.
Outdated
}

private final HashMap<UUID, Integer> 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) {
Expand All @@ -42,7 +40,7 @@ public synchronized IParty createNew(int id) {
@Nullable
@Override
public synchronized DBEntry<IParty> getParty(@Nonnull UUID uuid) {
if (!partyEnabled)
if (!partyEnabled.get())
return null; // We're merely preventing access. Not erasing data

Integer cachedID = partyCache.get(uuid);
Expand Down Expand Up @@ -107,11 +105,4 @@ public synchronized void reset() {
super.reset();
partyCache.clear();
}

@Override
public void propertyChanged(IPropertyType<Boolean> prop, Boolean newValue) {
if (prop == NativeProps.PARTY_ENABLE && FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) {
partyEnabled = newValue;
}
}
}
18 changes: 17 additions & 1 deletion src/main/java/betterquesting/storage/PropertyContainer.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package betterquesting.storage;

import betterquesting.api.properties.IPropertyContainer;
import betterquesting.api.properties.IPropertyListener;
import betterquesting.api.properties.IPropertyReducible;
import betterquesting.api.properties.IPropertyType;
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;
Expand All @@ -19,6 +23,8 @@ public class PropertyContainer implements IPropertyContainer, INBTSaveLoad<NBTTa
// 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<ResourceLocation, IPropertyType<?>> id2PropertyMap = HashBiMap.create(); // property.getKey() -> property
@SuppressWarnings("UnstableApiUsage")
private final Multimap<ResourceLocation, IPropertyListener<?>> propListeners = MultimapBuilder.hashKeys().arrayListValues().build();

@Override
public synchronized <T> T getProperty(IPropertyType<T> prop) {
Expand Down Expand Up @@ -59,13 +65,19 @@ public synchronized void removeProperty(IPropertyType<?> prop) {
if (jProp.isEmpty()) nbtInfo.removeTag(prop.getKey().getNamespace());
}

@SuppressWarnings("unchecked")
@Override
public synchronized <T> void setProperty(IPropertyType<T> 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 (IPropertyListener<?> listener : propListeners.get(prop.getKey())) {
((IPropertyListener<T>) listener).propertyChanged(prop, value);
}
}

dom.setTag(prop.getKey().getPath(), prop.writeValue(value));
nbtInfo.setTag(prop.getKey().getNamespace(), dom);
}
Expand All @@ -76,6 +88,10 @@ public synchronized void removeAllProps() {
for (String key : keys) nbtInfo.removeTag(key);
}

public synchronized <T> void addPropertyListener(IPropertyType<T> prop, IPropertyListener<T> listener) {
propListeners.put(prop.getKey(), listener);
}

@Deprecated
@Override
public synchronized NBTTagCompound writeToNBT(NBTTagCompound nbt) {
Expand Down
Loading