Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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

This file was deleted.

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);
}
}
}
20 changes: 5 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,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<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, PartyManager.INSTANCE.partyEnabled::set);
}

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 +39,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 +104,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;
}
}
}
19 changes: 18 additions & 1 deletion src/main/java/betterquesting/storage/PropertyContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@
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;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

public class PropertyContainer implements IPropertyContainer, INBTSaveLoad<NBTTagCompound> {
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<ResourceLocation, IPropertyType<?>> 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<ResourceLocation, Consumer<?>> propListeners = MultimapBuilder.hashKeys().arrayListValues().build();

@Override
public synchronized <T> T getProperty(IPropertyType<T> prop) {
Expand Down Expand Up @@ -59,13 +66,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 (Consumer<?> listener : propListeners.get(prop.getKey())) {
((Consumer<T>) listener).accept(value);
}
}

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

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

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