-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
KeyImpl.java
146 lines (113 loc) · 4.45 KB
/
KeyImpl.java
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
package com.bgsoftware.superiorskyblock.core.key;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.world.BukkitEntities;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
import java.util.regex.Pattern;
public class KeyImpl implements Key {
private static final Pattern LEGACY_PATTERN = Pattern.compile("LEGACY_");
private static final Pattern KEY_SPLITTER_PATTERN = Pattern.compile("[:;]");
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();
private final String globalKey;
private final String subKey;
private final int cachedHash;
private boolean apiKey = false;
private KeyImpl(String globalKey, String subKey) {
this.globalKey = globalKey.toUpperCase(Locale.ENGLISH);
this.subKey = subKey.toUpperCase(Locale.ENGLISH);
this.cachedHash = hashCodeInternal();
}
public static Key of(EntityType entityType) {
return of(entityType.name());
}
public static Key of(Entity entity) {
return of(BukkitEntities.getLimitEntityType(entity), entity);
}
public static Key of(Block block) {
return of(block.getState());
}
public static Key of(BlockState blockState) {
if (blockState instanceof CreatureSpawner) {
CreatureSpawner creatureSpawner = (CreatureSpawner) blockState;
return of(Materials.SPAWNER.toBukkitType() + "", creatureSpawner.getSpawnedType() + "", blockState.getLocation());
}
//noinspection deprecation
Key rawBlockKey = of(blockState.getType(), blockState.getRawData());
return of(rawBlockKey.getGlobalKey(), rawBlockKey.getSubKey(), blockState.getLocation());
}
public static Key of(ItemStack itemStack) {
return of(Materials.SPAWNER.toBukkitType() == itemStack.getType() ? plugin.getProviders().getSpawnerKey(itemStack) :
of(itemStack.getType(), itemStack.getDurability()), itemStack);
}
public static Key of(Material material, short data) {
return of(of(material + "", data + ""), new ItemStack(material, 1, data));
}
public static Key of(String globalKey, String subKey) {
return new KeyImpl(LEGACY_PATTERN.matcher(globalKey).replaceAll(""), subKey);
}
public static Key of(String key) {
String[] sections = KEY_SPLITTER_PATTERN.split(key);
return new KeyImpl(sections[0], sections.length == 2 ? sections[1] : "");
}
public static Key of(Material material, short data, Location location) {
return of(material + "", data + "", location);
}
public static Key of(String globalKey, String subKey, Location location) {
return of(KeyImpl.of(globalKey, subKey), location);
}
public static Key of(Key key, Location location) {
return plugin.getBlockValues().convertKey(key, location);
}
public static Key of(Key key, ItemStack itemStack) {
return plugin.getBlockValues().convertKey(key, itemStack);
}
public static Key of(Key key, Entity entity) {
return plugin.getBlockValues().convertKey(key, entity);
}
@Override
public String getGlobalKey() {
return globalKey;
}
@Override
public String getSubKey() {
return subKey;
}
public Key markAPIKey() {
this.apiKey = true;
return this;
}
public boolean isAPIKey() {
return apiKey;
}
@Override
public int compareTo(@NotNull Key o) {
return toString().compareTo(o.toString());
}
@Override
@SuppressWarnings("all")
public boolean equals(Object obj) {
return obj instanceof Key && this.cachedHash == obj.hashCode() &&
this.globalKey.equals(((Key) obj).getGlobalKey()) && this.subKey.equals(((Key) obj).getSubKey());
}
@Override
public String toString() {
return subKey.isEmpty() ? globalKey : globalKey + ":" + subKey;
}
@Override
public int hashCode() {
return this.cachedHash;
}
private int hashCodeInternal() {
return toString().hashCode();
}
}