forked from espidev/ProtectionStones
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockUtil.java
More file actions
204 lines (177 loc) · 8.17 KB
/
BlockUtil.java
File metadata and controls
204 lines (177 loc) · 8.17 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dev.espi.protectionstones.utils;
import dev.espi.protectionstones.PSProtectBlock;
import dev.espi.protectionstones.ProtectionStones;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.profile.PlayerProfile;
import org.bukkit.profile.PlayerTextures;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.UUID;
public class BlockUtil {
static final int MAX_USERNAME_LENGTH = 16;
public static HashMap<String, String> uuidToBase64Head = new HashMap<>();
public static ItemStack getProtectBlockItemFromType(String type) {
if (type.startsWith(Material.PLAYER_HEAD.toString())) {
return new ItemStack(Material.PLAYER_HEAD);
} else {
return new ItemStack(Material.getMaterial(type));
}
}
// used for preventing unnecessary calls to .getOwningPlayer() which could cause server freezes
private static boolean isOwnedSkullTypeConfigured() {
for (PSProtectBlock b : ProtectionStones.getInstance().getConfiguredBlocks()) {
if (b.type.startsWith("PLAYER_HEAD:")) {
return true;
}
}
return false;
}
public static String getProtectBlockType(ItemStack i) {
if (i.getType() == Material.PLAYER_HEAD || i.getType() == Material.LEGACY_SKULL_ITEM) {
SkullMeta sm = (SkullMeta) i.getItemMeta();
// PLAYER_HEAD
if (!sm.hasOwner() || !isOwnedSkullTypeConfigured()) {
return Material.PLAYER_HEAD.toString();
}
// PLAYER_HEAD:base64
PlayerProfile offlineProfile = sm.getOwnerProfile();
if (ProtectionStones.getBlockOptions("PLAYER_HEAD:" +offlineProfile.getUniqueId()) != null) {
return Material.PLAYER_HEAD + ":" +offlineProfile.getUniqueId();
}
// PLAYER_HEAD:name
return Material.PLAYER_HEAD + ":" + offlineProfile.getName(); // return name if it doesn't exist
}
return i.getType().toString();
}
public static String getProtectBlockType(Block block) {
if (block.getType() == Material.PLAYER_HEAD || block.getType() == Material.PLAYER_WALL_HEAD) {
Skull s = (Skull) block.getState();
if (s.hasOwner() && isOwnedSkullTypeConfigured()) {
PlayerProfile offlineProfile = s.getOwnerProfile();
if (ProtectionStones.getBlockOptions("PLAYER_HEAD:" + offlineProfile.getUniqueId()) != null) {
// PLAYER_HEAD:base64
return Material.PLAYER_HEAD + ":" + offlineProfile.getUniqueId();
} else {
// PLAYER_HEAD:name
return Material.PLAYER_HEAD + ":" + offlineProfile.getName(); // return name if doesn't exist
}
} else { // PLAYER_HEAD
return Material.PLAYER_HEAD.toString();
}
} else if (block.getType() == Material.CREEPER_WALL_HEAD) {
return Material.CREEPER_HEAD.toString();
} else if (block.getType() == Material.DRAGON_WALL_HEAD) {
return Material.DRAGON_HEAD.toString();
} else if (block.getType() == Material.ZOMBIE_WALL_HEAD) {
return Material.ZOMBIE_HEAD.toString();
} else if (block.getType() == Material.SKELETON_WALL_SKULL) {
return Material.SKELETON_SKULL.toString();
} else if (block.getType() == Material.WITHER_SKELETON_WALL_SKULL) {
return Material.WITHER_SKELETON_SKULL.toString();
} else {
return block.getType().toString();
}
}
public static void setHeadType(String psType, Block b) {
if (psType.split(":").length < 2) return;
String name = psType.split(":")[1];
if (name.length() > MAX_USERNAME_LENGTH) {
blockWithBase64(b, name);
} else {
OfflinePlayer op = Bukkit.getOfflinePlayer(psType.split(":")[1]);
Skull s = (Skull) b.getState();
s.setOwningPlayer(op);
s.update();
}
}
public static PlayerProfile getProfile(String uuid, String base64) {
String name = uuid.substring(0, 16);
PlayerProfile profile = Bukkit.getServer().createPlayerProfile(UUID.fromString(uuid), name);
PlayerTextures textures = profile.getTextures();
// decode base64 to URL
byte[] decodedBytes = Base64.getDecoder().decode(base64);
String decodedString = new String(decodedBytes);
// read decoded string as JSON object
// sample: {"textures":{"SKIN":{"url":"http://textures.minecraft.net/texture/..."}}}
String url = "";
try {
JSONParser parser = new JSONParser();
JSONObject object = (JSONObject) parser.parse(decodedString);
JSONObject jsonTextures = (JSONObject) object.get("textures");
JSONObject jsonSkin = (JSONObject) jsonTextures.get("SKIN");
url = (String) jsonSkin.get("url");
} catch (ParseException exception) {
throw new RuntimeException("Invalid JSON retrieved from base64 " + decodedString, exception);
}
URL urlObject;
try {
urlObject = new URL(url);
} catch (MalformedURLException exception) {
throw new RuntimeException("Invalid decoded URL from head data: " + url, exception);
}
textures.setSkin(urlObject);
profile.setTextures(textures);
return profile;
}
public static ItemStack setHeadType(String psType, ItemStack item) {
String name = psType.split(":")[1];
if (name.length() > MAX_USERNAME_LENGTH) { // base 64 head
String uuid = name;
// decode base64 to URL
String base64 = uuidToBase64Head.get(name);
PlayerProfile profile = getProfile(uuid, base64);
SkullMeta meta = (SkullMeta) item.getItemMeta();
meta.setOwnerProfile(profile);
item.setItemMeta(meta);
return item;
} else { // normal name head
SkullMeta sm = (SkullMeta) item.getItemMeta();
sm.setOwningPlayer(Bukkit.getOfflinePlayer(name));
item.setItemMeta(sm);
return item;
}
}
private static void blockWithBase64(Block block, String uuid) {
String base64 = uuidToBase64Head.get(uuid);
PlayerProfile profile = getProfile(uuid, base64);
Skull skull = (Skull) block.getState();
skull.setOwnerProfile(profile);
skull.update(false);
}
public static boolean isBase64PSHead(String type) {
return type.startsWith("PLAYER_HEAD") && type.split(":").length > 1 && type.split(":")[1].length() > MAX_USERNAME_LENGTH;
}
public static String getUUIDFromBase64PS(PSProtectBlock b) {
String base64 = b.type.split(":")[1];
// return UUID.nameUUIDFromBytes(base64.getBytes()).toString(); <- I should be using this
// the below is bad, because hashcode should really not be used... unfortunately, this is used in production so it will have to stay like this
// until I can find a way to convert items to the new uuid
// see github issue #126
return new UUID(base64.hashCode(), base64.hashCode()).toString();
}
}