|
1 | 1 | package de.dosmike.sponge.vshop.shops;
|
2 | 2 |
|
3 | 3 | import com.google.common.reflect.TypeToken;
|
4 |
| -import de.dosmike.sponge.vshop.VillagerShops; |
5 | 4 | import org.apache.commons.lang3.StringUtils;
|
6 | 5 | import org.spongepowered.api.CatalogType;
|
7 | 6 | import org.spongepowered.api.Sponge;
|
8 | 7 | import org.spongepowered.api.data.key.Key;
|
9 |
| -import org.spongepowered.api.data.key.Keys; |
10 |
| -import org.spongepowered.api.data.type.Career; |
11 |
| -import org.spongepowered.api.data.type.Profession; |
12 | 8 | import org.spongepowered.api.data.value.BaseValue;
|
13 | 9 | import org.spongepowered.api.entity.Entity;
|
14 | 10 | import org.spongepowered.api.entity.EntityType;
|
15 |
| -import org.spongepowered.api.entity.EntityTypes; |
16 |
| -import org.spongepowered.api.entity.living.player.User; |
17 | 11 |
|
18 | 12 | import java.util.*;
|
19 | 13 | import java.util.regex.Pattern;
|
20 | 14 | import java.util.stream.Collectors;
|
21 | 15 |
|
22 | 16 | public class FieldResolver {
|
23 | 17 |
|
24 |
| - public static final FieldResolver HORSE_VARIANT = new FieldResolver( |
25 |
| - EntityTypes.HORSE, Arrays.asList(Keys.HORSE_STYLE, Keys.HORSE_COLOR) |
26 |
| - ); |
27 |
| - public static final FieldResolver OCELOT_VARIANT = new FieldResolver( |
28 |
| - EntityTypes.OCELOT, Collections.singletonList(Keys.OCELOT_TYPE) |
29 |
| - ); |
30 |
| - public static final FieldResolver LLAMA_VARIANT = new FieldResolver( |
31 |
| - EntityTypes.LLAMA, Collections.singletonList(Keys.LLAMA_VARIANT) |
32 |
| - ); |
33 |
| - public static final FieldResolver RABBIT_VARIANT = new FieldResolver( |
34 |
| - EntityTypes.RABBIT, Collections.singletonList(Keys.RABBIT_TYPE) |
35 |
| - ); |
36 |
| - public static final FieldResolver PARROT_VARIANT = new FieldResolver( |
37 |
| - EntityTypes.PARROT, Collections.singletonList(Keys.PARROT_VARIANT) |
38 |
| - ); |
39 |
| - public static final FieldResolver PLAYER_SKIN = new FieldResolver( |
40 |
| - EntityTypes.HUMAN, Collections.singletonList(Keys.SKIN_UNIQUE_ID) |
41 |
| - ) { |
42 |
| - final Pattern patUUID = Pattern.compile("(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})|(?:[0-9a-f]{32})", Pattern.CASE_INSENSITIVE); |
43 |
| - |
44 |
| - @Override |
45 |
| - public KeyAttacher validate(String magicVariant) { |
46 |
| - UUID uuid = null; |
47 |
| - if (patUUID.matcher(magicVariant).matches()) { |
48 |
| - if (magicVariant.indexOf('-') < 0) { |
49 |
| - magicVariant = magicVariant.substring(0, 8) + "-" + magicVariant.substring(8, 12) + "-" + magicVariant.substring(12, 16) + "-" + magicVariant.substring(16, 20) + "-" + magicVariant.substring(20); |
50 |
| - } |
51 |
| - uuid = UUID.fromString(magicVariant); |
52 |
| - } else { |
53 |
| - try { |
54 |
| - User user = VillagerShops.getUserStorage() |
55 |
| - .get(magicVariant).orElse(null); |
56 |
| - if (user != null) |
57 |
| - uuid = user.getUniqueId(); |
58 |
| - } catch (Exception e) { |
59 |
| - /* ignore invalid values */ |
60 |
| - } |
61 |
| - } |
62 |
| - KeyAttacher attacher = new KeyAttacher(); |
63 |
| - attacher.add(Keys.SKIN_UNIQUE_ID, uuid); |
64 |
| - return attacher; |
65 |
| - } |
66 |
| - |
67 |
| - /** can't auto this on server startup */ |
68 |
| - @Override |
69 |
| - public Set<String> registerAutoComplete() { |
70 |
| - return new HashSet<>(); |
71 |
| - } |
72 |
| - }; |
73 | 18 | protected static final Map<EntityType, Set<String>> autoTabCompleteMapping = new HashMap<>();
|
74 |
| - private static final String VARIANT_CONCATINATOR = "+"; |
75 |
| - private static final Pattern VARIANT_SPLITERATOR = Pattern.compile("\\+"); |
76 |
| - public static final FieldResolver VILLAGER_VARIANT = new FieldResolver( |
77 |
| - EntityTypes.VILLAGER, Collections.singletonList(Keys.CAREER) |
78 |
| - ) { |
79 |
| - @Override |
80 |
| - public KeyAttacher validate(String magicVariant) { |
81 |
| - String[] raw = VARIANT_SPLITERATOR.split(magicVariant); |
82 |
| - Career career = null; |
83 |
| - Profession profession = null; |
84 |
| - for (String s : raw) { |
85 |
| - Optional<Career> careerOptional = Sponge.getRegistry().getType(Career.class, s); |
86 |
| - if (careerOptional.isPresent()) career = careerOptional.get(); |
87 |
| - Optional<Profession> professionOptional = Sponge.getRegistry().getType(Profession.class, s); |
88 |
| - if (professionOptional.isPresent()) profession = professionOptional.get(); |
89 |
| - } |
90 |
| - if (career == null && profession != null) |
91 |
| - career = profession.getCareers().stream().findAny().orElse(null); |
| 19 | + static final String VARIANT_CONCATINATOR = "+"; |
| 20 | + static final Pattern VARIANT_SPLITERATOR = Pattern.compile("\\+"); |
92 | 21 |
|
93 |
| - KeyAttacher attacher = new KeyAttacher(); |
94 |
| - attacher.add(Keys.CAREER, career); |
95 |
| - return attacher; |
96 |
| - } |
97 |
| - }; |
98 | 22 | private static final Map<EntityType, FieldResolver> autoResolverMapping = new HashMap<>();
|
99 | 23 | protected final ArrayList<Key<? extends BaseValue<?>>> keys;
|
100 | 24 |
|
101 |
| - private FieldResolver(EntityType type, List<Key<? extends BaseValue<?>>> keyList) { |
| 25 | + FieldResolver(EntityType type, List<Key<? extends BaseValue<?>>> keyList) { |
102 | 26 | this.keys = new ArrayList<>(keyList);
|
103 | 27 | autoResolverMapping.put(type, this);
|
104 | 28 | autoTabCompleteMapping.put(type, registerAutoComplete());
|
@@ -230,7 +154,7 @@ public String toString() {
|
230 | 154 | public static class KeyAttacher {
|
231 | 155 | final List<KeyValuePair<?, ?>> values = new LinkedList<>();
|
232 | 156 |
|
233 |
| - private KeyAttacher() { |
| 157 | + KeyAttacher() { |
234 | 158 | }
|
235 | 159 |
|
236 | 160 | <V, K extends BaseValue<V>> void add(Key<K> key, V value) {
|
|
0 commit comments