forked from CleanroomMC/BetterQuesting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartyManager.java
More file actions
108 lines (88 loc) · 3.63 KB
/
Copy pathPartyManager.java
File metadata and controls
108 lines (88 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package betterquesting.questing.party;
import betterquesting.api.enums.EnumPartyStatus;
import betterquesting.api.properties.NativeProps;
import betterquesting.api.questing.party.IParty;
import betterquesting.api.questing.party.IPartyDatabase;
import betterquesting.api2.storage.DBEntry;
import betterquesting.api2.storage.SimpleDatabase;
import betterquesting.storage.QuestSettings;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
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 {
public static final PartyManager INSTANCE;
static {
INSTANCE = new PartyManager();
QuestSettings.INSTANCE.addPropertyListener(NativeProps.PARTY_ENABLE,
(partyEnabledProp, isEnabled) -> PartyManager.INSTANCE.partyEnabled.set(isEnabled));
}
private final HashMap<UUID, Integer> partyCache = new HashMap<>();
// Cache PARTY_ENABLED prop due to frequent checks when creating ParticipantInfo in tick handler.
private final AtomicBoolean partyEnabled = new AtomicBoolean(false);
@Override
public synchronized IParty createNew(int id) {
IParty party = new PartyInstance();
if (id >= 0) this.add(id, party);
return party;
}
@Nullable
@Override
public synchronized DBEntry<IParty> getParty(@Nonnull UUID uuid) {
if (!partyEnabled.get())
return null; // We're merely preventing access. Not erasing data
Integer cachedID = partyCache.get(uuid);
IParty cachedParty = cachedID == null ? null : getValue(cachedID);
if (cachedID != null && cachedParty == null) // Disbanded party
{
partyCache.remove(uuid);
} else if (cachedParty != null) // Active party. Check validity...
{
EnumPartyStatus status = cachedParty.getStatus(uuid);
if (status != null) return new DBEntry<>(cachedID, cachedParty);
partyCache.remove(uuid); // User isn't a party member anymore
}
// NOTE: A server with a lot of solo players may still hammer this loop. Optimise further?
for (DBEntry<IParty> entry : getEntries()) {
EnumPartyStatus status = entry.getValue().getStatus(uuid);
if (status != null) {
partyCache.put(uuid, entry.getID());
return entry;
}
}
return null;
}
@Override
public synchronized NBTTagList writeToNBT(NBTTagList nbt, List<Integer> subset) {
for (DBEntry<IParty> entry : getEntries()) {
if (subset != null && !subset.contains(entry.getID())) continue;
NBTTagCompound jp = entry.getValue().writeToNBT(new NBTTagCompound());
jp.setInteger("partyID", entry.getID());
nbt.appendTag(jp);
}
return nbt;
}
@Override
public synchronized void readFromNBT(NBTTagList json, boolean merge) {
if (!merge) reset();
for (int i = 0; i < json.tagCount(); i++) {
NBTTagCompound jp = json.getCompoundTagAt(i);
int partyID = jp.hasKey("partyID", 99) ? jp.getInteger("partyID") : -1;
if (partyID < 0) continue;
IParty party = new PartyInstance();
party.readFromNBT(jp);
if (party.getMembers().size() > 0) {
add(partyID, party);
}
}
}
@Override
public synchronized void reset() {
super.reset();
partyCache.clear();
}
}