Use Myles new Magic transformer for the ArmorListener (#429)

* Use Myles new Magic transformer for the ArmorListener

* Use Myles new Magic transformer for the CommandBlockListener
This commit is contained in:
Mats 2016-06-09 11:49:31 +02:00 committed by Myles
parent d6edec3a55
commit f1b81f77d6
2 changed files with 38 additions and 36 deletions

View File

@ -1,7 +1,5 @@
package us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
@ -17,9 +15,9 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.CraftingInventory;
import us.myles.ViaVersion.ViaVersionPlugin;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.ArmorType;
@ -40,24 +38,21 @@ public class ArmorListener implements Listener {
if (!userConnection.get(ProtocolInfo.class).getPipeline().contains(Protocol1_9TO1_8.class)) return;
int armor = ArmorType.calculateArmorPoints(player.getInventory().getArmorContents());
int protocol = userConnection.get(ProtocolInfo.class).getProtocolVersion();
ByteBuf buf = Unpooled.buffer();
PacketWrapper wrapper = new PacketWrapper(0x4B, null, userConnection);
try {
wrapper.write(Type.VAR_INT, player.getEntityId()); // Player ID
wrapper.write(Type.INT, 1); // only 1 property
wrapper.write(Type.STRING, "generic.armor");
wrapper.write(Type.DOUBLE, 0D); //default 0 armor
wrapper.write(Type.VAR_INT, 1); // 1 modifier
wrapper.write(Type.UUID, ARMOR_ATTRIBUTE); // armor modifier uuid
wrapper.write(Type.DOUBLE, (double) armor); // the modifier value
wrapper.write(Type.BYTE, (byte) 0);// the modifier operation, 0 is add number
//TODO possibility to send packets by Protocol version, to let the transformer do the work
Type.VAR_INT.write(buf, (protocol >= ProtocolVersion.v1_9_3.getId()) ? 0x4A : 0x4B); // Entity Properties
Type.VAR_INT.write(buf, player.getEntityId());
buf.writeInt(1); // only 1 property
Type.STRING.write(buf, "generic.armor");
buf.writeDouble(0); //default 0 armor
Type.VAR_INT.write(buf, 1); // 1 modifier
Type.UUID.write(buf, ARMOR_ATTRIBUTE); // armor modifier uuid
buf.writeDouble((double) armor); // the modifier value
buf.writeByte(0); // the modifier operation, 0 is add number
ViaVersion.getInstance().sendRawPacket(player, buf);
} catch (Exception ignored) {
buf.release();
wrapper.send(Protocol1_9TO1_8.class);
} catch (Exception e) {
e.printStackTrace();
}
}

View File

@ -19,6 +19,7 @@ import org.bukkit.event.player.PlayerRespawnEvent;
import org.spacehq.opennbt.tag.builtin.ByteTag;
import org.spacehq.opennbt.tag.builtin.CompoundTag;
import us.myles.ViaVersion.ViaVersionPlugin;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.minecraft.Position;
@ -80,12 +81,12 @@ public class CommandBlockListener implements Listener {
if (!userConnection.get(ProtocolInfo.class).getPipeline().contains(Protocol1_9TO1_8.class)) return;
try {
ByteBuf buf = Unpooled.buffer();
Type.VAR_INT.write(buf, 0x1B); // Entity Status
buf.writeInt(p.getEntityId());
buf.writeByte(26);
plugin.sendRawPacket(p, buf);
} catch (Exception ignored) {
PacketWrapper wrapper = new PacketWrapper(0x1B, null, userConnection); // Entity status
wrapper.write(Type.INT, p.getEntityId());
wrapper.write(Type.BYTE, (byte) 26); //Hardcoded op permission level
wrapper.send(Protocol1_9TO1_8.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@ -97,26 +98,32 @@ public class CommandBlockListener implements Listener {
Object tileEntityCommand = ReflectionUtil.get(cmd, "commandBlock", ReflectionUtil.nms("TileEntityCommand"));
Object updatePacket = ReflectionUtil.invoke(tileEntityCommand, "getUpdatePacket");
ByteBuf buf = packetToByteBuf(updatePacket);
plugin.sendRawPacket(player, buf);
UserConnection userConnection = ((ViaVersionPlugin) ViaVersion.getInstance()).getConnection(player);
PacketWrapper wrapper = generatePacket(updatePacket, userConnection);
wrapper.send(Protocol1_9TO1_8.class);
}
private ByteBuf packetToByteBuf(Object updatePacket) throws Exception {
ByteBuf buf = Unpooled.buffer();
Type.VAR_INT.write(buf, 0x09); //Block Entity Packet ID
private PacketWrapper generatePacket(Object updatePacket, UserConnection usr) throws Exception {
PacketWrapper wrapper = new PacketWrapper(0x09, null, usr); // Update block entity
long[] pos = getPosition(ReflectionUtil.get(updatePacket, "a", ReflectionUtil.nms("BlockPosition")));
Type.POSITION.write(buf, new Position(pos[0], pos[1], pos[2])); //Block position
buf.writeByte(2); //Action id always 2
wrapper.write(Type.POSITION, new Position(pos[0], pos[1], pos[2])); //Block position
wrapper.write(Type.BYTE, (byte) 2); // Action id always 2
CompoundTag nbt = getNBT(ReflectionUtil.get(updatePacket, "c", ReflectionUtil.nms("NBTTagCompound")));
if (nbt == null) {
buf.writeByte(0); //If nbt is null. Use 0 as nbt
return buf;
wrapper.write(Type.BYTE, (byte) 0); //If nbt is null. Use 0 as nbt
return wrapper;
}
nbt.put(new ByteTag("powered", (byte) 0));
nbt.put(new ByteTag("auto", (byte) 0));
nbt.put(new ByteTag("conditionMet", (byte) 0));
Type.NBT.write(buf, nbt); //NBT tag
return buf;
wrapper.write(Type.NBT, nbt); // NBT TAG
return wrapper;
}
private long[] getPosition(Object obj) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {