Skip to content

Commit b232904

Browse files
Pablete1234twizmwazin
authored andcommitted
Add id module
1 parent b985841 commit b232904

File tree

13 files changed

+304
-226
lines changed

13 files changed

+304
-226
lines changed

src/main/java/in/twizmwaz/cardinal/module/apply/regions/WoolMonumentPlace.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import ee.ellytr.chat.ChatConstant;
2929
import ee.ellytr.chat.component.builder.LocalizedComponentBuilder;
30-
import in.twizmwaz.cardinal.component.TeamComponentBuilder;
30+
import in.twizmwaz.cardinal.component.team.TeamComponentBuilder;
3131
import in.twizmwaz.cardinal.module.apply.AppliedRegion;
3232
import in.twizmwaz.cardinal.module.apply.ApplyType;
3333
import in.twizmwaz.cardinal.module.filter.type.MaterialFilter;

src/main/java/in/twizmwaz/cardinal/module/filter/FilterModule.java

+7-17
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
package in.twizmwaz.cardinal.module.filter;
2727

28-
import com.google.common.collect.Maps;
2928
import in.twizmwaz.cardinal.Cardinal;
3029
import in.twizmwaz.cardinal.event.match.MatchModuleLoadCompleteEvent;
3130
import in.twizmwaz.cardinal.match.Match;
@@ -75,6 +74,7 @@
7574
import in.twizmwaz.cardinal.module.filter.type.modifiers.NotFilter;
7675
import in.twizmwaz.cardinal.module.filter.type.modifiers.OneFilter;
7776
import in.twizmwaz.cardinal.module.filter.type.modifiers.RangeFilter;
77+
import in.twizmwaz.cardinal.module.id.IdModule;
7878
import in.twizmwaz.cardinal.module.region.RegionModule;
7979
import in.twizmwaz.cardinal.module.team.TeamModule;
8080
import lombok.NonNull;
@@ -84,13 +84,10 @@
8484
import org.jdom2.located.Located;
8585

8686
import java.util.Collection;
87-
import java.util.Map;
8887

89-
@ModuleEntry(depends = {RegionModule.class, TeamModule.class})
88+
@ModuleEntry(depends = {IdModule.class, RegionModule.class, TeamModule.class})
9089
public class FilterModule extends AbstractModule implements Listener {
9190

92-
private Map<Match, Map<String, Filter>> filters = Maps.newHashMap();
93-
9491
//Static filters. Can be shared across matches, because they use no arguments
9592
public static final Filter ALLOW = new StaticFilter(FilterState.ALLOW);
9693
public static final Filter ABSTAIN = new StaticFilter(FilterState.ABSTAIN);
@@ -113,11 +110,10 @@ public FilterModule() {
113110

114111
@Override
115112
public boolean loadMatch(@NonNull Match match) {
116-
filters.put(match, Maps.newHashMap());
113+
IdModule.get().add(match, "always", ALLOW);
114+
IdModule.get().add(match, "deny", DENY);
117115
for (Element filtersElement : match.getMap().getDocument().getRootElement().getChildren("filters")) {
118116
for (Element filterElement : filtersElement.getChildren()) {
119-
filters.get(match).put("always", ALLOW);
120-
filters.get(match).put("never", DENY);
121117
try {
122118
getFilter(match, filterElement);
123119
} catch (FilterException e) {
@@ -128,17 +124,12 @@ public boolean loadMatch(@NonNull Match match) {
128124
return true;
129125
}
130126

131-
@Override
132-
public void clearMatch(@NonNull Match match) {
133-
filters.remove(match);
134-
}
135-
136127
/**
137128
* Runs load() on filters that implement the LoadLateFilter interface.
138129
*/
139130
@EventHandler
140131
public void onModuleLoad(MatchModuleLoadCompleteEvent event) {
141-
loadFilters(event.getMatch(), filters.get(event.getMatch()).values());
132+
loadFilters(event.getMatch(), IdModule.get().getList(event.getMatch(), Filter.class));
142133
}
143134

144135
/**
@@ -155,7 +146,7 @@ public void loadFilters(Match match, Collection<Filter> filters) {
155146
}
156147

157148
public Filter getFilter(@NonNull Match match, @NonNull String id) {
158-
return filters.get(match).get(id);
149+
return IdModule.get().get(match, id, Filter.class);
159150
}
160151

161152
/**
@@ -332,11 +323,10 @@ public Filter getFilter(Match match, Element element, String... alternateAttribu
332323

333324
private Filter checkFilter(Match match, String id, Filter filter) throws FilterException {
334325
if (id != null) {
335-
if (!filters.get(match).containsKey(id)) {
326+
if (!IdModule.get().add(match, id, filter)) {
336327
//Fixme: needs descriptive exception
337328
throw new FilterException();
338329
}
339-
filters.get(match).put(id, filter);
340330
}
341331
return filter;
342332
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Copyright (c) 2016, Kevin Phoenix
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
package in.twizmwaz.cardinal.module.id;
27+
28+
import com.google.common.collect.Lists;
29+
import in.twizmwaz.cardinal.match.Match;
30+
import in.twizmwaz.cardinal.module.AbstractModule;
31+
import in.twizmwaz.cardinal.module.ModuleEntry;
32+
import lombok.NonNull;
33+
34+
import java.util.HashMap;
35+
import java.util.List;
36+
import java.util.Map;
37+
import java.util.UUID;
38+
39+
@ModuleEntry
40+
public class IdModule extends AbstractModule {
41+
42+
private static IdModule idModule;
43+
private final Map<Match, Map<String, Object>> ids = new HashMap<>();
44+
45+
public IdModule() {
46+
IdModule.idModule = this;
47+
}
48+
49+
@Override
50+
public boolean loadMatch(@NonNull Match match) {
51+
ids.put(match, new HashMap<>());
52+
return true;
53+
}
54+
55+
@Override
56+
public void clearMatch(@NonNull Match match) {
57+
if (ids.containsKey(match)) {
58+
ids.remove(match);
59+
}
60+
}
61+
62+
/**
63+
* Gets the id module, used for easy access from outside classes.
64+
* @return the IdModule.
65+
*/
66+
public static IdModule get() {
67+
return idModule;
68+
}
69+
70+
/**
71+
* Adds an object to a match, only if the id is not already present.
72+
* @param match The match to add the object to.
73+
* @param id The id to add the object with, can't be duplicated.
74+
* @param object The object to add.
75+
* @return if the object was successfully added.
76+
*/
77+
public boolean add(Match match, String id, Object object) {
78+
return add(match, id, object, false);
79+
}
80+
81+
/**
82+
* Adds an object to a match, only if the id is not already present or it will be added with a random id if force.
83+
* @param match The match to add the object to.
84+
* @param id The id to add the object with, can't be duplicated.
85+
* @param object The object to add.
86+
* @param force Add the object with a random id if the id is null or already in use.
87+
* @return if the object was successfully added with the provided id.
88+
*/
89+
public boolean add(Match match, String id, Object object, boolean force) {
90+
if (id != null && !ids.get(match).containsKey(id)) {
91+
ids.get(match).put(id, object);
92+
return true;
93+
} else if (force) {
94+
add(match, UUID.randomUUID().toString(), object, true);
95+
}
96+
return false;
97+
}
98+
99+
/**
100+
* Get an object with the given id.
101+
* @param match The match the object belongs to.
102+
* @param id The id of the object.
103+
* @param clazz The class of the object you want to retrieve.
104+
* @param <T> The object type.
105+
* @return The object if id is present and it's the correct type, null otherwise.
106+
*/
107+
public <T> T get(Match match, String id, Class<T> clazz) {
108+
return get(match, id, clazz, false);
109+
}
110+
111+
public <T> T get(Match match, String id, Class<T> clazz, boolean caseSensitive) {
112+
if (ids.get(match).containsKey(id)) {
113+
Object obj = ids.get(match).get(id);
114+
if (clazz.isInstance(obj)) {
115+
return (T) obj;
116+
}
117+
}
118+
if (!caseSensitive) {
119+
return null;
120+
}
121+
Map<String, T> map = getMap(match, clazz);
122+
123+
for (String idVal : map.keySet()) {
124+
if (idVal.replaceAll(" ", "").equalsIgnoreCase(id.replaceAll(" ", ""))) {
125+
return map.get(idVal);
126+
}
127+
}
128+
for (String idVal : map.keySet()) {
129+
if (idVal.replaceAll(" ", "").toLowerCase().startsWith(id.replaceAll(" ", "").toLowerCase())) {
130+
return map.get(idVal);
131+
}
132+
}
133+
for (String idVal : map.keySet()) {
134+
if (idVal.replaceAll(" ", "-").equalsIgnoreCase(id.replaceAll(" ", "-"))) {
135+
return map.get(idVal);
136+
}
137+
}
138+
for (String idVal : map.keySet()) {
139+
if (idVal.replaceAll(" ", "-").toLowerCase().startsWith(id.replaceAll(" ", "-").toLowerCase())) {
140+
return map.get(idVal);
141+
}
142+
}
143+
return null;
144+
}
145+
146+
147+
/**
148+
* Get a list of all objects of the given class
149+
* @param match The match the objects belong to.
150+
* @param clazz The class of the objects you want to retrieve.
151+
* @param <T> The object type.
152+
* @return A List with all the objects of that type.
153+
*/
154+
public <T> List<T> getList(Match match, Class<T> clazz) {
155+
List<T> result = Lists.newArrayList();
156+
for (Object obj : ids.get(match).values()) {
157+
if (clazz.isInstance(obj)) {
158+
result.add((T) obj);
159+
}
160+
}
161+
return result;
162+
}
163+
164+
/**
165+
* Get a id object map for all objects of the given class
166+
* @param match The match the objects belong to.
167+
* @param clazz The class of the objects you want to retrieve.
168+
* @param <T> The object type.
169+
* @return A map with all the objects of that type and their id's.
170+
*/
171+
public <T> Map<String, T> getMap(Match match, Class<T> clazz) {
172+
Map<String, T> result = new HashMap<>();
173+
for (Map.Entry<String, Object> entry : ids.get(match).entrySet()) {
174+
if (clazz.isInstance(entry.getValue())) {
175+
result.put(entry.getKey(), (T) entry.getValue());
176+
}
177+
}
178+
return result;
179+
}
180+
181+
}

src/main/java/in/twizmwaz/cardinal/module/kit/KitModule.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
package in.twizmwaz.cardinal.module.kit;
2727

2828
import com.google.common.collect.Lists;
29-
import com.google.common.collect.Maps;
3029
import com.google.common.collect.Sets;
3130
import in.twizmwaz.cardinal.Cardinal;
3231
import in.twizmwaz.cardinal.match.Match;
3332
import in.twizmwaz.cardinal.module.AbstractModule;
3433
import in.twizmwaz.cardinal.module.ModuleEntry;
3534
import in.twizmwaz.cardinal.module.filter.Filter;
3635
import in.twizmwaz.cardinal.module.filter.FilterModule;
36+
import in.twizmwaz.cardinal.module.id.IdModule;
3737
import in.twizmwaz.cardinal.module.kit.listener.DoubleJumpListener;
3838
import in.twizmwaz.cardinal.module.kit.listener.ShieldListener;
3939
import in.twizmwaz.cardinal.module.kit.type.KitArmor;
@@ -52,7 +52,6 @@
5252
import in.twizmwaz.cardinal.util.Numbers;
5353
import in.twizmwaz.cardinal.util.Strings;
5454
import in.twizmwaz.cardinal.util.document.DocumentItems;
55-
import lombok.Getter;
5655
import lombok.NonNull;
5756
import org.bukkit.GameMode;
5857
import org.bukkit.attribute.AttributeModifier;
@@ -67,32 +66,27 @@
6766
import java.util.UUID;
6867
import java.util.stream.Collectors;
6968

70-
@ModuleEntry(depends = {FilterModule.class})
69+
@ModuleEntry(depends = {IdModule.class, FilterModule.class})
7170
public class KitModule extends AbstractModule {
7271

73-
@Getter
74-
private final Map<Match, Map<String, Kit>> kits = Maps.newHashMap();
75-
7672
public KitModule() {
7773
Cardinal.registerEvents(new DoubleJumpListener());
7874
Cardinal.registerEvents(new ShieldListener());
7975
}
8076

8177
@Override
8278
public boolean loadMatch(Match match) {
83-
kits.put(match, Maps.newHashMap());
84-
Map<String, Kit> matchKits = kits.get(match);
8579
for (Element kits : match.getMap().getDocument().getRootElement().getChildren("kits")) {
8680
for (Element element : kits.getChildren("kit")) {
8781
Map.Entry<String, Kit> entry = parseKit(match, element);
88-
matchKits.put(entry.getKey(), entry.getValue());
82+
IdModule.get().add(match, entry.getKey(), entry.getValue());
8983
}
9084
}
9185
return true;
9286
}
9387

9488
public Kit getKit(@NonNull Match match, @NonNull String id) {
95-
return kits.get(match).get(id);
89+
return IdModule.get().get(match, id, Kit.class);
9690
}
9791

9892
private Map.Entry<String, Kit> parseKit(Match match, Element element) {
@@ -104,7 +98,7 @@ private Map.Entry<String, Kit> parseKit(Match match, Element element) {
10498
if (element.getAttributeValue("id") != null) {
10599
name = element.getAttributeValue("id");
106100
}
107-
for (Map.Entry<String, Kit> kitPair : this.kits.get(match).entrySet()) {
101+
for (Map.Entry<String, Kit> kitPair : IdModule.get().getMap(match, Kit.class).entrySet()) {
108102
if (kitPair.getKey().equalsIgnoreCase(name)) {
109103
return kitPair;
110104
}

src/main/java/in/twizmwaz/cardinal/module/kit/type/KitCluster.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import in.twizmwaz.cardinal.Cardinal;
3030
import in.twizmwaz.cardinal.match.Match;
3131
import in.twizmwaz.cardinal.module.filter.Filter;
32+
import in.twizmwaz.cardinal.module.id.IdModule;
3233
import in.twizmwaz.cardinal.module.kit.Kit;
33-
import in.twizmwaz.cardinal.module.kit.KitModule;
3434
import in.twizmwaz.cardinal.module.kit.KitRemovable;
3535
import lombok.Getter;
3636
import lombok.RequiredArgsConstructor;
@@ -92,7 +92,7 @@ public void remove(Player player) {
9292
private void evaluateParents() {
9393
if (parentsRaw != null) {
9494
parentsRaw.forEach(parent -> {
95-
Kit kit = Cardinal.getModule(KitModule.class).getKits().get(match).get(parent);
95+
Kit kit = IdModule.get().get(match, parent, Kit.class);
9696
if (kit != null) {
9797
parents.add(kit);
9898
}

0 commit comments

Comments
 (0)