support glowstone (maybe)
This commit is contained in:
parent
9715c5394c
commit
867ba24c6c
@ -46,32 +46,46 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
public class Injector {
|
public class Injector {
|
||||||
private static final Map<UUID, LPPermissible> INJECTED_PERMISSIBLES = new ConcurrentHashMap<>();
|
private static final Map<UUID, LPPermissible> INJECTED_PERMISSIBLES = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private static Field HUMAN_ENTITY_FIELD;
|
private static Field humanEntityField;
|
||||||
private static Field PERMISSIBLEBASE_ATTACHMENTS;
|
private static Field permissibleAttachmentsField;
|
||||||
|
private static Throwable cachedThrowable = null;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
HUMAN_ENTITY_FIELD = Class.forName(getVersionedClassName("entity.CraftHumanEntity")).getDeclaredField("perm");
|
try {
|
||||||
HUMAN_ENTITY_FIELD.setAccessible(true);
|
// craftbukkit
|
||||||
|
humanEntityField = Class.forName(getVersionedClassName("entity.CraftHumanEntity")).getDeclaredField("perm");
|
||||||
|
humanEntityField.setAccessible(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// glowstone
|
||||||
|
humanEntityField = Class.forName("net.glowstone.entity.GlowHumanEntity").getDeclaredField("permissions");
|
||||||
|
humanEntityField.setAccessible(true);
|
||||||
|
}
|
||||||
|
|
||||||
PERMISSIBLEBASE_ATTACHMENTS = PermissibleBase.class.getDeclaredField("attachments");
|
permissibleAttachmentsField = PermissibleBase.class.getDeclaredField("attachments");
|
||||||
PERMISSIBLEBASE_ATTACHMENTS.setAccessible(true);
|
permissibleAttachmentsField.setAccessible(true);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Throwable t) {
|
||||||
e.printStackTrace();
|
cachedThrowable = t;
|
||||||
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean inject(Player player, LPPermissible lpPermissible) {
|
public static boolean inject(Player player, LPPermissible lpPermissible) {
|
||||||
|
if (cachedThrowable != null) {
|
||||||
|
cachedThrowable.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PermissibleBase existing = (PermissibleBase) HUMAN_ENTITY_FIELD.get(player);
|
PermissibleBase existing = (PermissibleBase) humanEntityField.get(player);
|
||||||
if (existing instanceof LPPermissible) {
|
if (existing instanceof LPPermissible) {
|
||||||
// uh oh
|
// uh oh
|
||||||
throw new IllegalStateException("LPPermissible already injected into player " + player.toString());
|
throw new IllegalStateException("LPPermissible already injected into player " + player.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move attachments over from the old permissible.
|
// Move attachments over from the old permissible.
|
||||||
List<PermissionAttachment> attachments = (List<PermissionAttachment>) PERMISSIBLEBASE_ATTACHMENTS.get(existing);
|
List<PermissionAttachment> attachments = (List<PermissionAttachment>) permissibleAttachmentsField.get(existing);
|
||||||
lpPermissible.addAttachments(attachments);
|
lpPermissible.addAttachments(attachments);
|
||||||
attachments.clear();
|
attachments.clear();
|
||||||
existing.clearPermissions();
|
existing.clearPermissions();
|
||||||
@ -82,7 +96,7 @@ public class Injector {
|
|||||||
|
|
||||||
lpPermissible.updateSubscriptionsAsync();
|
lpPermissible.updateSubscriptionsAsync();
|
||||||
|
|
||||||
HUMAN_ENTITY_FIELD.set(player, lpPermissible);
|
humanEntityField.set(player, lpPermissible);
|
||||||
INJECTED_PERMISSIBLES.put(player.getUniqueId(), lpPermissible);
|
INJECTED_PERMISSIBLES.put(player.getUniqueId(), lpPermissible);
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -92,8 +106,13 @@ public class Injector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean unInject(Player player, boolean dummy, boolean unsubscribe) {
|
public static boolean unInject(Player player, boolean dummy, boolean unsubscribe) {
|
||||||
|
if (cachedThrowable != null) {
|
||||||
|
cachedThrowable.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PermissibleBase permissible = (PermissibleBase) HUMAN_ENTITY_FIELD.get(player);
|
PermissibleBase permissible = (PermissibleBase) humanEntityField.get(player);
|
||||||
if (permissible instanceof LPPermissible) {
|
if (permissible instanceof LPPermissible) {
|
||||||
|
|
||||||
permissible.clearPermissions();
|
permissible.clearPermissions();
|
||||||
@ -105,7 +124,7 @@ public class Injector {
|
|||||||
((LPPermissible) permissible).getActive().set(false);
|
((LPPermissible) permissible).getActive().set(false);
|
||||||
|
|
||||||
if (dummy) {
|
if (dummy) {
|
||||||
HUMAN_ENTITY_FIELD.set(player, new DummyPermissibleBase());
|
humanEntityField.set(player, new DummyPermissibleBase());
|
||||||
} else {
|
} else {
|
||||||
LPPermissible lpp = ((LPPermissible) permissible);
|
LPPermissible lpp = ((LPPermissible) permissible);
|
||||||
List<PermissionAttachment> attachments = lpp.getAttachments();
|
List<PermissionAttachment> attachments = lpp.getAttachments();
|
||||||
@ -115,11 +134,11 @@ public class Injector {
|
|||||||
newPb = new PermissibleBase(player);
|
newPb = new PermissibleBase(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<PermissionAttachment> newAttachments = (List<PermissionAttachment>) PERMISSIBLEBASE_ATTACHMENTS.get(newPb);
|
List<PermissionAttachment> newAttachments = (List<PermissionAttachment>) permissibleAttachmentsField.get(newPb);
|
||||||
newAttachments.addAll(attachments);
|
newAttachments.addAll(attachments);
|
||||||
attachments.clear();
|
attachments.clear();
|
||||||
|
|
||||||
HUMAN_ENTITY_FIELD.set(player, newPb);
|
humanEntityField.set(player, newPb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
INJECTED_PERMISSIBLES.remove(player.getUniqueId());
|
INJECTED_PERMISSIBLES.remove(player.getUniqueId());
|
||||||
@ -134,10 +153,10 @@ public class Injector {
|
|||||||
return INJECTED_PERMISSIBLES.get(uuid);
|
return INJECTED_PERMISSIBLES.get(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getVersionedClassName(String className) {
|
private static String getVersionedClassName(String className) throws ClassNotFoundException {
|
||||||
Class server = Bukkit.getServer().getClass();
|
Class server = Bukkit.getServer().getClass();
|
||||||
if (!server.getSimpleName().equals("CraftServer")) {
|
if (!server.getSimpleName().equals("CraftServer")) {
|
||||||
throw new RuntimeException("Couldn't inject into server " + server);
|
throw new ClassNotFoundException("Couldn't inject into server " + server);
|
||||||
}
|
}
|
||||||
|
|
||||||
String version;
|
String version;
|
||||||
|
Loading…
Reference in New Issue
Block a user