3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-26 16:12:42 +01:00

Replace nms code with fancy library and own code.

Dieser Commit ist enthalten in:
Paulomart 2016-03-03 21:13:49 +01:00
Ursprung 64c2ba243f
Commit 00df80e6fd
5 geänderte Dateien mit 197 neuen und 154 gelöschten Zeilen

39
pom.xml
Datei anzeigen

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>us.myles</groupId> <groupId>us.myles</groupId>
@ -9,6 +8,22 @@
<version>0.4.6</version> <version>0.4.6</version>
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
@ -26,12 +41,12 @@
</resources> </resources>
</build> </build>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spacehq</groupId>
<artifactId>spigot</artifactId> <artifactId>opennbt</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version> <version>1.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId> <artifactId>bukkit</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version> <version>1.8.8-R0.1-SNAPSHOT</version>
@ -61,5 +76,9 @@
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository> </repository>
</repositories> <repository>
<id>spacehq-repo</id>
<url>https://repo.spacehq.org/content/repositories/releases/</url>
</repository>
</repositories>
</project> </project>

Datei anzeigen

@ -1,33 +1,23 @@
package us.myles.ViaVersion.api.slot; package us.myles.ViaVersion.api.slot;
import java.lang.reflect.Constructor; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Material; import org.bukkit.Material;
import org.spacehq.opennbt.tag.builtin.CompoundTag;
import net.minecraft.server.v1_8_R3.EntityTypes; import org.spacehq.opennbt.tag.builtin.StringTag;
import net.minecraft.server.v1_8_R3.Item;
import net.minecraft.server.v1_8_R3.ItemStack;
import net.minecraft.server.v1_8_R3.MobEffect;
import net.minecraft.server.v1_8_R3.MobEffectList;
import net.minecraft.server.v1_8_R3.NBTTagCompound;
import net.minecraft.server.v1_8_R3.PotionBrewer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.CancelException; import us.myles.ViaVersion.CancelException;
import us.myles.ViaVersion.util.ReflectionUtil; import us.myles.ViaVersion.util.PacketUtil;
public class ItemSlotRewriter { public class ItemSlotRewriter {
public static void rewrite1_9To1_8(ByteBuf input, ByteBuf output) throws CancelException { public static void rewrite1_9To1_8(ByteBuf input, ByteBuf output) throws CancelException {
try { try {
Object item = readItemStack(input); ItemStack item = readItemStack(input);
fixIdsFrom1_9To1_8(item); fixIdsFrom1_9To1_8(item);
writeItemStack(item, output); writeItemStack(item, output);
} catch (Exception e) { } catch (Exception e) {
@ -39,7 +29,7 @@ public class ItemSlotRewriter {
public static void rewrite1_8To1_9(ByteBuf input, ByteBuf output) throws CancelException { public static void rewrite1_8To1_9(ByteBuf input, ByteBuf output) throws CancelException {
try { try {
Object item = readItemStack(input); ItemStack item = readItemStack(input);
fixIdsFrom1_8To1_9(item); fixIdsFrom1_8To1_9(item);
writeItemStack(item, output); writeItemStack(item, output);
} catch (Exception e) { } catch (Exception e) {
@ -49,120 +39,153 @@ public class ItemSlotRewriter {
} }
} }
public static void fixIdsFrom1_9To1_8(Object itemstack) throws NoSuchFieldException, IllegalAccessException { public static void fixIdsFrom1_9To1_8(ItemStack item) {
if (itemstack != null) { if (item != null) {
ItemStack stack = (ItemStack) itemstack; if (item.id == Material.MONSTER_EGG.getId() && item.data == 0) {
int itemId = Item.getId(stack.getItem()); CompoundTag tag = item.tag;
if (itemId == Material.MONSTER_EGG.getId() && stack.getData() == 0) {
NBTTagCompound tag = stack.getTag();
int data = 0; int data = 0;
if (tag != null && tag.hasKeyOfType("EntityTag", 10)) { if (tag != null && tag.get("EntityTag") instanceof CompoundTag) {
NBTTagCompound entityTag = tag.getCompound("EntityTag"); CompoundTag entityTag = tag.get("EntityTag");
if (entityTag.hasKeyOfType("id", 8)) { if (entityTag.get("id") instanceof StringTag) {
String id = entityTag.getString("id"); StringTag id = entityTag.get("id");
Map<String, Integer> g = (Map<String, Integer>) ReflectionUtil.getStatic(EntityTypes.class, "g", Map.class); data = ENTTIY_NAME_TO_ID.get(id.getValue());
data = g.get(id);
} }
} }
stack.setTag(null); item.tag = null;
stack.setData(data); item.data = (short) data;
} else if (itemId == Material.POTION.getId() && stack.getData() == 0) {
NBTTagCompound tag = stack.getTag();
if (tag != null) {
System.out.println("in: " + tag);
}
} }
} }
} }
public static void fixIdsFrom1_8To1_9(Object itemstack) { public static void fixIdsFrom1_8To1_9(ItemStack item) {
if (itemstack != null) { if (item != null) {
ItemStack stack = (ItemStack) itemstack; if (item.id == Material.MONSTER_EGG.getId() && item.data != 0) {
int itemId = Item.getId(stack.getItem()); CompoundTag tag = item.tag;
if (itemId == Material.MONSTER_EGG.getId() && stack.getData() != 0) {
NBTTagCompound tag = stack.getTag();
if (tag == null) { if (tag == null) {
tag = new NBTTagCompound(); tag = new CompoundTag("tag");
}
NBTTagCompound entityTag = new NBTTagCompound();
entityTag.setString("id", EntityTypes.b(stack.getData()));
tag.set("EntityTag", entityTag);
stack.setTag(tag);
stack.setData(0);
} else if (itemId == Material.POTION.getId() && stack.getData() != 0) {
NBTTagCompound tag = stack.getTag();
if (tag == null) {
tag = new NBTTagCompound();
stack.setTag(tag);
}
try {
List<MobEffect> effects = PotionBrewer.getEffects(stack.getData(), true);
if (effects != null && effects.size() >= 1) {
MobEffect effect = effects.get(0);
MobEffectList type = MobEffectList.byId[effect.getEffectId()];
StringBuilder name = new StringBuilder();
System.out.println(effect.getDuration() + " ?>? " +type.k());
if (effect.getAmplifier() > 0) {
name.append("strong_");
} else if (effect.getDuration() > type.k()) {
name.append("long_");
}
name.append(POTION_TYPE_TO_KEY.get(effect.getEffectId()));
System.out.println("Rewriting to: " + name.toString());
tag.setString("Potion", name.toString());
} else {
System.out.println("Falling back to water for subId: " + stack.getData());
tag.setString("Potion", "water");
}
} catch (Exception e) {
e.printStackTrace();
} }
CompoundTag entityTag = new CompoundTag("EntityTag");
StringTag id = new StringTag("id", ENTTIY_ID_TO_NAME.get(item.data));
entityTag.put(id);
tag.put(entityTag);
item.tag = tag;
item.data = 0;
} }
} }
} }
public static Object readItemStack(ByteBuf input) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public static ItemStack readItemStack(ByteBuf input) throws IOException {
Object serializer = getPacketDataSerializer(input); short id = input.readShort();
return READ_ITEM.invoke(serializer); if (id < 0) {
return null;
} else {
ItemStack item = new ItemStack();
item.id = id;
item.amount = input.readByte();
item.data = input.readShort();
item.tag = PacketUtil.readNBT(input);
return item;
}
} }
public static void writeItemStack(Object itemstack, ByteBuf output) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public static void writeItemStack(ItemStack item, ByteBuf output) throws IOException {
Object serializer = getPacketDataSerializer(output); if (item == null) {
WRITE_ITEM.invoke(serializer, itemstack); output.writeShort(-1);
} else {
output.writeShort(item.id);
output.writeByte(item.amount);
output.writeShort(item.data);
PacketUtil.writeNBT(output, item.tag);
}
} }
private static final Map<Integer, String> POTION_TYPE_TO_KEY = new HashMap<>(); public static class ItemStack {
private static Constructor<?> SERIALIZER_CONSTRUCTOR;
private static Method WRITE_ITEM; private short id;
private static Method READ_ITEM; private byte amount;
private short data;
private CompoundTag tag;
public static ItemStack fromBukkit(org.bukkit.inventory.ItemStack stack) {
ItemStack item = new ItemStack();
item.id = (short) stack.getTypeId();
item.amount = (byte) stack.getAmount();
item.data = stack.getData().getData();
// TODO: nbt
return item;
}
}
private static Map<String, Integer> ENTTIY_NAME_TO_ID = new HashMap<>();
private static Map<Integer, String> ENTTIY_ID_TO_NAME = new HashMap<>();
static { static {
try { register(1, "Item");
Class<?> list = ReflectionUtil.nms("MobEffectList"); register(2, "XPOrb");
Map<Object, Object> map = ReflectionUtil.getStatic(list, "I", Map.class); register(7, "ThrownEgg");
for (Entry<Object, Object> e : map.entrySet()) { register(8, "LeashKnot");
System.out.println(e.getValue()); register(9, "Painting");
System.out.println(e.getValue().getClass()); register(10, "Arrow");
int id = ReflectionUtil.get(e.getValue(), list, "id", int.class); register(11, "Snowball");
String type = ReflectionUtil.get(e.getKey(), "b", String.class); register(12, "Fireball");
POTION_TYPE_TO_KEY.put(id, type); register(13, "SmallFireball");
} register(14, "ThrownEnderpearl");
} catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException e1) { register(15, "EyeOfEnderSignal");
e1.printStackTrace(); register(16, "ThrownPotion");
} register(17, "ThrownExpBottle");
try { register(18, "ItemFrame");
Class<?> serializer = ReflectionUtil.nms("PacketDataSerializer"); register(19, "WitherSkull");
Class<?> itemStack = ReflectionUtil.nms("ItemStack"); register(20, "PrimedTnt");
SERIALIZER_CONSTRUCTOR = serializer.getDeclaredConstructor(ByteBuf.class); register(21, "FallingSand");
WRITE_ITEM = serializer.getDeclaredMethod("a", itemStack); register(22, "FireworksRocketEntity");
READ_ITEM = serializer.getDeclaredMethod("i"); register(30, "ArmorStand");
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) { register(40, "MinecartCommandBlock");
e.printStackTrace(); register(41, "Boat");
} register(42, "MinecartRideable");
register(43, "MinecartChest");
register(44, "MinecartFurnace");
register(45, "MinecartTNT");
register(46, "MinecartHopper");
register(47, "MinecartSpawner");
register(48, "Mob");
register(49, "Monster");
register(50, "Creeper");
register(51, "Skeleton");
register(52, "Spider");
register(53, "Giant");
register(54, "Zombie");
register(55, "Slime");
register(56, "Ghast");
register(57, "PigZombie");
register(58, "Enderman");
register(59, "CaveSpider");
register(60, "Silverfish");
register(61, "Blaze");
register(62, "LavaSlime");
register(63, "EnderDragon");
register(64, "WitherBoss");
register(65, "Bat");
register(66, "Witch");
register(67, "Endermite");
register(68, "Guardian");
register(90, "Pig");
register(91, "Sheep");
register(92, "Cow");
register(93, "Chicken");
register(94, "Squid");
register(95, "Wolf");
register(96, "MushroomCow");
register(97, "SnowMan");
register(98, "Ozelot");
register(99, "VillagerGolem");
register(100, "EntityHorse");
register(101, "Rabbit");
register(120, "Villager");
register(200, "EnderCrystal");
} }
private static Object getPacketDataSerializer(ByteBuf buf) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { private static void register(int id, String name) {
return SERIALIZER_CONSTRUCTOR.newInstance(buf); ENTTIY_ID_TO_NAME.put(id, name);
ENTTIY_NAME_TO_ID.put(name, id);
} }
} }

Datei anzeigen

@ -12,6 +12,7 @@ import org.bukkit.util.Vector;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.slot.ItemSlotRewriter; import us.myles.ViaVersion.api.slot.ItemSlotRewriter;
import us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack;
import us.myles.ViaVersion.util.PacketUtil; import us.myles.ViaVersion.util.PacketUtil;
public class MetadataRewriter { public class MetadataRewriter {
@ -93,8 +94,9 @@ public class MetadataRewriter {
output.writeBoolean(((Byte) value).byteValue() != 0); output.writeBoolean(((Byte) value).byteValue() != 0);
break; break;
case Slot: case Slot:
ItemSlotRewriter.fixIdsFrom1_8To1_9(value); ItemStack item = (ItemStack) value;
ItemSlotRewriter.writeItemStack(value, output); ItemSlotRewriter.fixIdsFrom1_8To1_9(item);
ItemSlotRewriter.writeItemStack(item, output);
break; break;
case Position: case Position:
Vector vector = (Vector) value; Vector vector = (Vector) value;

Datei anzeigen

@ -175,25 +175,11 @@ public class IncomingTransformer {
int hand = PacketUtil.readVarInt(input); int hand = PacketUtil.readVarInt(input);
ItemStack inHand = ViaVersionPlugin.getHandItem(info); ItemStack inHand = ViaVersionPlugin.getHandItem(info);
Object item = null;
try { try {
Method m = ReflectionUtil.obc("inventory.CraftItemStack").getDeclaredMethod("asNMSCopy", ItemStack.class); us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack item = us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack.fromBukkit(inHand);
item = m.invoke(null, inHand);
ItemSlotRewriter.fixIdsFrom1_9To1_8(item); ItemSlotRewriter.fixIdsFrom1_9To1_8(item);
ItemSlotRewriter.writeItemStack(item, output); ItemSlotRewriter.writeItemStack(item, output);
} catch (NoSuchMethodException e) { } catch (Exception e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -213,25 +199,11 @@ public class IncomingTransformer {
output.writeByte(255); output.writeByte(255);
// write item in hand // write item in hand
ItemStack inHand = ViaVersionPlugin.getHandItem(info); ItemStack inHand = ViaVersionPlugin.getHandItem(info);
Object item = null;
try { try {
Method m = ReflectionUtil.obc("inventory.CraftItemStack").getDeclaredMethod("asNMSCopy", ItemStack.class); us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack item = us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack.fromBukkit(inHand);
item = m.invoke(null, inHand);
ItemSlotRewriter.fixIdsFrom1_9To1_8(item); ItemSlotRewriter.fixIdsFrom1_9To1_8(item);
ItemSlotRewriter.writeItemStack(item, output); ItemSlotRewriter.writeItemStack(item, output);
} catch (NoSuchMethodException e) { } catch (Exception e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace(); e.printStackTrace();
} }

Datei anzeigen

@ -3,6 +3,8 @@ package us.myles.ViaVersion.util;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
@ -11,6 +13,9 @@ import us.myles.ViaVersion.chunks.PacketChunk;
import us.myles.ViaVersion.chunks.PacketChunkData; import us.myles.ViaVersion.chunks.PacketChunkData;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -21,6 +26,9 @@ import java.util.BitSet;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.spacehq.opennbt.NBTIO;
import org.spacehq.opennbt.tag.builtin.CompoundTag;
public class PacketUtil { public class PacketUtil {
private static Method DECODE_METHOD; private static Method DECODE_METHOD;
private static Method ENCODE_METHOD; private static Method ENCODE_METHOD;
@ -42,6 +50,25 @@ public class PacketUtil {
} }
} }
public static CompoundTag readNBT(ByteBuf input) throws IOException {
int readerIndex = input.readerIndex();
byte b = input.readByte();
if (b == 0) {
return null;
} else {
input.readerIndex(readerIndex);
return (CompoundTag) NBTIO.readTag(new DataInputStream(new ByteBufInputStream(input)));
}
}
public static void writeNBT(ByteBuf output, CompoundTag tag) throws IOException {
if (tag == null) {
output.writeByte(0);
} else {
NBTIO.writeTag(new DataOutputStream(new ByteBufOutputStream(output)), tag);
}
}
public static List<Object> callDecode(ByteToMessageDecoder decoder, ChannelHandlerContext ctx, Object input) { public static List<Object> callDecode(ByteToMessageDecoder decoder, ChannelHandlerContext ctx, Object input) {
List<Object> output = new ArrayList<Object>(); List<Object> output = new ArrayList<Object>();
try { try {