This repository has been archived by the owner on Aug 1, 2021. It is now read-only.
/
SecurityHelper.java
285 lines (222 loc) · 7.88 KB
/
SecurityHelper.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package cofh.lib.util.helpers;
import cofh.api.tileentity.ISecurable;
import cofh.api.tileentity.ISecurable.AccessMode;
import com.google.common.base.Strings;
import com.mojang.authlib.GameProfile;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;
import cpw.mods.fml.relauncher.ReflectionHelper;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.network.EnumConnectionState;
import net.minecraft.network.INetHandler;
import net.minecraft.network.Packet;
import net.minecraft.network.PacketBuffer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PreYggdrasilConverter;
public class SecurityHelper {
public static final GameProfile UNKNOWN_GAME_PROFILE = new GameProfile(UUID.fromString("1ef1a6f0-87bc-4e78-0a0b-c6824eb787ea"), "[None]");
private static boolean setup = false;
public static void setup() {
if (setup) {
return;
}
EnumConnectionState.PLAY.func_150755_b().put(-26, Login.S__PacketSendUUID.class);
Map<Class<?>, EnumConnectionState> data;
data = ReflectionHelper.getPrivateValue(EnumConnectionState.class, null, "field_150761_f");
data.put(Login.S__PacketSendUUID.class, EnumConnectionState.PLAY);
FMLCommonHandler.instance().bus().register(new Login.S__PacketSendUUID());
setup = true;
}
static {
setup();
}
private SecurityHelper() {
}
public static boolean isDefaultUUID(UUID uuid) {
return uuid == null || (uuid.version() == 4 && uuid.variant() == 0);
}
public static UUID getID(EntityPlayer player) {
if (MinecraftServer.getServer() != null && MinecraftServer.getServer().isServerRunning()) {
return player.getGameProfile().getId();
}
return getClientId(player);
}
private static UUID cachedId;
private static UUID getClientId(EntityPlayer player) {
if (player != Minecraft.getMinecraft().thePlayer) {
return player.getGameProfile().getId();
}
if (cachedId == null) {
cachedId = Minecraft.getMinecraft().thePlayer.getGameProfile().getId();
}
return cachedId;
}
/* NBT TAG HELPER */
public static NBTTagCompound setItemStackTagSecure(NBTTagCompound tag, ISecurable tile) {
if (tile == null) {
return null;
}
if (tag == null) {
tag = new NBTTagCompound();
}
tag.setBoolean("Secure", true);
tag.setByte("Access", (byte) tile.getAccess().ordinal());
tag.setString("OwnerUUID", tile.getOwner().getId().toString());
tag.setString("Owner", tile.getOwner().getName());
return tag;
}
/**
* Adds Security information to ItemStacks.
*/
public static void addOwnerInformation(ItemStack stack, List<String> list) {
if (SecurityHelper.isSecure(stack)) {
boolean hasUUID = stack.stackTagCompound.hasKey("OwnerUUID");
if (!stack.stackTagCompound.hasKey("Owner") && !hasUUID) {
list.add(StringHelper.localize("info.cofh.owner") + ": " + StringHelper.localize("info.cofh.none"));
} else {
if (hasUUID && stack.stackTagCompound.hasKey("Owner")) {
list.add(StringHelper.localize("info.cofh.owner") + ": " + stack.stackTagCompound.getString("Owner") + " \u0378");
} else {
list.add(StringHelper.localize("info.cofh.owner") + ": " + StringHelper.localize("info.cofh.anotherplayer"));
}
}
}
}
public static void addAccessInformation(ItemStack stack, List<String> list) {
if (SecurityHelper.isSecure(stack)) {
String accessString = "";
switch (stack.stackTagCompound.getByte("Access")) {
case 0:
accessString = StringHelper.localize("info.cofh.accessPublic");
break;
case 1:
accessString = StringHelper.localize("info.cofh.accessRestricted");
break;
case 2:
accessString = StringHelper.localize("info.cofh.accessPrivate");
break;
}
list.add(StringHelper.localize("info.cofh.access") + ": " + accessString);
}
}
/* ITEM HELPERS */
public static boolean isSecure(ItemStack stack) {
return stack.stackTagCompound == null ? false : stack.stackTagCompound.hasKey("Secure");
}
public static ItemStack setSecure(ItemStack stack) {
if (isSecure(stack)) {
return stack;
}
if (stack.stackTagCompound == null) {
stack.setTagCompound(new NBTTagCompound());
}
stack.stackTagCompound.setBoolean("Secure", true);
stack.stackTagCompound.setByte("Access", (byte) 0);
return stack;
}
public static ItemStack removeSecure(ItemStack stack) {
if (!isSecure(stack)) {
return stack;
}
stack.stackTagCompound.removeTag("Secure");
stack.stackTagCompound.removeTag("Access");
stack.stackTagCompound.removeTag("OwnerUUID");
stack.stackTagCompound.removeTag("Owner");
if (stack.stackTagCompound.hasNoTags()) {
stack.stackTagCompound = null;
}
return stack;
}
public static boolean setAccess(ItemStack stack, AccessMode access) {
if (!isSecure(stack)) {
return false;
}
stack.stackTagCompound.setByte("Access", (byte) access.ordinal());
return true;
}
public static AccessMode getAccess(ItemStack stack) {
return stack.stackTagCompound == null ? AccessMode.PUBLIC : AccessMode.values()[stack.stackTagCompound.getByte("Access")];
}
public static boolean setOwner(ItemStack stack, GameProfile name) {
if (!isSecure(stack)) {
return false;
}
stack.setTagInfo("OwnerUUID", new NBTTagString(name.getId().toString()));
stack.setTagInfo("Owner", new NBTTagString(name.getName()));
return true;
}
public static GameProfile getOwner(ItemStack stack) {
if (stack.stackTagCompound != null) {
NBTTagCompound nbt = stack.stackTagCompound;
String uuid = nbt.getString("OwnerUUID");
String name = nbt.getString("Owner");
if (!Strings.isNullOrEmpty(uuid)) {
return new GameProfile(UUID.fromString(uuid), name);
} else if (!Strings.isNullOrEmpty(name)) {
return new GameProfile(UUID.fromString(PreYggdrasilConverter.func_152719_a(name)), name);
}
}
return UNKNOWN_GAME_PROFILE;
}
public static GameProfile getProfile(UUID uuid, String name) {
GameProfile owner = MinecraftServer.getServer().func_152358_ax().func_152652_a(uuid);
if (owner == null) {
GameProfile temp = new GameProfile(uuid, name);
owner = MinecraftServer.getServer().func_147130_as().fillProfileProperties(temp, true);
if (owner != temp) {
MinecraftServer.getServer().func_152358_ax().func_152649_a(owner);
}
}
return owner;
}
public static String getOwnerName(ItemStack stack) {
NBTTagCompound nbt = stack.stackTagCompound;
boolean hasUUID;
if (nbt == null || (!(hasUUID = nbt.hasKey("OwnerUUID")) && !nbt.hasKey("Owner"))) {
return "[None]";
}
return hasUUID ? stack.stackTagCompound.getString("Owner") : StringHelper.localize("info.cofh.anotherplayer");
}
// this class is to avoid an illegal access error from FML's event handler
private static class Login {
public static class S__PacketSendUUID extends Packet {
@SubscribeEvent
public void login(PlayerLoggedInEvent evt) {
((EntityPlayerMP) evt.player).playerNetServerHandler.sendPacket(new S__PacketSendUUID(evt.player));
}
private UUID id;
public S__PacketSendUUID() {
}
public S__PacketSendUUID(EntityPlayer player) {
id = player.getGameProfile().getId();
}
@Override
public void readPacketData(PacketBuffer buffer) throws IOException {
id = new UUID(buffer.readLong(), buffer.readLong());
}
@Override
public void writePacketData(PacketBuffer buffer) throws IOException {
buffer.writeLong(id.getMostSignificantBits());
buffer.writeLong(id.getLeastSignificantBits());
}
@Override
public boolean hasPriority() {
return true;
}
@Override
public void processPacket(INetHandler p_148833_1_) {
cachedId = id;
}
}
}
}