-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathFcData.java
More file actions
177 lines (155 loc) · 6.48 KB
/
FcData.java
File metadata and controls
177 lines (155 loc) · 6.48 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.froobworld.farmcontrol.data;
import com.froobworld.farmcontrol.FarmControl;
import com.froobworld.farmcontrol.controller.action.Action;
import com.froobworld.farmcontrol.controller.trigger.Trigger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.persistence.PersistentDataAdapterContext;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
public class FcData {
private static final Map<Entity, FcData> dataCache = new WeakHashMap<>();
private static final NamespacedKey KEY = new NamespacedKey(FarmControl.getPlugin(FarmControl.class), "data");
private static final PersistentDataType<String, FcData> TYPE = new PersistentDataType<String, FcData>() {
private final Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
@Override
public @NotNull Class<String> getPrimitiveType() {
return String.class;
}
@Override
public @NotNull Class<FcData> getComplexType() {
return FcData.class;
}
@NotNull
@Override
public String toPrimitive(@NotNull FcData fcData, @NotNull PersistentDataAdapterContext persistentDataAdapterContext) {
return gson.toJson(fcData);
}
@NotNull
@Override
public FcData fromPrimitive(@NotNull String s, @NotNull PersistentDataAdapterContext persistentDataAdapterContext) {
FcData fcData = gson.fromJson(s, FcData.class);
fcData.postLoad();
return fcData;
}
};
@Expose
private final ConcurrentHashMap<String, Set<String>> persistentActionTriggerMap = new ConcurrentHashMap<>();
@Expose
private final ConcurrentHashMap<String, Set<String>> persistentTriggerActionMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Set<String>> actionTriggerMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Set<String>> triggerActionMap = new ConcurrentHashMap<>();
private boolean dirty = false;
private FcData() {
}
public Set<String> getActions(Trigger trigger) {
return triggerActionMap.get(trigger.getName());
}
public Set<String> getTriggers(Action action) {
return actionTriggerMap.get(action.getName());
}
public Set<String> getActions() {
return actionTriggerMap.keySet();
}
public boolean add(Trigger trigger, Action action) {
boolean previouslyActioned = actionTriggerMap.containsKey(action.getName());
actionTriggerMap.computeIfAbsent(action.getName(), a -> new HashSet<>()).add(trigger.getName());
triggerActionMap.computeIfAbsent(trigger.getName(), t -> new HashSet<>()).add(action.getName());
if (action.isPersistent()) {
if (persistentActionTriggerMap.computeIfAbsent(action.getName(), a -> new HashSet<>()).add(trigger.getName())) {
dirty = true;
}
persistentTriggerActionMap.computeIfAbsent(trigger.getName(), t -> new HashSet<>()).add(action.getName());
}
return !previouslyActioned;
}
public boolean remove(Trigger trigger, Action action) {
if (actionTriggerMap.containsKey(action.getName())) {
Set<String> triggers = actionTriggerMap.get(action.getName());
triggers.remove(trigger.getName());
if (triggers.isEmpty()) {
actionTriggerMap.remove(action.getName());
}
}
if (persistentActionTriggerMap.containsKey(action.getName())) {
Set<String> triggers = persistentActionTriggerMap.get(action.getName());
if (triggers.remove(trigger.getName())) {
dirty = true;
}
if (triggers.isEmpty()) {
persistentActionTriggerMap.remove(action.getName());
}
}
if (triggerActionMap.containsKey(trigger.getName())) {
Set<String> actions = triggerActionMap.get(trigger.getName());
actions.remove(action.getName());
if (actions.isEmpty()) {
triggerActionMap.remove(trigger.getName());
}
}
if (persistentTriggerActionMap.containsKey(trigger.getName())) {
Set<String> actions = persistentTriggerActionMap.get(trigger.getName());
actions.remove(action.getName());
if (actions.isEmpty()) {
persistentTriggerActionMap.remove(trigger.getName());
}
}
return !actionTriggerMap.containsKey(action.getName());
}
public boolean removeAction(Action action) {
if (!actionTriggerMap.containsKey(action.getName())) {
return false;
}
actionTriggerMap.remove(action.getName());
triggerActionMap.values().forEach(actions -> actions.remove(action.getName()));
if (persistentActionTriggerMap.containsKey(action.getName())) {
persistentActionTriggerMap.remove(action.getName());
persistentTriggerActionMap.values().forEach(actions -> actions.remove(action.getName()));
dirty = true;
}
return true;
}
public void save(Entity entity) {
if (dirty) {
entity.getPersistentDataContainer().set(KEY, TYPE, this);
dirty = false;
}
}
private void postLoad() {
actionTriggerMap.putAll(persistentActionTriggerMap);
triggerActionMap.putAll(persistentTriggerActionMap);
}
public static FcData get(Entity entity) {
FcData data = dataCache.get(entity);
if (data == null) {
data = entity.getPersistentDataContainer().get(KEY, TYPE);
if (data != null) {
dataCache.put(entity, data);
}
}
return data;
}
public static FcData getOrCreate(Entity entity) {
FcData data = get(entity);
if (data == null) {
data = new FcData();
dataCache.put(entity, data);
}
return data;
}
public static void removeIfEmpty(Entity entity) {
FcData data = get(entity);
if (data != null && data.actionTriggerMap.isEmpty()) {
entity.getPersistentDataContainer().remove(KEY);
dataCache.remove(entity);
}
}
}