Update LightAPI

This commit is contained in:
sky 2020-04-02 18:18:50 +08:00
parent 075f505616
commit cba68af5bb
9 changed files with 428 additions and 18 deletions

View File

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

View File

@ -5,7 +5,6 @@ import com.google.common.collect.Maps;
import io.izzel.taboolib.client.TabooLibClient; import io.izzel.taboolib.client.TabooLibClient;
import io.izzel.taboolib.client.TabooLibServer; import io.izzel.taboolib.client.TabooLibServer;
import io.izzel.taboolib.metrics.BStats; import io.izzel.taboolib.metrics.BStats;
import io.izzel.taboolib.metrics.CStats;
import io.izzel.taboolib.module.dependency.TDependencyInjector; import io.izzel.taboolib.module.dependency.TDependencyInjector;
import io.izzel.taboolib.module.inject.TSchedule; import io.izzel.taboolib.module.inject.TSchedule;
import io.izzel.taboolib.util.Files; import io.izzel.taboolib.util.Files;
@ -36,7 +35,6 @@ public class TabooLibLoader {
TDependencyInjector.inject(TabooLib.getPlugin(), TabooLib.class); TDependencyInjector.inject(TabooLib.getPlugin(), TabooLib.class);
// 插件统计 // 插件统计
BStats bStats = new BStats(TabooLib.getPlugin()); BStats bStats = new BStats(TabooLib.getPlugin());
CStats cStats = new CStats(TabooLib.getPlugin());
bStats.addCustomChart(new BStats.SingleLineChart("plugins_using_taboolib", () -> Math.toIntExact(Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(TabooLibAPI::isDependTabooLib).count()))); bStats.addCustomChart(new BStats.SingleLineChart("plugins_using_taboolib", () -> Math.toIntExact(Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(TabooLibAPI::isDependTabooLib).count())));
// 读取插件类 // 读取插件类
setupClasses(TabooLib.getPlugin()); setupClasses(TabooLib.getPlugin());

View File

@ -11,7 +11,9 @@ import io.izzel.taboolib.module.db.local.LocalPlayer;
import io.izzel.taboolib.module.hologram.Hologram; import io.izzel.taboolib.module.hologram.Hologram;
import io.izzel.taboolib.module.hologram.THologram; import io.izzel.taboolib.module.hologram.THologram;
import io.izzel.taboolib.module.inject.TListener; import io.izzel.taboolib.module.inject.TListener;
import io.izzel.taboolib.module.light.TLight;
import io.izzel.taboolib.module.locale.logger.TLogger; import io.izzel.taboolib.module.locale.logger.TLogger;
import io.izzel.taboolib.module.nms.impl.Type;
import io.izzel.taboolib.module.tellraw.TellrawJson; import io.izzel.taboolib.module.tellraw.TellrawJson;
import io.izzel.taboolib.util.Files; import io.izzel.taboolib.util.Files;
import io.izzel.taboolib.util.book.BookFormatter; import io.izzel.taboolib.util.book.BookFormatter;
@ -121,8 +123,29 @@ public class ListenerCommand implements Listener {
.hoverText("HoverText")) .hoverText("HoverText"))
.open(player); .open(player);
} }
},
new Module() {
@Override
public String[] name() {
return new String[] {"light"};
}
@Override
public void run(Player player) {
player.sendMessage("§8[§fTabooLib§8] §7Lighting. §a(+)");
TLight.create(player.getLocation().getBlock(), Type.BLOCK, 15);
Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> {
TLight.create(player.getLocation().getBlock(), Type.BLOCK, 5);
player.sendMessage("§8[§fTabooLib§8] §7Lighting. §c(-)");
}, 20);
Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> {
TLight.delete(player.getLocation().getBlock(), Type.BLOCK);
player.sendMessage("§8[§fTabooLib§8] §7Lighting. §8(-)");
}, 40);
}
}); });
@Startup.Starting @Startup.Starting
public void init() { public void init() {
// 版本命令 // 版本命令

View File

@ -0,0 +1,55 @@
package io.izzel.taboolib.module.light;
import io.izzel.taboolib.module.nms.NMS;
import io.izzel.taboolib.module.nms.impl.Type;
import org.bukkit.Location;
import org.bukkit.block.Block;
/**
* @Author sky
* @Since 2020-04-02 18:10
*/
public class TLight {
public static boolean create(Block block, Type lightType, int lightLevel) {
if (NMS.handle().getRawLightLevel(block, lightType) > lightLevel) {
NMS.handle().deleteLight(block, lightType);
}
boolean r = NMS.handle().createLight(block, lightType, lightLevel);
NMS.handle().update(block.getChunk());
return r;
}
public static boolean delete(Block block, Type lightType) {
boolean r = NMS.handle().deleteLight(block, lightType);
NMS.handle().update(block.getChunk());
return r;
}
public static boolean create(Location location, Type lightType, int lightLevel) {
if (NMS.handle().getRawLightLevel(location.getBlock(), lightType) > lightLevel) {
deleteLight(location, lightType);
}
boolean r = NMS.handle().createLight(location.getBlock(), lightType, lightLevel);
NMS.handle().update(location.getChunk());
return r;
}
public static boolean delete(Location location, Type lightType) {
boolean r = NMS.handle().deleteLight(location.getBlock(), lightType);
NMS.handle().update(location.getChunk());
return r;
}
public static boolean createLight(Location location, Type lightType, int lightLevel) {
if (NMS.handle().getRawLightLevel(location.getBlock(), lightType) > lightLevel) {
deleteLight(location, lightType);
}
return NMS.handle().createLight(location.getBlock(), lightType, lightLevel);
}
public static boolean deleteLight(Location location, Type lightType) {
return NMS.handle().deleteLight(location.getBlock(), lightType);
}
}

View File

@ -5,45 +5,71 @@ import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.util.Ref; import io.izzel.taboolib.util.Ref;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
/** /**
* @Author 坏黑 * @Author sky
* @Since 2018-10-25 22:51 * @Since 2018-10-25 22:51
*/ */
public class SimpleReflection { public class SimpleReflection {
private static Map<String, Map<String, Field>> fieldCached = Maps.newHashMap(); private static Map<String, Map<String, Field>> fieldCached = Maps.newHashMap();
private static Map<String, Map<String, Method>> methodCached = Maps.newHashMap();
@Deprecated
public static boolean isExists(Class<?> nmsClass) { public static boolean isExists(Class<?> nmsClass) {
return fieldCached.containsKey(nmsClass.getName()); return fieldCached.containsKey(nmsClass.getName());
} }
public static boolean isFieldExists(Class<?> nmsClass) {
return fieldCached.containsKey(nmsClass.getName());
}
public static boolean isMethodExists(Class<?> nmsClass) {
return methodCached.containsKey(nmsClass.getName());
}
public static Map<String, Field> getFields(Class<?> nmsClass) { public static Map<String, Field> getFields(Class<?> nmsClass) {
return fieldCached.getOrDefault(nmsClass.getName(), Maps.newHashMap()); return fieldCached.getOrDefault(nmsClass.getName(), Maps.newHashMap());
} }
public static Map<String, Method> getMethods(Class<?> nmsClass) {
return methodCached.getOrDefault(nmsClass.getName(), Maps.newHashMap());
}
public static Field getField(Class<?> nmsClass, String fieldName) { public static Field getField(Class<?> nmsClass, String fieldName) {
return fieldCached.getOrDefault(nmsClass.getName(), Maps.newHashMap()).get(fieldName); return fieldCached.getOrDefault(nmsClass.getName(), Maps.newHashMap()).get(fieldName);
} }
public static Method getMethod(Class<?> nmsClass, String methodName) {
return methodCached.getOrDefault(nmsClass.getName(), Maps.newHashMap()).get(methodName);
}
public static void checkAndSave(Class<?>... nmsClass) { public static void checkAndSave(Class<?>... nmsClass) {
Arrays.stream(nmsClass).forEach(SimpleReflection::checkAndSave); Arrays.stream(nmsClass).forEach(SimpleReflection::checkAndSave);
} }
public static void checkAndSave(Class<?> nmsClass) { public static void checkAndSave(Class<?> nmsClass) {
if (!isExists(nmsClass)) { if (!isFieldExists(nmsClass)) {
saveField(nmsClass); saveField(nmsClass);
} }
if (!isMethodExists(nmsClass)) {
saveMethod(nmsClass);
}
} }
public static void saveField(Class<?>... nmsClass) { public static void saveField(Class<?>... nmsClass) {
Arrays.stream(nmsClass).forEach(SimpleReflection::saveField); Arrays.stream(nmsClass).forEach(SimpleReflection::saveField);
} }
public static void saveMethod(Class<?>... nmsClass) {
Arrays.stream(nmsClass).forEach(SimpleReflection::saveMethod);
}
public static void saveField(Class<?> nmsClass) { public static void saveField(Class<?> nmsClass) {
try { try {
Arrays.stream(nmsClass.getDeclaredFields()).forEach(declaredField -> saveField(nmsClass, declaredField.getName())); Arrays.stream(nmsClass.getDeclaredFields()).forEach(declaredField -> saveField(nmsClass, declaredField.getName()));
@ -52,6 +78,14 @@ public class SimpleReflection {
} }
} }
public static void saveMethod(Class<?> nmsClass) {
try {
Ref.getDeclaredMethods(nmsClass).forEach(declaredMethod -> saveMethod(nmsClass, declaredMethod.getName()));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void saveField(Class<?> nmsClass, String fieldName) { public static void saveField(Class<?> nmsClass, String fieldName) {
try { try {
Field declaredField = nmsClass.getDeclaredField(fieldName); Field declaredField = nmsClass.getDeclaredField(fieldName);
@ -61,7 +95,23 @@ public class SimpleReflection {
} }
} }
public static void saveMethod(Class<?> nmsClass, String methodName) {
try {
Method declaredMethod = nmsClass.getDeclaredMethod(methodName);
methodCached.computeIfAbsent(nmsClass.getName(), name -> Maps.newHashMap()).put(methodName, declaredMethod);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setFieldValue(Class<?> nmsClass, Object instance, String fieldName, Object value) { public static void setFieldValue(Class<?> nmsClass, Object instance, String fieldName, Object value) {
setFieldValue(nmsClass, instance, fieldName, value, false);
}
public static void setFieldValue(Class<?> nmsClass, Object instance, String fieldName, Object value, boolean check) {
if (check) {
checkAndSave(nmsClass);
}
Map<String, Field> fields = fieldCached.get(nmsClass.getName()); Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) { if (fields == null) {
return; return;
@ -78,6 +128,13 @@ public class SimpleReflection {
} }
public static Object getFieldValue(Class<?> nmsClass, Object instance, String fieldName) { public static Object getFieldValue(Class<?> nmsClass, Object instance, String fieldName) {
return getFieldValueChecked(nmsClass, instance, fieldName, false);
}
public static Object getFieldValueChecked(Class<?> nmsClass, Object instance, String fieldName, boolean check) {
if (check) {
checkAndSave(nmsClass);
}
Map<String, Field> fields = fieldCached.get(nmsClass.getName()); Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) { if (fields == null) {
return null; return null;
@ -94,8 +151,14 @@ public class SimpleReflection {
return null; return null;
} }
@SuppressWarnings("unchecked")
public static <T> T getFieldValue(Class<?> nmsClass, Object instance, String fieldName, T def) { public static <T> T getFieldValue(Class<?> nmsClass, Object instance, String fieldName, T def) {
return getFieldValue(nmsClass, instance, fieldName, def, false);
}
public static <T> T getFieldValue(Class<?> nmsClass, Object instance, String fieldName, T def, boolean check) {
if (check) {
checkAndSave(nmsClass);
}
Map<String, Field> fields = fieldCached.get(nmsClass.getName()); Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) { if (fields == null) {
return null; return null;
@ -112,6 +175,31 @@ public class SimpleReflection {
return def; return def;
} }
public static Object invokeMethod(Class<?> nmsClass, Object instance, String methodName, Object[] arguments) {
return invokeMethod(nmsClass, instance, methodName, arguments, false);
}
public static Object invokeMethod(Class<?> nmsClass, Object instance, String methodName, Object[] arguments, boolean check) {
if (check) {
checkAndSave(nmsClass);
}
Map<String, Method> methods = methodCached.get(nmsClass.getName());
if (methods == null) {
return null;
}
Method method = methods.get(methodName);
if (method == null) {
return null;
}
try {
method.setAccessible(true);
return method.invoke(instance, arguments);
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public static Class getListType(Field field) { public static Class getListType(Field field) {
Type genericType = field.getGenericType(); Type genericType = field.getGenericType();
try { try {

View File

@ -2,11 +2,13 @@ package io.izzel.taboolib.module.nms;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.izzel.taboolib.module.inject.TInject; import io.izzel.taboolib.module.inject.TInject;
import io.izzel.taboolib.module.nms.impl.Type;
import io.izzel.taboolib.module.nms.impl.Position; import io.izzel.taboolib.module.nms.impl.Position;
import io.izzel.taboolib.module.nms.nbt.Attribute; import io.izzel.taboolib.module.nms.nbt.Attribute;
import io.izzel.taboolib.module.nms.nbt.NBTAttribute; import io.izzel.taboolib.module.nms.nbt.NBTAttribute;
import io.izzel.taboolib.module.nms.nbt.NBTCompound; import io.izzel.taboolib.module.nms.nbt.NBTCompound;
import io.izzel.taboolib.module.nms.nbt.NBTList; import io.izzel.taboolib.module.nms.nbt.NBTList;
import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Particle; import org.bukkit.Particle;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -99,4 +101,16 @@ public abstract class NMS {
abstract public Object asEntityType(String name); abstract public Object asEntityType(String name);
abstract public boolean createLight(Block block, Type lightType, int lightLevel);
abstract public boolean deleteLight(Block block, Type lightType);
abstract public void setRawLightLevel(Block block, Type lightType, int lightLevel);
abstract public int getRawLightLevel(Block block, Type lightType);
abstract public void recalculate(Block block, Type lightType);
abstract public void update(Chunk chunk);
} }

View File

@ -3,7 +3,10 @@ package io.izzel.taboolib.module.nms;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import io.izzel.taboolib.Version; import io.izzel.taboolib.Version;
import io.izzel.taboolib.module.nms.impl.Type;
import io.izzel.taboolib.module.lite.SimpleReflection; import io.izzel.taboolib.module.lite.SimpleReflection;
import io.izzel.taboolib.module.locale.logger.TLogger;
import io.izzel.taboolib.module.nms.nbt.NBTList;
import io.izzel.taboolib.module.nms.nbt.*; import io.izzel.taboolib.module.nms.nbt.*;
import io.izzel.taboolib.module.packet.TPacketHandler; import io.izzel.taboolib.module.packet.TPacketHandler;
import io.izzel.taboolib.util.Ref; import io.izzel.taboolib.util.Ref;
@ -13,12 +16,26 @@ import net.minecraft.server.v1_12_R1.MinecraftServer;
import net.minecraft.server.v1_12_R1.NBTTagCompound; import net.minecraft.server.v1_12_R1.NBTTagCompound;
import net.minecraft.server.v1_13_R2.EnumHand; import net.minecraft.server.v1_13_R2.EnumHand;
import net.minecraft.server.v1_13_R2.IRegistry; import net.minecraft.server.v1_13_R2.IRegistry;
import net.minecraft.server.v1_8_R3.NBTBase; import net.minecraft.server.v1_14_R1.*;
import net.minecraft.server.v1_8_R3.*; import net.minecraft.server.v1_15_R1.LightEngineThreaded;
import org.bukkit.Bukkit; import net.minecraft.server.v1_8_R3.ChatComponentText;
import org.bukkit.Location; import net.minecraft.server.v1_8_R3.GenericAttributes;
import net.minecraft.server.v1_8_R3.NBTTagByte;
import net.minecraft.server.v1_8_R3.NBTTagByteArray;
import net.minecraft.server.v1_8_R3.NBTTagDouble;
import net.minecraft.server.v1_8_R3.NBTTagFloat;
import net.minecraft.server.v1_8_R3.NBTTagInt;
import net.minecraft.server.v1_8_R3.NBTTagIntArray;
import net.minecraft.server.v1_8_R3.NBTTagList;
import net.minecraft.server.v1_8_R3.NBTTagLong;
import net.minecraft.server.v1_8_R3.NBTTagShort;
import net.minecraft.server.v1_8_R3.NBTTagString;
import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
import net.minecraft.server.v1_8_R3.PacketPlayOutTitle;
import org.bukkit.Chunk;
import org.bukkit.Particle; import org.bukkit.Particle;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_12_R1.CraftParticle; import org.bukkit.craftbukkit.v1_12_R1.CraftParticle;
import org.bukkit.craftbukkit.v1_13_R2.CraftServer; import org.bukkit.craftbukkit.v1_13_R2.CraftServer;
@ -31,7 +48,6 @@ import org.bukkit.entity.Player;
import org.bukkit.entity.Villager; import org.bukkit.entity.Villager;
import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.ProjectileHitEvent; import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@ -39,6 +55,7 @@ import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
@ -48,6 +65,8 @@ import java.util.function.Consumer;
public class NMSImpl extends NMS { public class NMSImpl extends NMS {
private Field entityTypesField; private Field entityTypesField;
private boolean is11400 = Version.isAfter(Version.v1_14);
private boolean is11500 = Version.isAfter(Version.v1_15);
static { static {
SimpleReflection.saveField(NBTTagString.class); SimpleReflection.saveField(NBTTagString.class);
@ -77,7 +96,7 @@ public class NMSImpl extends NMS {
} }
@Override @Override
public void openBook(Player player, ItemStack book) { public void openBook(Player player, org.bukkit.inventory.ItemStack book) {
// 你妈 1.14.3 a() 1.14.4 openBook() 不改 nms 版本号都是 1_14_R1神经病吧 // 你妈 1.14.3 a() 1.14.4 openBook() 不改 nms 版本号都是 1_14_R1神经病吧
Object bookItem = org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack.asNMSCopy(book); Object bookItem = org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack.asNMSCopy(book);
try { try {
@ -106,7 +125,7 @@ public class NMSImpl extends NMS {
} }
@Override @Override
public String getName(ItemStack itemStack) { public String getName(org.bukkit.inventory.ItemStack itemStack) {
Object nmsItem = CraftItemStack.asNMSCopy(itemStack); Object nmsItem = CraftItemStack.asNMSCopy(itemStack);
if (Version.isAfter(Version.v1_13)) { if (Version.isAfter(Version.v1_13)) {
String name = ((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).getItem().getName(); String name = ((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).getItem().getName();
@ -225,7 +244,7 @@ public class NMSImpl extends NMS {
} }
@Override @Override
public ItemStack _NBT(ItemStack itemStack, Object compound) { public org.bukkit.inventory.ItemStack _NBT(org.bukkit.inventory.ItemStack itemStack, Object compound) {
Object nmsItem = CraftItemStack.asNMSCopy(itemStack); Object nmsItem = CraftItemStack.asNMSCopy(itemStack);
try { try {
((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).setTag((net.minecraft.server.v1_8_R3.NBTTagCompound) toNBTBase((io.izzel.taboolib.module.nms.nbt.NBTBase) compound)); ((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).setTag((net.minecraft.server.v1_8_R3.NBTTagCompound) toNBTBase((io.izzel.taboolib.module.nms.nbt.NBTBase) compound));
@ -236,7 +255,7 @@ public class NMSImpl extends NMS {
} }
@Override @Override
public Object _NBT(ItemStack itemStack) { public Object _NBT(org.bukkit.inventory.ItemStack itemStack) {
Object nmsItem = CraftItemStack.asNMSCopy(itemStack); Object nmsItem = CraftItemStack.asNMSCopy(itemStack);
try { try {
return ((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).hasTag() ? fromNBTBase(((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).getTag()) : new NBTCompound(); return ((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).hasTag() ? fromNBTBase(((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).getTag()) : new NBTCompound();
@ -247,7 +266,7 @@ public class NMSImpl extends NMS {
} }
@Override @Override
public List<NBTAttribute> getBaseAttribute(ItemStack item) { public List<NBTAttribute> getBaseAttribute(org.bukkit.inventory.ItemStack item) {
List<NBTAttribute> list = Lists.newArrayList(); List<NBTAttribute> list = Lists.newArrayList();
Object nmsItem = CraftItemStack.asNMSCopy(item); Object nmsItem = CraftItemStack.asNMSCopy(item);
Object attr; Object attr;
@ -361,7 +380,7 @@ public class NMSImpl extends NMS {
} }
// 1.12- // 1.12-
else { else {
((NBTTagList) nmsList).add((NBTBase) toNBTBase(value)); ((NBTTagList) nmsList).add((net.minecraft.server.v1_8_R3.NBTBase) toNBTBase(value));
} }
} }
return nmsList; return nmsList;
@ -502,4 +521,206 @@ public class NMSImpl extends NMS {
return net.minecraft.server.v1_13_R2.EntityTypes.a(name); return net.minecraft.server.v1_13_R2.EntityTypes.a(name);
} }
} }
@Override
public boolean createLight(Block block, Type lightType, int lightLevel) {
int level = getRawLightLevel(block, lightType);
setRawLightLevel(block, lightType, lightLevel);
recalculate(block, lightType);
return getRawLightLevel(block, lightType) >= level;
}
@Override
public boolean deleteLight(Block block, Type lightType) {
int level = getRawLightLevel(block, lightType);
setRawLightLevel(block, lightType, 0);
recalculate(block, lightType);
return getRawLightLevel(block, lightType) != level;
}
@Override
public void setRawLightLevel(Block block, Type lightType, int lightLevel) {
int level = Math.max(Math.min(lightLevel, 15), 0);
Object world = ((CraftWorld) block.getWorld()).getHandle();
Object position = new net.minecraft.server.v1_15_R1.BlockPosition(block.getX(), block.getY(), block.getZ());
if (is11400) {
sync(((net.minecraft.server.v1_14_R1.WorldServer) world).getChunkProvider().getLightEngine(), lightEngine -> {
if (lightType == Type.BLOCK) {
Object lightEngineLayer = ((net.minecraft.server.v1_14_R1.LightEngineThreaded) lightEngine).a(net.minecraft.server.v1_14_R1.EnumSkyBlock.BLOCK);
setRawLightLevelBlock(level, position, lightEngineLayer);
} else if (lightType == Type.SKY) {
Object lightEngineLayer = ((net.minecraft.server.v1_14_R1.LightEngineThreaded) lightEngine).a(net.minecraft.server.v1_14_R1.EnumSkyBlock.SKY);
setRawLightLevelSky(level, position, lightEngineLayer);
} else {
Object lightEngineLayer1 = ((net.minecraft.server.v1_14_R1.LightEngineThreaded) lightEngine).a(net.minecraft.server.v1_14_R1.EnumSkyBlock.BLOCK);
Object lightEngineLayer2 = ((net.minecraft.server.v1_14_R1.LightEngineThreaded) lightEngine).a(net.minecraft.server.v1_14_R1.EnumSkyBlock.SKY);
setRawLightLevelBlock(level, position, lightEngineLayer1);
setRawLightLevelSky(level, position, lightEngineLayer2);
}
});
} else {
if (lightType == Type.BLOCK) {
((net.minecraft.server.v1_13_R2.WorldServer) world).a(net.minecraft.server.v1_13_R2.EnumSkyBlock.BLOCK, (net.minecraft.server.v1_13_R2.BlockPosition) position, level);
} else if (lightType == Type.SKY) {
((net.minecraft.server.v1_13_R2.WorldServer) world).a(net.minecraft.server.v1_13_R2.EnumSkyBlock.SKY, (net.minecraft.server.v1_13_R2.BlockPosition) position, level);
} else {
((net.minecraft.server.v1_13_R2.WorldServer) world).a(net.minecraft.server.v1_13_R2.EnumSkyBlock.BLOCK, (net.minecraft.server.v1_13_R2.BlockPosition) position, level);
((net.minecraft.server.v1_13_R2.WorldServer) world).a(net.minecraft.server.v1_13_R2.EnumSkyBlock.SKY, (net.minecraft.server.v1_13_R2.BlockPosition) position, level);
}
}
}
@Override
public int getRawLightLevel(Block block, Type lightType) {
Object world = ((CraftWorld) block.getWorld()).getHandle();
Object position = new net.minecraft.server.v1_15_R1.BlockPosition(block.getX(), block.getY(), block.getZ());
if (lightType == Type.BLOCK) {
return ((net.minecraft.server.v1_13_R2.WorldServer) world).getBrightness(net.minecraft.server.v1_13_R2.EnumSkyBlock.BLOCK, (net.minecraft.server.v1_13_R2.BlockPosition) position);
} else if (lightType == Type.SKY) {
return ((net.minecraft.server.v1_13_R2.WorldServer) world).getBrightness(net.minecraft.server.v1_13_R2.EnumSkyBlock.SKY, (net.minecraft.server.v1_13_R2.BlockPosition) position);
} else {
return ((net.minecraft.server.v1_13_R2.WorldServer) world).getLightLevel((net.minecraft.server.v1_13_R2.BlockPosition) position);
}
}
@Override
public void recalculate(Block block, Type lightType) {
Object world = ((CraftWorld) block.getWorld()).getHandle();
Object position = new net.minecraft.server.v1_15_R1.BlockPosition(block.getX(), block.getY(), block.getZ());
if (is11400) {
Object lightEngine = ((net.minecraft.server.v1_14_R1.WorldServer) world).getChunkProvider().getLightEngine();
if (((net.minecraft.server.v1_14_R1.LightEngineThreaded) lightEngine).a()) {
sync(lightEngine, e -> {
Object[] lightEngineLayers;
if (lightType == Type.BLOCK) {
((LightEngineLayer) ((net.minecraft.server.v1_14_R1.LightEngineThreaded) lightEngine).a(net.minecraft.server.v1_14_R1.EnumSkyBlock.BLOCK)).a(Integer.MAX_VALUE, true, true);
} else if (lightType == Type.SKY) {
((LightEngineLayer) ((net.minecraft.server.v1_14_R1.LightEngineThreaded) lightEngine).a(net.minecraft.server.v1_14_R1.EnumSkyBlock.SKY)).a(Integer.MAX_VALUE, true, true);
} else {
Object b = ((net.minecraft.server.v1_14_R1.LightEngineThreaded) lightEngine).a(net.minecraft.server.v1_14_R1.EnumSkyBlock.BLOCK);
Object s = ((net.minecraft.server.v1_14_R1.LightEngineThreaded) lightEngine).a(net.minecraft.server.v1_14_R1.EnumSkyBlock.SKY);
int maxUpdateCount = Integer.MAX_VALUE;
int integer4 = maxUpdateCount / 2;
int integer5 = ((LightEngineLayer) b).a(integer4, true, true);
int integer6 = maxUpdateCount - integer4 + integer5;
int integer7 = ((LightEngineLayer) s).a(integer6, true, true);
if (integer5 == 0 && integer7 > 0) {
((LightEngineLayer) b).a(integer7, true, true);
}
}
});
}
} else {
if (lightType == Type.SKY) {
((net.minecraft.server.v1_13_R2.WorldServer) world).c(net.minecraft.server.v1_13_R2.EnumSkyBlock.SKY, (net.minecraft.server.v1_13_R2.BlockPosition) position);
} else if (lightType == Type.BLOCK) {
((net.minecraft.server.v1_13_R2.WorldServer) world).c(net.minecraft.server.v1_13_R2.EnumSkyBlock.BLOCK, (net.minecraft.server.v1_13_R2.BlockPosition) position);
} else {
((net.minecraft.server.v1_13_R2.WorldServer) world).c(net.minecraft.server.v1_13_R2.EnumSkyBlock.SKY, (net.minecraft.server.v1_13_R2.BlockPosition) position);
((net.minecraft.server.v1_13_R2.WorldServer) world).c(net.minecraft.server.v1_13_R2.EnumSkyBlock.BLOCK, (net.minecraft.server.v1_13_R2.BlockPosition) position);
}
}
}
@Override
public void update(Chunk chunk) {
for (Player player : Bukkit.getOnlinePlayers()) {
Object human = ((org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer) player).getHandle();
Object chunk1 = ((CraftWorld) player.getWorld()).getHandle().getChunkAt(chunk.getX(), chunk.getZ());
Object chunk2 = ((net.minecraft.server.v1_8_R3.EntityPlayer) human).world.getChunkAtWorldCoords(((net.minecraft.server.v1_8_R3.EntityPlayer) human).getChunkCoordinates());
if (distance(chunk2, chunk1) < distance(human)) {
if (is11400) {
TPacketHandler.sendPacket(player, new PacketPlayOutLightUpdate(((net.minecraft.server.v1_14_R1.Chunk) chunk1).getPos(), ((net.minecraft.server.v1_14_R1.Chunk) chunk1).e()));
} else {
TPacketHandler.sendPacket(player, new net.minecraft.server.v1_14_R1.PacketPlayOutMapChunk((net.minecraft.server.v1_14_R1.Chunk) chunk1, 0x1ffff));
}
}
}
}
public int distance(Object player) {
int viewDistance = Bukkit.getViewDistance();
try {
int playerViewDistance = ((net.minecraft.server.v1_14_R1.EntityPlayer) player).clientViewDistance;
if (playerViewDistance < viewDistance) {
viewDistance = playerViewDistance;
}
} catch (Exception ignored) {
}
return viewDistance;
}
private int distance(Object from, Object to) {
if (!((net.minecraft.server.v1_14_R1.Chunk) from).world.getWorldData().getName().equals(((net.minecraft.server.v1_14_R1.Chunk) to).world.getWorldData().getName())) {
return 100;
}
double var2 = ((net.minecraft.server.v1_14_R1.Chunk) to).getPos().x - ((net.minecraft.server.v1_14_R1.Chunk) from).getPos().x;
double var4 = ((net.minecraft.server.v1_14_R1.Chunk) to).getPos().z - ((net.minecraft.server.v1_14_R1.Chunk) from).getPos().z;
return (int) Math.sqrt(var2 * var2 + var4 * var4);
}
public void sync(Object lightEngine, Consumer<Object> task) {
try {
Object b = SimpleReflection.getFieldValueChecked(LightEngineThreaded.class, lightEngine, "b", true);
Object c = SimpleReflection.getFieldValueChecked(ThreadedMailbox.class, b, "c", true);
int flags;
long wait = -1L;
while (!((AtomicInteger) c).compareAndSet(flags = ((AtomicInteger) c).get() & ~2, flags | 2)) {
if ((flags & 1) != 0) {
if (wait == -1) {
wait = System.currentTimeMillis() + 3 * 1000;
TLogger.getGlobalLogger().info("ThreadedMailbox is closing. Will wait...");
} else if (System.currentTimeMillis() >= wait) {
TLogger.getGlobalLogger().warn("Failed to enter critical section while ThreadedMailbox is closing");
}
try {
Thread.sleep(50);
} catch (InterruptedException ignored) {
}
}
}
try {
task.accept(lightEngine);
} finally {
while (!((AtomicInteger) c).compareAndSet(flags = ((AtomicInteger) c).get(), flags & ~2))
;
SimpleReflection.invokeMethod(ThreadedMailbox.class, b, "f", new Object[0], true);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
private void setRawLightLevelBlock(int level, Object position, Object lightEngineLayer) {
if (level == 0) {
((LightEngineBlock) lightEngineLayer).a((BlockPosition) position);
} else if (((LightEngineLayer) lightEngineLayer).a(SectionPosition.a((net.minecraft.server.v1_14_R1.BlockPosition) position)) != null) {
try {
((LightEngineLayer) lightEngineLayer).a((net.minecraft.server.v1_14_R1.BlockPosition) position, level);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
private void setRawLightLevelSky(int level, Object position, Object lightEngineLayer) {
if (level == 0) {
((LightEngineSky) lightEngineLayer).a((BlockPosition) position);
} else if (((LightEngineLayer) lightEngineLayer).a(SectionPosition.a((net.minecraft.server.v1_14_R1.BlockPosition) position)) != null) {
try {
Object s = SimpleReflection.getFieldValueChecked(LightEngineLayer.class, lightEngineLayer, "c", true);
if (is11500) {
SimpleReflection.invokeMethod(LightEngineStorage.class, s, "d", new Object[0], true);
} else {
SimpleReflection.invokeMethod(LightEngineStorage.class, s, "c", new Object[0], true);
}
SimpleReflection.invokeMethod(LightEngineGraph.class, lightEngineLayer, "a", new Object[] {9223372036854775807L, ((net.minecraft.server.v1_14_R1.BlockPosition) position).asLong(), 15 - level, true}, true);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
} }

View File

@ -0,0 +1,11 @@
package io.izzel.taboolib.module.nms.impl;
/**
* @Author sky
* @Since 2020-04-02 15:50
*/
public enum Type {
SKY, BLOCK, ALL
}

View File

@ -33,7 +33,7 @@ public class AsmClassTransformer extends ClassVisitor implements Opcodes {
ClassReader classReader = new ClassReader(from.getResourceAsStream("/" + from.getName().replace('.', '/') + ".class")); ClassReader classReader = new ClassReader(from.getResourceAsStream("/" + from.getName().replace('.', '/') + ".class"));
newClassName = from.getName() + "_TabooLibRemap_" + this.hashCode() + "_" + toVer; newClassName = from.getName() + "_TabooLibRemap_" + this.hashCode() + "_" + toVer;
prevName = from.getName().replace('.', '/'); prevName = from.getName().replace('.', '/');
classReader.accept(this, ClassReader.SKIP_DEBUG); classReader.accept(this, ClassReader.SKIP_CODE);
Class<?> clazz = AsmClassLoader.createNewClass(newClassName, writer.toByteArray()); Class<?> clazz = AsmClassLoader.createNewClass(newClassName, writer.toByteArray());
Field field = from.getClassLoader().getClass().getDeclaredField("classes"); Field field = from.getClassLoader().getClass().getDeclaredField("classes");
field.setAccessible(true); field.setAccessible(true);