Merge pull request #32 from Bkm016/patch/java11

Update for JDK 11
This commit is contained in:
坏黑 2020-02-03 14:00:48 +08:00 committed by GitHub
commit 0df4e8dfd1
23 changed files with 572 additions and 80 deletions

View File

@ -6,7 +6,7 @@ plugins {
}
group = 'me.skymc'
version = '5.15'
version = '5.16'
sourceCompatibility = 1.8
targetCompatibility = 1.8

View File

@ -7,6 +7,7 @@ import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.client.packet.impl.PacketEmpty;
import io.izzel.taboolib.module.inject.TListener;
import io.izzel.taboolib.util.Ref;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -68,7 +69,7 @@ public class PacketSerializer implements Listener {
Arrays.stream(packet.getClass().getDeclaredFields()).filter(field -> field.isAnnotationPresent(PacketValue.class)).forEach(field -> {
field.setAccessible(true);
try {
Object obj = field.get(packet);
Object obj = Ref.getField(packet, field);
if (obj instanceof Number) {
json.addProperty(field.getName(), (Number) obj);
} else if (obj instanceof Boolean) {

View File

@ -62,10 +62,10 @@ public class ListenerCommand implements Listener {
TabooLibAPI.debug(true);
TLogger.getGlobalLogger().info("&aEnabled.");
}
} else if ("libupdate".equalsIgnoreCase(e.getCommand())) {
} else if (e.getCommand().equalsIgnoreCase("libUpdate")) {
e.setCancelled(true);
e.getSender().sendMessage("§8[§fTabooLib§8] §cWARNING §7| §4Update TabooLib will force to restart your server. Please confirm this action by type §c/libupdateConfirm");
} else if ("libupdateConfirm".equalsIgnoreCase(e.getCommand()) || "libupdate confirm".equalsIgnoreCase(e.getCommand())) {
e.getSender().sendMessage("§8[§fTabooLib§8] §cWARNING §7| §4Update TabooLib will force to restart your server. Please confirm this action by type §c/libupdateconfirm");
} else if (e.getCommand().equalsIgnoreCase("libUpdateConfirm") || e.getCommand().equalsIgnoreCase("libUpdate confirm")) {
e.getSender().sendMessage("§8[§fTabooLib§8] §7Downloading TabooLib file...");
Files.downloadFile("https://skymc.oss-cn-shanghai.aliyuncs.com/plugins/TabooLib.jar", new File("libs/TabooLib.jar"));
e.getSender().sendMessage("§8[§fTabooLib§8] §2Download completed, the server will restart in 3 secs");

View File

@ -4,6 +4,7 @@ import io.izzel.taboolib.module.ai.PathfinderExecutor;
import io.izzel.taboolib.module.ai.SimpleAi;
import io.izzel.taboolib.module.ai.SimpleAiSelector;
import io.izzel.taboolib.module.lite.SimpleReflection;
import io.izzel.taboolib.util.Ref;
import net.minecraft.server.v1_8_R3.*;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
@ -29,7 +30,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
try {
SimpleReflection.saveField(PathfinderGoalSelector.class);
SimpleReflection.saveField(ControllerJump.class);
pathfinderGoalSelectorSet =SimpleReflection.getField(PathfinderGoalSelector.class, "b");
pathfinderGoalSelectorSet = SimpleReflection.getField(PathfinderGoalSelector.class, "b");
controllerJumpCurrent = SimpleReflection.getField(ControllerJump.class, "a");
} catch (Exception e) {
e.printStackTrace();
@ -85,7 +86,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override
public Object getPathEntity(LivingEntity entity) {
try {
return pathEntity.get(getNavigation(entity));
return Ref.getField(getNavigation(entity), pathEntity);
} catch (Exception e) {
e.printStackTrace();
}
@ -95,7 +96,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override
public void setPathEntity(LivingEntity entity, Object pathEntity) {
try {
this.pathEntity.set(getNavigation(entity), pathEntity);
Ref.putField(getNavigation(entity), this.pathEntity, pathEntity);
} catch (Exception e) {
e.printStackTrace();
}
@ -114,7 +115,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override
public void clearGoalAi(LivingEntity entity) {
try {
((Collection) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).goalSelector)).clear();
((Collection) Ref.getField(((EntityInsentient) getEntityInsentient(entity)).goalSelector, pathfinderGoalSelectorSet)).clear();
} catch (Exception e) {
e.printStackTrace();
}
@ -123,7 +124,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override
public void clearTargetAi(LivingEntity entity) {
try {
((Collection) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).targetSelector)).clear();
((Collection) Ref.getField(((EntityInsentient) getEntityInsentient(entity)).targetSelector, pathfinderGoalSelectorSet)).clear();
} catch (Exception e) {
e.printStackTrace();
}
@ -132,7 +133,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override
public Iterable getGoalAi(LivingEntity entity) {
try {
return ((Collection) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).goalSelector));
return ((Collection) Ref.getField(((EntityInsentient) getEntityInsentient(entity)).goalSelector, pathfinderGoalSelectorSet));
} catch (Throwable t) {
t.printStackTrace();
}
@ -142,7 +143,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override
public Iterable getTargetAi(LivingEntity entity) {
try {
return ((Collection) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).targetSelector));
return ((Collection) Ref.getField(((EntityInsentient) getEntityInsentient(entity)).targetSelector, pathfinderGoalSelectorSet));
} catch (Throwable t) {
t.printStackTrace();
}
@ -152,7 +153,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override
public void setGoalAi(LivingEntity entity, Iterable ai) {
try {
pathfinderGoalSelectorSet.set(((EntityInsentient) getEntityInsentient(entity)).goalSelector, ai);
Ref.putField(((EntityInsentient) getEntityInsentient(entity)).goalSelector, this.pathfinderGoalSelectorSet, ai);
} catch (Throwable t) {
t.printStackTrace();
}
@ -161,7 +162,8 @@ public class InternalPathfinderExecutor extends PathfinderExecutor {
@Override
public void setTargetAi(LivingEntity entity, Iterable ai) {
try {
pathfinderGoalSelectorSet.set(((EntityInsentient) getEntityInsentient(entity)).targetSelector, ai);
Ref.putField(((EntityInsentient) getEntityInsentient(entity)).targetSelector,
this.pathfinderGoalSelectorSet, ai);
} catch (Throwable t) {
t.printStackTrace();
}

View File

@ -7,6 +7,7 @@ import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.Version;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.util.ArrayUtil;
import io.izzel.taboolib.util.Ref;
import io.izzel.taboolib.util.Strings;
import org.bukkit.Bukkit;
import org.bukkit.command.*;
@ -82,8 +83,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
fields.sort(Comparator.comparingDouble(commandField -> commandField.getField().getAnnotation(SubCommand.class).priority()));
fields.forEach(commandField -> {
try {
commandField.getField().setAccessible(true);
BaseSubCommand subCommand = (BaseSubCommand) commandField.getField().get(commandField.getParent().newInstance());
BaseSubCommand subCommand = Ref.getField(commandField.getParent().newInstance(), commandField.getField(), BaseSubCommand.class);
subCommand.label(commandField.getField().getName()).annotation(commandField.getField().getAnnotation(SubCommand.class));
baseMainCommand.registerSubCommand(subCommand);
} catch (Throwable ignored) {
@ -249,8 +249,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
private void disguisedPlugin(Class<?> targetClass, Plugin plugin) {
try {
Field pluginField = targetClass.getClassLoader().getClass().getDeclaredField("plugin");
pluginField.setAccessible(true);
pluginField.set(targetClass.newInstance(), plugin);
Ref.putField(targetClass.newInstance(), pluginField, plugin);
} catch (Exception ignored) {
}
}

View File

@ -20,10 +20,9 @@ public class LocalLoader implements TabooLibLoader.Loader {
if (annotation == null) {
continue;
}
Ref.forcedAccess(field);
for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) {
try {
field.set(instance, Local.get(plugin.getName()).get(annotation.value()));
Ref.putField(instance, field, Local.get(plugin.getName()).get(annotation.value()));
} catch (Throwable t) {
t.printStackTrace();
}

View File

@ -1,10 +1,14 @@
package io.izzel.taboolib.module.dependency;
import io.izzel.taboolib.common.plugin.InternalPlugin;
import io.izzel.taboolib.util.Ref;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import java.io.File;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
@ -15,10 +19,13 @@ public class TDependencyLoader {
public static synchronized void addToPath(Plugin plugin, URL url) {
try {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(plugin instanceof InternalPlugin ? Bukkit.class.getClassLoader() : plugin.getClass().getClassLoader(), url);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
ClassLoader loader = plugin instanceof InternalPlugin ? Bukkit.class.getClassLoader() : plugin.getClass().getClassLoader();
Field ucpField = loader.getClass().getDeclaredField("ucp");
long ucpOffset = Ref.getUnsafe().objectFieldOffset(ucpField);
Object ucp = Ref.getUnsafe().getObject(loader, ucpOffset);
MethodHandle methodHandle = Ref.lookup().findVirtual(ucp.getClass(), "addURL", MethodType.methodType(void.class, java.net.URL.class));
methodHandle.invoke(ucp, url);
} catch (Throwable e) {
e.printStackTrace();
}
}

View File

@ -32,8 +32,7 @@ public abstract class EventNormal<T extends EventNormal> extends Event {
public T async(boolean value) {
try {
Field asyncField = Reflection.getField(Event.class, true, "async");
Ref.forcedAccess(asyncField);
asyncField.setBoolean(this, value);
Ref.putField(this, asyncField, value);
} catch (Throwable t) {
t.printStackTrace();
}

View File

@ -2,6 +2,7 @@ package io.izzel.taboolib.module.inject;
import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.module.locale.logger.TLogger;
import io.izzel.taboolib.util.Ref;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@ -34,7 +35,7 @@ public class PlayerContainerLoader implements Listener, TabooLibLoader.Loader {
field.setAccessible(true);
for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) {
try {
pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(field.get(instance), annotation.uniqueId()));
pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(Ref.getField(instance, field), annotation.uniqueId()));
} catch (Throwable t) {
t.printStackTrace();
}

View File

@ -27,10 +27,9 @@ public class THookLoader implements TabooLibLoader.Loader {
if (Plugin.class.isAssignableFrom(declaredField.getType()) && declaredField.isAnnotationPresent(THook.class)) {
THook hook = declaredField.getAnnotation(THook.class);
if (Strings.nonEmpty(hook.plugin())) {
Ref.forcedAccess(declaredField);
for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) {
try {
declaredField.set(instance, Bukkit.getPluginManager().getPlugin(hook.plugin()));
Ref.putField(instance, declaredField, Bukkit.getPluginManager().getPlugin(hook.plugin()));
} catch (Throwable t) {
t.printStackTrace();
}

View File

@ -20,10 +20,9 @@ public class TInjectAsm implements TabooLibLoader.Loader {
if (annotation == null || annotation.asm().isEmpty()) {
continue;
}
Ref.forcedAccess(declaredField);
for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) {
try {
declaredField.set(instance, SimpleVersionControl.createNMS(annotation.asm()).useCache().translate(plugin).newInstance());
Ref.putField(instance, declaredField, SimpleVersionControl.createNMS(annotation.asm()).useCache().translate(plugin).newInstance());
} catch (Throwable t) {
t.printStackTrace();
}

View File

@ -58,10 +58,9 @@ public class TInjectCreator implements TabooLibLoader.Loader {
if (instance.isEmpty()) {
continue;
}
Ref.forcedAccess(declaredField);
try {
InstanceData instanceData = new InstanceData(declaredField.getType().newInstance(), annotation);
declaredField.set(instance, instanceData.getInstance());
Ref.putField(instance, declaredField, instanceData.getInstance());
instanceMap.put(new ClassData(loadClass, declaredField.getType()), instanceData);
} catch (Throwable t) {
TLogger.getGlobalLogger().error(declaredField.getName() + " instantiation failed: " + t.getMessage());

View File

@ -34,7 +34,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
// Instance Inject
injectTypes.put(Plugin.class, (plugin, field, args, pluginClass, instance) -> {
try {
field.set(instance, plugin);
Ref.putField(instance, field, plugin);
} catch (Exception e) {
e.printStackTrace();
}
@ -42,7 +42,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
// TLogger Inject
injectTypes.put(TLogger.class, (plugin, field, args, pluginClass, instance) -> {
try {
field.set(instance, args.value().length == 0 ? TLogger.getUnformatted(plugin) : TLogger.getUnformatted(args.value()[0]));
Ref.putField(instance, field, args.value().length == 0 ? TLogger.getUnformatted(plugin) : TLogger.getUnformatted(args.value()[0]));
} catch (Exception e) {
e.printStackTrace();
}
@ -50,7 +50,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
// TPacketListener Inject
injectTypes.put(TPacketListener.class, (plugin, field, args, pluginClass, instance) -> {
try {
TPacketHandler.addListener(plugin, ((TPacketListener) field.get(instance)));
TPacketHandler.addListener(plugin, ((TPacketListener) Ref.getField(instance, field)));
} catch (Exception e) {
e.printStackTrace();
}
@ -59,7 +59,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
injectTypes.put(TConfig.class, (plugin, field, args, pluginClass, instance) -> {
try {
TConfig config = TConfig.create(plugin, args.value().length == 0 ? "config.yml" : args.value()[0]);
field.set(instance, config);
Ref.putField(instance, field, config);
if (Strings.nonEmpty(args.locale())) {
config.listener(() -> {
List<String> localePriority = Lists.newArrayList();
@ -98,7 +98,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
// SimpleCommandBuilder Inject
injectTypes.put(CommandBuilder.class, (plugin, field, args, pluginClass, instance) -> {
try {
CommandBuilder builder = (CommandBuilder) field.get(instance);
CommandBuilder builder = Ref.getField(instance, field, CommandBuilder.class);
if (!builder.isBuild()) {
if (builder.isSimpleMode()) {
builder.command(field.getName());
@ -115,7 +115,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
// CooldownPack Inject
injectTypes.put(Cooldown.class, (plugin, field, args, pluginClass, instance) -> {
try {
Cooldowns.register((Cooldown) field.get(instance), plugin);
Cooldowns.register((Cooldown) Ref.getField(instance, field), plugin);
} catch (Throwable t) {
t.printStackTrace();
}
@ -124,7 +124,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
injectTypes.put(Boolean.TYPE, (plugin, field, args, pluginClass, instance) -> {
try {
if (args.value().length > 0) {
field.set(instance, Bukkit.getPluginManager().getPlugin(args.value()[0]) != null);
Ref.putField(instance, field, Bukkit.getPluginManager().getPlugin(args.value()[0]) != null);
}
} catch (Throwable t) {
t.printStackTrace();
@ -134,7 +134,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
injectTypes.put(JavaPlugin.class, (plugin, field, args, pluginClass, instance) -> {
try {
if (args.value().length > 0) {
field.set(instance, Bukkit.getPluginManager().getPlugin(args.value()[0]));
Ref.putField(instance, field, Bukkit.getPluginManager().getPlugin(args.value()[0]));
}
} catch (Throwable t) {
t.printStackTrace();

View File

@ -65,7 +65,7 @@ public class TScheduleLoader implements TabooLibLoader.Loader {
method.invoke(instance);
} catch (Throwable t) {
try {
method.invoke(Ref.UNSAFE.allocateInstance(pluginClass));
method.invoke(Ref.getUnsafe().allocateInstance(pluginClass));
} catch (Throwable t2) {
t.printStackTrace();
t2.printStackTrace();

View File

@ -2,7 +2,6 @@ package io.izzel.taboolib.module.lite;
import com.google.common.collect.Maps;
import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.module.locale.logger.TLogger;
import io.izzel.taboolib.util.Ref;
import java.lang.reflect.Field;
@ -56,7 +55,6 @@ public class SimpleReflection {
public static void saveField(Class<?> nmsClass, String fieldName) {
try {
Field declaredField = nmsClass.getDeclaredField(fieldName);
Ref.forcedAccess(declaredField);
fieldCached.computeIfAbsent(nmsClass.getName(), name -> Maps.newHashMap()).put(fieldName, declaredField);
} catch (Exception e) {
e.printStackTrace();
@ -66,15 +64,15 @@ public class SimpleReflection {
public static void setFieldValue(Class<?> nmsClass, Object instance, String fieldName, Object value) {
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName());
return;
}
Field field = fields.get(fieldName);
if (value == null) {
return;
}
try {
field.set(instance, value);
} catch (IllegalAccessException e) {
Ref.putField(instance, field, value);
} catch (Throwable e) {
e.printStackTrace();
}
}
@ -82,32 +80,33 @@ public class SimpleReflection {
public static Object getFieldValue(Class<?> nmsClass, Object instance, String fieldName) {
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName());
return null;
}
Field field = fields.get(fieldName);
if (field == null) {
return null;
}
try {
return field.get(instance);
} catch (IllegalAccessException e) {
return Ref.getField(instance, field);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("unchecked")
public static <T> T getFieldValue(Class<?> nmsClass, Object instance, String fieldName, T def) {
Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName());
return null;
}
Field field = fields.get(fieldName);
if (field == null) {
return null;
}
try {
return (T) field.get(instance);
} catch (IllegalAccessException e) {
return (T) Ref.getField(instance, field);
} catch (Exception e) {
e.printStackTrace();
}
return def;

View File

@ -6,6 +6,7 @@ import io.izzel.taboolib.Version;
import io.izzel.taboolib.module.lite.SimpleReflection;
import io.izzel.taboolib.module.nms.nbt.*;
import io.izzel.taboolib.module.packet.TPacketHandler;
import io.izzel.taboolib.util.Ref;
import net.minecraft.server.v1_12_R1.ChatMessageType;
import net.minecraft.server.v1_12_R1.EntityVillager;
import net.minecraft.server.v1_12_R1.MinecraftServer;
@ -130,7 +131,7 @@ public class NMSImpl extends NMS {
return "entity.minecraft." + ((net.minecraft.server.v1_14_R1.MinecraftKey) minecraftKey).getKey();
} else if (Version.isAfter(Version.v1_13)) {
try {
String name = "entity.minecraft." + IRegistry.ENTITY_TYPE.getKey((net.minecraft.server.v1_13_R2.EntityTypes<?>) entityTypesField.get(((org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity) entity).getHandle())).getKey();
String name = "entity.minecraft." + IRegistry.ENTITY_TYPE.getKey((net.minecraft.server.v1_13_R2.EntityTypes<?>) Ref.getField(((org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity) entity).getHandle(), entityTypesField)).getKey();
if (entity instanceof Villager && ((CraftVillager) entity).getCareer() != null) {
name += "." + String.valueOf(((CraftVillager) entity).getCareer()).toLowerCase();
}

View File

@ -0,0 +1,57 @@
package io.izzel.taboolib.util;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
/**
* @author Arasple
* @date 2020/1/20 16:40
*/
public class Hastebin {
private static final String HASTEBIN_URL = "https://hasteb.in/";
public static Result paste(String content) {
try {
HttpURLConnection con = (HttpURLConnection) new URL(HASTEBIN_URL + "documents").openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Charset", "UTF-8");
con.setDoInput(true);
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(content.getBytes(StandardCharsets.UTF_8));
return new Result(IO.readFully(con.getInputStream(), StandardCharsets.UTF_8));
} catch (Throwable e) {
return null;
}
}
public static class Result {
private String source;
private JsonObject sourceJson;
public Result(String source) {
this.source = source;
this.sourceJson = new JsonParser().parse(source).getAsJsonObject();
}
public String getURL() {
return HASTEBIN_URL + sourceJson.get("key").getAsString();
}
public String getSource() {
return source;
}
public JsonObject getSourceJson() {
return sourceJson;
}
}
}

View File

@ -1,5 +1,6 @@
package io.izzel.taboolib.util;
import com.google.common.base.Preconditions;
import com.google.gson.annotations.SerializedName;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.TabooLibAPI;
@ -12,6 +13,7 @@ import sun.misc.Unsafe;
import sun.reflect.Reflection;
import javax.annotation.concurrent.ThreadSafe;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@ -19,6 +21,7 @@ import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@SuppressWarnings("restriction")
@ThreadSafe
public class Ref {
@ -28,16 +31,112 @@ public class Ref {
public static final int ACC_BRIDGE = 0x0040;
public static final int ACC_SYNTHETIC = 0x1000;
public static final Unsafe UNSAFE = getUnsafe();
private static final Unsafe UNSAFE;
private static final MethodHandles.Lookup LOOKUP;
static Unsafe getUnsafe() {
static {
try {
return (Unsafe) io.izzel.taboolib.util.Reflection.getValue(null, Unsafe.class, true, "theUnsafe");
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
UNSAFE = (Unsafe) theUnsafe.get(null);
Field lookupField = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
Object lookupBase = UNSAFE.staticFieldBase(lookupField);
long lookupOffset = UNSAFE.staticFieldOffset(lookupField);
LOOKUP = (MethodHandles.Lookup) UNSAFE.getObject(lookupBase, lookupOffset);
} catch (Throwable t) {
t.printStackTrace();
throw new IllegalStateException("Unsafe not found");
}
}
public static Unsafe getUnsafe() {
return UNSAFE;
}
public static MethodHandles.Lookup lookup() {
return LOOKUP;
}
public static void putField(Object src, Field field, Object value) {
Preconditions.checkNotNull(field);
if (Modifier.isStatic(field.getModifiers())) {
Object base = getUnsafe().staticFieldBase(field);
long offset = getUnsafe().staticFieldOffset(field);
put(field, base, offset, value);
} else {
long offset = getUnsafe().objectFieldOffset(field);
put(field, src, offset, value);
}
}
private static void put(Field field, Object base, long offset, Object value) {
Class<?> type = field.getType();
if (type.isPrimitive()) {
if (type == boolean.class) {
getUnsafe().putBoolean(base, offset, (Boolean) value);
} else if (type == int.class) {
getUnsafe().putInt(base, offset, ((Number) value).intValue());
} else if (type == double.class) {
getUnsafe().putDouble(base, offset, ((Number) value).doubleValue());
} else if (type == long.class) {
getUnsafe().putLong(base, offset, ((Number) value).longValue());
} else if (type == float.class) {
getUnsafe().putFloat(base, offset, ((Number) value).floatValue());
} else if (type == short.class) {
getUnsafe().putShort(base, offset, ((Number) value).shortValue());
} else if (type == byte.class) {
getUnsafe().putByte(base, offset, ((Number) value).byteValue());
} else if (type == char.class) {
getUnsafe().putChar(base, offset, ((Character) value));
}
} else {
getUnsafe().putObject(base, offset, value);
}
}
public static <T> T getField(Object src, Field field, Class<T> cast) {
Object obj = getField(src, field);
return obj == null ? null : (T) obj;
}
public static Object getField(Object src, Field field) {
Preconditions.checkNotNull(field);
getUnsafe().ensureClassInitialized(field.getDeclaringClass());
if (Modifier.isStatic(field.getModifiers())) {
Object base = getUnsafe().staticFieldBase(field);
long offset = getUnsafe().staticFieldOffset(field);
return get(field, base, offset);
} else {
long offset = getUnsafe().objectFieldOffset(field);
return get(field, src, offset);
}
}
private static Object get(Field field, Object base, long offset) {
Class<?> type = field.getType();
if (type.isPrimitive()) {
if (type == boolean.class) {
return getUnsafe().getBoolean(base, offset);
} else if (type == int.class) {
return getUnsafe().getInt(base, offset);
} else if (type == double.class) {
return getUnsafe().getDouble(base, offset);
} else if (type == long.class) {
return getUnsafe().getLong(base, offset);
} else if (type == float.class) {
return getUnsafe().getFloat(base, offset);
} else if (type == short.class) {
return getUnsafe().getShort(base, offset);
} else if (type == byte.class) {
return getUnsafe().getByte(base, offset);
} else if (type == char.class) {
return getUnsafe().getChar(base, offset);
} else {
return null;
}
} else {
return getUnsafe().getObject(base, offset);
}
}
public static List<Field> getDeclaredFields(Class<?> clazz) {
return getDeclaredFields(clazz, 0, true);
@ -166,10 +265,8 @@ public class Ref {
return cachePlugin.computeIfAbsent(callerClass.getName(), n -> {
try {
ClassLoader loader = callerClass.getClassLoader();
Field pluginF = loader.getClass().getDeclaredField("plugin");
pluginF.setAccessible(true);
Object o = pluginF.get(loader);
return (JavaPlugin) o;
Object instance = getField(loader, loader.getClass().getDeclaredField("plugin"));
return (JavaPlugin) instance;
} catch (Exception e) {
return TabooLib.getPlugin();
}
@ -194,9 +291,6 @@ public class Ref {
public static void forcedAccess(Field field) {
try {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
} catch (Throwable t) {
t.printStackTrace();
}

View File

@ -247,7 +247,7 @@ public final class Reflection {
* @see #getField(Class, boolean, String)
*/
public static Object getValue(Object instance, Class<?> clazz, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
return getField(clazz, declared, fieldName).get(instance);
return Ref.getField(instance, getField(clazz, declared, fieldName));
}
/**
@ -302,7 +302,7 @@ public final class Reflection {
* @see #getField(Class, boolean, String)
*/
public static void setValue(Object instance, Class<?> clazz, boolean declared, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
getField(clazz, declared, fieldName).set(instance, value);
Ref.putField(instance, getField(clazz, declared, fieldName), value);
}
/**

View File

@ -1,6 +1,7 @@
package io.izzel.taboolib.util.asm;
import io.izzel.taboolib.util.Ref;
import org.bukkit.Bukkit;
import org.objectweb.asm.*;
@ -36,7 +37,7 @@ public class AsmClassTransformer extends ClassVisitor implements Opcodes {
Class<?> clazz = AsmClassLoader.createNewClass(newClassName, writer.toByteArray());
Field field = from.getClassLoader().getClass().getDeclaredField("classes");
field.setAccessible(true);
((Map<String, Class<?>>) field.get(from.getClassLoader())).put(newClassName, clazz);
Ref.getField(from.getClassLoader(), field, Map.class).put(newClassName, clazz);
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();

View File

@ -0,0 +1,333 @@
package io.izzel.taboolib.util.item;
import io.izzel.taboolib.util.Reflection;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.map.*;
import org.bukkit.map.MapView.Scale;
import org.bukkit.plugin.AuthorNagException;
import javax.annotation.Nonnull;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
/**
* @Author LagBug
*/
public class MapBuilder {
public static final String VERSION = "1.5";
private MapView map;
private BufferedImage image;
private List<Text> texts;
private MapCursorCollection cursors;
private boolean rendered;
private boolean renderOnce;
private boolean isNewVersion;
public MapBuilder() {
cursors = new MapCursorCollection();
texts = new ArrayList<>();
rendered = false;
renderOnce = true;
isNewVersion = Bukkit.getVersion().contains("1.15") || Bukkit.getVersion().contains("1.14") || Bukkit.getVersion().contains("1.13");
}
/**
* Get the image that's being used
*
* @return the image used
*/
public BufferedImage getImage() {
return image;
}
/**
* Set an image to be used
*
* @param image the buffered image to use
* @return the instance of this class
*/
public MapBuilder setImage(@Nonnull BufferedImage image) {
this.image = image;
return this;
}
/**
* Set and image to be used
*
* @param x, y the coordinates to add the text
* @param font the font to be used
* @param text the string that will be displayed
* @return the instance of this class
*/
public MapBuilder addText(@Nonnull int x, @Nonnull int y, @Nonnull MapFont font, @Nonnull String text) {
this.texts.add(new Text(x, y, font, text));
return this;
}
/**
* Gets the list of all the texts used
*
* @return a List of all the texts
*/
public List<Text> getTexts() {
return texts;
}
/**
* Adds a cursor to the map
*
* @param x, y the coordinates to add the cursor
* @param direction the direction to display the cursor
* @param type the type of the cursor
* @return the instance of this class
*/
@SuppressWarnings("deprecation")
public MapBuilder addCursor(@Nonnull int x, @Nonnull int y, @Nonnull CursorDirection direction, @Nonnull CursorType type) {
cursors.addCursor(x, y, (byte) direction.getId(), (byte) type.getId());
return this;
}
/**
* Gets all the currently used cursors
*
* @return a MapCursorCollection with all current cursors
*/
public MapCursorCollection getCursors() {
return cursors;
}
/**
* Sets whether the image should only be rendered once.
* Good for static images and reduces lag.
*
* @param renderOnce the value to determine if it's going to be rendered once
* @return the instance of this class
*/
public MapBuilder setRenderOnce(@Nonnull boolean renderOnce) {
this.renderOnce = renderOnce;
return this;
}
/**
* Builds an ItemStack of the map.
*
* @return the ItemStack of the map containing what's been set from the above methods
*/
@SuppressWarnings("deprecation")
public ItemStack build() {
ItemStack item = null;
try {
item = new ItemStack(isNewVersion ? Material.MAP : Material.valueOf("MAP"));
} catch (AuthorNagException ex) {
System.out.println("Could not get material for the current spigot version. This won't be shown again until server restats");
}
map = Bukkit.createMap(Bukkit.getWorlds().get(0));
map.setScale(Scale.NORMAL);
map.getRenderers().forEach(map::removeRenderer);
map.addRenderer(new MapRenderer() {
@Override
public void render(MapView mapView, MapCanvas mapCanvas, Player player) {
if (rendered && renderOnce) {
return;
}
if (player != null && player.isOnline()) {
if (image != null) {
mapCanvas.drawImage(0, 0, image);
}
if (!texts.isEmpty()) {
texts.forEach(text -> mapCanvas.drawText(text.getX(), text.getY(), text.getFont(), text.getMessage()));
}
if (cursors.size() > 0) {
mapCanvas.setCursors(cursors);
}
rendered = true;
}
}
});
if (isNewVersion) {
MapMeta mapMeta = (MapMeta) item.getItemMeta();
try {
Reflection.invokeMethod(mapMeta, "setMapView", map);
} catch (Throwable ignored) {
}
item.setItemMeta(mapMeta);
} else {
item.setDurability(getMapId(map));
}
return item;
}
/**
* Gets a map id cross-version using reflection
*
* @param mapView the map to get the id
* @return the instance of this class
*/
private short getMapId(@Nonnull MapView mapView) {
try {
return (short) mapView.getId();
} catch (NoSuchMethodError ex) {
try {
return (short) Class.forName("org.bukkit.map.MapView").getMethod("getId", (Class<?>[]) new Class[0])
.invoke(mapView, new Object[0]);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | ClassNotFoundException e) {
e.printStackTrace();
return -1;
}
}
}
/**
* An enum containing user friendly cursor directions. Instead of using the integers, you can instead use this enum
*/
public enum CursorDirection {
SOUTH(0), SOUTH_WEST_SOUTH(1), SOUTH_WEST(2), SOUTH_WEST_WEST(3), WEST(4), NORTH_WEST_WEST(5), NORTH_WEST(6),
NORTH_WEST_NORTH(7), NORTH(8), NORTH_EAST_NORTH(9), NORTH_EAST(10), NORTH_EAST_EAST(11), EAST(12),
SOUTH_EAST_EAST(13), SOUNT_EAST(14), SOUTH_EAST_SOUTH(15);
private final int id;
CursorDirection(@Nonnull int id) {
this.id = id;
}
/**
* Returns the actual integer to use
*
* @return the integer of the specified enum type
*/
public int getId() {
return this.id;
}
}
/**
* An enum containing user friendly cursor types. Instead of using the integers, you can instead use this enum
*/
public enum CursorType {
WHITE_POINTER(0), GREEN_POINTER(1), RED_POINTER(2), BLUE_POINTER(3), WHITE_CLOVER(4), RED_BOLD_POINTER(5),
WHITE_DOT(6), LIGHT_BLUE_SQUARE(7);
private final int id;
CursorType(@Nonnull int id) {
this.id = id;
}
/**
* Returns the actual integer to use
*
* @return the integer of the specified enum type
*/
public int getId() {
return this.id;
}
}
/**
* A storage class to save text information to later be used in order to write in maps
*/
public static class Text {
private int x;
private int y;
private MapFont font;
private String message;
public Text(@Nonnull int x, @Nonnull int y, @Nonnull MapFont font, @Nonnull String message) {
setX(x);
setY(y);
setFont(font);
setMessage(message);
}
/**
* Gets the x position for the text to be displayed
*
* @return the x position
*/
public int getX() {
return x;
}
/**
* Sets the x position of the text to display it
*
* @param x the x postion
*/
public void setX(@Nonnull int x) {
this.x = x;
}
/**
* Gets the y position for the text to be displayed
*
* @return the y position
*/
public int getY() {
return y;
}
/**
* Sets the y position of the text to display it
*
* @param y the y position
*/
public void setY(@Nonnull int y) {
this.y = y;
}
/**
* Gets the font to be used
*
* @return the MapFont that is used
*/
public MapFont getFont() {
return font;
}
/**
* Sets what font should be used
*
* @param font the actual font
*/
public void setFont(@Nonnull MapFont font) {
this.font = font;
}
/**
* Gets what test will be displayed
*
* @return the text
*/
public String getMessage() {
return message;
}
/**
* Sets what text will be displayed
*
* @param message the actual text
*/
public void setMessage(@Nonnull String message) {
this.message = message;
}
}
}

View File

@ -2,6 +2,7 @@ package io.izzel.taboolib.util.plugin;
import com.google.common.base.Joiner;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.util.Ref;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
@ -248,26 +249,26 @@ public class PluginUtils {
try {
Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins");
pluginsField.setAccessible(true);
plugins = (List) pluginsField.get(pluginManager);
plugins = (List) Ref.getField(pluginManager, pluginsField);
Field lookupNamesField = Bukkit.getPluginManager().getClass().getDeclaredField("lookupNames");
lookupNamesField.setAccessible(true);
names = (Map) lookupNamesField.get(pluginManager);
names = (Map) Ref.getField(pluginManager, lookupNamesField);
Field commandMapField;
try {
commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("listeners");
commandMapField.setAccessible(true);
listeners = (Map) commandMapField.get(pluginManager);
listeners = (Map) Ref.getField(pluginManager, commandMapField);
} catch (Exception ignored) {
}
commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
commandMapField.setAccessible(true);
commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
commandMap = (SimpleCommandMap) Ref.getField(pluginManager, commandMapField);
Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true);
commands = (Map) knownCommandsField.get(commandMap);
} catch (NoSuchFieldException | IllegalAccessException e) {
commands = (Map) Ref.getField(commandMap, knownCommandsField);
} catch (NoSuchFieldException e) {
return new PluginUnloadState(true, e.toString());
}
}

View File

@ -3,6 +3,7 @@ package io.izzel.taboolib.util.serialize;
import com.google.common.collect.Maps;
import com.google.gson.*;
import io.izzel.taboolib.module.lite.SimpleReflection;
import io.izzel.taboolib.util.Ref;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
@ -36,7 +37,7 @@ public class TSerializer {
}
// Serializable
if (declaredField.isAnnotationPresent(TSerializeCustom.class) && TSerializable.class.isAssignableFrom(declaredField.getType())) {
declaredField.set(serializable, generateElement((Class<? extends TSerializable>) declaredField.getType()).read(jsonElementEntry.getValue().getAsString()));
Ref.putField(serializable, declaredField, generateElement((Class<? extends TSerializable>) declaredField.getType()).read(jsonElementEntry.getValue().getAsString()));
}
// List
else if (declaredField.isAnnotationPresent(TSerializeCollection.class) && Collection.class.isAssignableFrom(declaredField.getType())) {
@ -49,7 +50,7 @@ public class TSerializer {
if (serializer == null) {
serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
} else {
readCollection((Collection) declaredField.get(serializable), jsonElementEntry.getValue().getAsString(), checkCustom(listType, serializer));
readCollection((Collection) Ref.getField(serializable, declaredField), jsonElementEntry.getValue().getAsString(), checkCustom(listType, serializer));
}
}
// Map
@ -64,7 +65,7 @@ public class TSerializer {
if (serializerK == null || serializerV == null) {
serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
} else {
readMap((Map) declaredField.get(serializable), jsonElementEntry.getValue().getAsString(), checkCustom(mapType[0], serializerK), checkCustom(mapType[1], serializerV));
readMap((Map) Ref.getField(serializable, declaredField), jsonElementEntry.getValue().getAsString(), checkCustom(mapType[0], serializerK), checkCustom(mapType[1], serializerV));
}
}
// 未声明类型
@ -73,7 +74,7 @@ public class TSerializer {
if (serializer == null) {
serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
} else {
declaredField.set(serializable, serializer.getSerializer().read(jsonElementEntry.getValue().getAsString()));
Ref.putField(serializable, declaredField, serializer.getSerializer().read(jsonElementEntry.getValue().getAsString()));
}
}
} catch (Throwable t) {
@ -94,7 +95,7 @@ public class TSerializer {
declaredField.setAccessible(true);
try {
if (!declaredField.isAnnotationPresent(DoNotSerialize.class) && !Modifier.isStatic(declaredField.getModifiers())) {
Object fieldObject = declaredField.get(serializable);
Object fieldObject = Ref.getField(serializable, declaredField);
if (fieldObject == null) {
continue;
}