Read desc

Moved packet constructing out of Disguise class.
Fixed datawatcher error.
Fixed datawatcher screwup where I was setting the wrong values
This commit is contained in:
Andrew 2013-07-29 11:21:25 +12:00
parent 53b5be001d
commit 57ebeacdd6
2 changed files with 208 additions and 179 deletions

View File

@ -4,28 +4,18 @@ import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseTypes.Watchers.AgeableWatcher;
import me.libraryaddict.disguise.DisguiseTypes.Watchers.ZombieWatcher;
import net.minecraft.server.v1_6_R2.Entity;
import net.minecraft.server.v1_6_R2.EntityAgeable;
import net.minecraft.server.v1_6_R2.EntityInsentient;
import net.minecraft.server.v1_6_R2.EntityLiving;
import net.minecraft.server.v1_6_R2.EntityPlayer;
import net.minecraft.server.v1_6_R2.EntityTrackerEntry;
import net.minecraft.server.v1_6_R2.ItemStack;
import net.minecraft.server.v1_6_R2.MathHelper;
import net.minecraft.server.v1_6_R2.EnumArt;
import net.minecraft.server.v1_6_R2.WorldServer;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftItemStack;
import org.bukkit.entity.Horse.Variant;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
@ -33,11 +23,11 @@ import org.bukkit.util.Vector;
import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
public class Disguise {
private static JavaPlugin plugin;
private DisguiseType disguiseType;
private org.bukkit.entity.Entity entity;
private boolean replaceSounds;
@ -54,165 +44,7 @@ public class Disguise {
return disguise;
}
public PacketContainer[] constructPacket(org.bukkit.entity.Entity disguisedEntity) {
PacketContainer[] spawnPackets = new PacketContainer[2];
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
Entity nmsEntity = ((CraftEntity) disguisedEntity).getHandle();
Location loc = disguisedEntity.getLocation();
if (getType() == DisguiseType.EXPERIENCE_ORB) {
spawnPackets[0] = manager.createPacket(Packets.Server.ADD_EXP_ORB);
StructureModifier<Object> mods = spawnPackets[0].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, (int) Math.floor(loc.getX() * 32));
mods.write(2, (int) Math.floor(loc.getY() * 32) + 2);
mods.write(3, (int) Math.floor(loc.getZ() * 32));
mods.write(4, 1);
} else if (getType() == DisguiseType.PAINTING) {
spawnPackets[0] = manager.createPacket(Packets.Server.ENTITY_PAINTING);
StructureModifier<Object> mods = spawnPackets[0].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, loc.getBlockX());
mods.write(2, loc.getBlockY());
mods.write(3, loc.getBlockZ());
mods.write(4, ((int) loc.getYaw()) % 4);
int id = ((MiscDisguise) this).getId();
if (id == -1)
id = new Random().nextInt(EnumArt.values().length);
mods.write(5, EnumArt.values()[id].B);
// Make the teleport packet to make it visible..
spawnPackets[1] = manager.createPacket(Packets.Server.ENTITY_TELEPORT);
mods = spawnPackets[1].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, (int) Math.floor(loc.getX() * 32D));
mods.write(2, (int) Math.floor(loc.getY() * 32D));
mods.write(3, (int) Math.floor(loc.getZ() * 32D));
mods.write(4, (byte) (int) (loc.getYaw() * 256.0F / 360.0F));
mods.write(5, (byte) (int) (loc.getPitch() * 256.0F / 360.0F));
// Need to fake a teleport packet as well to make the painting visible as a moving.
} else if (getType().isPlayer()) {
spawnPackets[0] = manager.createPacket(Packets.Server.NAMED_ENTITY_SPAWN);
StructureModifier<Object> mods = spawnPackets[0].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, ((PlayerDisguise) this).getName());
mods.write(2, (int) Math.floor(loc.getX() * 32));
mods.write(3, (int) Math.floor(loc.getY() * 32));
mods.write(4, (int) Math.floor(loc.getZ() * 32));
mods.write(5, (byte) (int) (loc.getYaw() * 256F / 360F));
mods.write(6, (byte) (int) (loc.getPitch() * 256F / 360F));
ItemStack item = null;
if (disguisedEntity instanceof Player && ((Player) disguisedEntity).getItemInHand() != null) {
item = CraftItemStack.asNMSCopy(((Player) disguisedEntity).getItemInHand());
} else if (disguisedEntity instanceof LivingEntity) {
item = CraftItemStack.asNMSCopy(((CraftLivingEntity) disguisedEntity).getEquipment().getItemInHand());
}
mods.write(7, (item == null ? 0 : item.id));
mods.write(8, nmsEntity.getDataWatcher());
} else if (getType().isMob()) {
Vector vec = disguisedEntity.getVelocity();
spawnPackets[0] = manager.createPacket(Packets.Server.MOB_SPAWN);
StructureModifier<Object> mods = spawnPackets[0].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, (int) getType().getEntityType().getTypeId());
double d1 = 3.9D;
double d2 = vec.getX();
double d3 = vec.getY();
double d4 = vec.getZ();
if (d2 < -d1)
d2 = -d1;
if (d3 < -d1)
d3 = -d1;
if (d4 < -d1)
d4 = -d1;
if (d2 > d1)
d2 = d1;
if (d3 > d1)
d3 = d1;
if (d4 > d1)
d4 = d1;
mods.write(2, nmsEntity.at.a(loc.getX()));
mods.write(3, (int) Math.floor(loc.getY() * 32D));
mods.write(4, nmsEntity.at.a(loc.getZ()));
mods.write(5, (int) (d2 * 8000.0D));
mods.write(6, (int) (d3 * 8000.0D));
mods.write(7, (int) (d4 * 8000.0D));
byte yawValue = (byte) (int) (loc.getYaw() * 256.0F / 360.0F);
if (getType() == DisguiseType.ENDER_DRAGON)
yawValue -= 128;
mods.write(8, yawValue);
mods.write(9, (byte) (int) (loc.getPitch() * 256.0F / 360.0F));
if (nmsEntity instanceof EntityLiving)
mods.write(10, (byte) (int) (((EntityLiving) nmsEntity).aA * 256.0F / 360.0F));
mods.write(11, nmsEntity.getDataWatcher());
// Theres a list sometimes written with this. But no problems have appeared!
// Probably just the metadata to be sent. But the next meta packet after fixes that anyways.
} else if (getType().isMisc()) {
int id = getType().getEntityId();
int data = 0;
if (((MiscDisguise) this).getId() >= 0)
if (((MiscDisguise) this).getData() >= 0)
data = (((MiscDisguise) this).getId() | ((MiscDisguise) this).getData() << 16);
else
data = ((MiscDisguise) this).getId();
// This won't actually work. But you can still do it when constructing a disguise
if (getType() == DisguiseType.FISHING_HOOK)
data = getEntity().getEntityId();
else if (getType() == DisguiseType.ITEM_FRAME)
data = (int) Math.abs(loc.getYaw() % 4);
spawnPackets[0] = manager.createPacket(Packets.Server.VEHICLE_SPAWN);
StructureModifier<Object> mods = spawnPackets[0].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, (int) Math.floor(loc.getX() * 32D));
mods.write(2, (int) Math.floor(loc.getY() * 32D));
mods.write(3, (int) Math.floor(loc.getZ() * 32D));
if (data > 0) {
Vector vec = disguisedEntity.getVelocity();
double d1 = vec.getX();
double d2 = vec.getY();
double d3 = vec.getZ();
double d4 = 3.9D;
if (d1 < -d4)
d1 = -d4;
if (d2 < -d4)
d2 = -d4;
if (d3 < -d4)
d3 = -d4;
if (d1 > d4)
d1 = d4;
if (d2 > d4)
d2 = d4;
if (d3 > d4)
d3 = d4;
mods.write(4, (int) (d1 * 8000.0D));
mods.write(5, (int) (d2 * 8000.0D));
mods.write(6, (int) (d3 * 8000.0D));
}
mods.write(7, (int) MathHelper.floor(loc.getPitch() * 256.0F / 360.0F));
mods.write(8, (int) MathHelper.floor(loc.getYaw() * 256.0F / 360.0F) - 64);
mods.write(9, id);
mods.write(10, data);
}
if (spawnPackets[1] == null) {
// Make a packet to turn his head!
spawnPackets[1] = manager.createPacket(Packets.Server.ENTITY_HEAD_ROTATION);
StructureModifier<Object> mods = spawnPackets[1].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, (byte) (int) Math.floor(loc.getYaw() * 256.0F / 360.0F));
}
return spawnPackets;
}
public void constructWatcher(JavaPlugin plugin, final org.bukkit.entity.Entity entity) {
public void constructWatcher(final org.bukkit.entity.Entity entity) {
if (this.entity != null)
throw new RuntimeException("This disguise is already in use! Try .clone()");
this.entity = entity;
@ -468,22 +300,22 @@ public class Disguise {
return watcher;
}
public boolean replaceSounds() {
return replaceSounds;
public boolean isMiscDisguise() {
return this instanceof MiscDisguise;
}
public boolean isMobDisguise() {
return this instanceof MobDisguise;
}
public boolean isMiscDisguise() {
return this instanceof MiscDisguise;
}
public boolean isPlayerDisguise() {
return this instanceof PlayerDisguise;
}
public boolean replaceSounds() {
return replaceSounds;
}
public void setReplaceSounds(boolean areSoundsReplaced) {
replaceSounds = areSoundsReplaced;
}

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@ -7,11 +8,14 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import me.libraryaddict.disguise.Commands.*;
import me.libraryaddict.disguise.DisguiseTypes.Disguise;
import me.libraryaddict.disguise.DisguiseTypes.DisguiseSound;
import me.libraryaddict.disguise.DisguiseTypes.DisguiseType;
import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher;
import me.libraryaddict.disguise.DisguiseTypes.MiscDisguise;
import me.libraryaddict.disguise.DisguiseTypes.PlayerDisguise;
import me.libraryaddict.disguise.DisguiseTypes.Values;
import me.libraryaddict.disguise.DisguiseTypes.Watchers.AgeableWatcher;
@ -19,14 +23,22 @@ import me.libraryaddict.disguise.DisguiseTypes.Watchers.LivingWatcher;
import net.minecraft.server.v1_6_R2.AttributeSnapshot;
import net.minecraft.server.v1_6_R2.ChatMessage;
import net.minecraft.server.v1_6_R2.ChunkCoordinates;
import net.minecraft.server.v1_6_R2.DataWatcher;
import net.minecraft.server.v1_6_R2.EntityHuman;
import net.minecraft.server.v1_6_R2.EntityLiving;
import net.minecraft.server.v1_6_R2.EnumArt;
import net.minecraft.server.v1_6_R2.GenericAttributes;
import net.minecraft.server.v1_6_R2.ItemStack;
import net.minecraft.server.v1_6_R2.MathHelper;
import net.minecraft.server.v1_6_R2.WatchableObject;
import net.minecraft.server.v1_6_R2.World;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_6_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftItemStack;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
@ -35,6 +47,8 @@ import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
@ -112,7 +126,7 @@ public class LibsDisguises extends JavaPlugin {
StructureModifier<Object> mods = event.getPacket().getModifier();
String name = (String) mods.read(1);
if (!name.equals(((PlayerDisguise) disguise).getName())) {
final PacketContainer[] packets = disguise.constructPacket(entity);
final PacketContainer[] packets = constructPacket(disguise, entity);
event.setPacket(packets[0]);
if (packets.length > 1) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@ -127,7 +141,7 @@ public class LibsDisguises extends JavaPlugin {
}
}
} else {
final PacketContainer[] packets = disguise.constructPacket(entity);
final PacketContainer[] packets = constructPacket(disguise, entity);
event.setPacket(packets[0]);
if (packets.length > 1) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@ -145,7 +159,7 @@ public class LibsDisguises extends JavaPlugin {
|| event.getPacketID() == Packets.Server.ADD_EXP_ORB
|| event.getPacketID() == Packets.Server.VEHICLE_SPAWN
|| event.getPacketID() == Packets.Server.ENTITY_PAINTING) {
final PacketContainer[] packets = disguise.constructPacket(entity);
final PacketContainer[] packets = constructPacket(disguise, entity);
event.setPacket(packets[0]);
if (packets.length > 1) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@ -206,6 +220,180 @@ public class LibsDisguises extends JavaPlugin {
});
}
protected PacketContainer[] constructPacket(Disguise disguise, Entity disguisedEntity) {
PacketContainer[] spawnPackets = new PacketContainer[2];
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
net.minecraft.server.v1_6_R2.Entity nmsEntity = ((CraftEntity) disguisedEntity).getHandle();
Location loc = disguisedEntity.getLocation();
if (disguise.getType() == DisguiseType.EXPERIENCE_ORB) {
spawnPackets[0] = manager.createPacket(Packets.Server.ADD_EXP_ORB);
StructureModifier<Object> mods = spawnPackets[0].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, (int) Math.floor(loc.getX() * 32));
mods.write(2, (int) Math.floor(loc.getY() * 32) + 2);
mods.write(3, (int) Math.floor(loc.getZ() * 32));
mods.write(4, 1);
} else if (disguise.getType() == DisguiseType.PAINTING) {
spawnPackets[0] = manager.createPacket(Packets.Server.ENTITY_PAINTING);
StructureModifier<Object> mods = spawnPackets[0].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, loc.getBlockX());
mods.write(2, loc.getBlockY());
mods.write(3, loc.getBlockZ());
mods.write(4, ((int) loc.getYaw()) % 4);
int id = ((MiscDisguise) disguise).getId();
if (id == -1)
id = new Random().nextInt(EnumArt.values().length);
mods.write(5, EnumArt.values()[id].B);
// Make the teleport packet to make it visible..
spawnPackets[1] = manager.createPacket(Packets.Server.ENTITY_TELEPORT);
mods = spawnPackets[1].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, (int) Math.floor(loc.getX() * 32D));
mods.write(2, (int) Math.floor(loc.getY() * 32D));
mods.write(3, (int) Math.floor(loc.getZ() * 32D));
mods.write(4, (byte) (int) (loc.getYaw() * 256.0F / 360.0F));
mods.write(5, (byte) (int) (loc.getPitch() * 256.0F / 360.0F));
} else if (disguise.getType().isPlayer()) {
spawnPackets[0] = manager.createPacket(Packets.Server.NAMED_ENTITY_SPAWN);
StructureModifier<Object> mods = spawnPackets[0].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, ((PlayerDisguise) disguise).getName());
mods.write(2, (int) Math.floor(loc.getX() * 32));
mods.write(3, (int) Math.floor(loc.getY() * 32));
mods.write(4, (int) Math.floor(loc.getZ() * 32));
mods.write(5, (byte) (int) (loc.getYaw() * 256F / 360F));
mods.write(6, (byte) (int) (loc.getPitch() * 256F / 360F));
ItemStack item = null;
if (disguisedEntity instanceof Player && ((Player) disguisedEntity).getItemInHand() != null) {
item = CraftItemStack.asNMSCopy(((Player) disguisedEntity).getItemInHand());
} else if (disguisedEntity instanceof LivingEntity) {
item = CraftItemStack.asNMSCopy(((CraftLivingEntity) disguisedEntity).getEquipment().getItemInHand());
}
mods.write(7, (item == null ? 0 : item.id));
mods.write(8, convertDataWatcher(nmsEntity.getDataWatcher(), disguise.getWatcher()));
} else if (disguise.getType().isMob()) {
Vector vec = disguisedEntity.getVelocity();
spawnPackets[0] = manager.createPacket(Packets.Server.MOB_SPAWN);
StructureModifier<Object> mods = spawnPackets[0].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, (int) disguise.getType().getEntityType().getTypeId());
double d1 = 3.9D;
double d2 = vec.getX();
double d3 = vec.getY();
double d4 = vec.getZ();
if (d2 < -d1)
d2 = -d1;
if (d3 < -d1)
d3 = -d1;
if (d4 < -d1)
d4 = -d1;
if (d2 > d1)
d2 = d1;
if (d3 > d1)
d3 = d1;
if (d4 > d1)
d4 = d1;
mods.write(2, nmsEntity.at.a(loc.getX()));
mods.write(3, (int) Math.floor(loc.getY() * 32D));
mods.write(4, nmsEntity.at.a(loc.getZ()));
mods.write(5, (int) (d2 * 8000.0D));
mods.write(6, (int) (d3 * 8000.0D));
mods.write(7, (int) (d4 * 8000.0D));
byte yawValue = (byte) (int) (loc.getYaw() * 256.0F / 360.0F);
if (disguise.getType() == DisguiseType.ENDER_DRAGON)
yawValue -= 128;
mods.write(8, yawValue);
mods.write(9, (byte) (int) (loc.getPitch() * 256.0F / 360.0F));
if (nmsEntity instanceof EntityLiving)
mods.write(10, (byte) (int) (((EntityLiving) nmsEntity).aA * 256.0F / 360.0F));
mods.write(11, convertDataWatcher(nmsEntity.getDataWatcher(), disguise.getWatcher()));
// Theres a list sometimes written with this. But no problems have appeared!
// Probably just the metadata to be sent. But the next meta packet after fixes that anyways.
} else if (disguise.getType().isMisc()) {
int id = disguise.getType().getEntityId();
int data = 0;
if (((MiscDisguise) disguise).getId() >= 0)
if (((MiscDisguise) disguise).getData() >= 0)
data = (((MiscDisguise) disguise).getId() | ((MiscDisguise) disguise).getData() << 16);
else
data = ((MiscDisguise) disguise).getId();
// This won't actually work.
// But if someone constructing the disguise uses it properly. It will work.
if (disguise.getType() == DisguiseType.FISHING_HOOK)
data = disguise.getEntity().getEntityId();
else if (disguise.getType() == DisguiseType.ITEM_FRAME)
data = (int) Math.abs(loc.getYaw() % 4);
spawnPackets[0] = manager.createPacket(Packets.Server.VEHICLE_SPAWN);
StructureModifier<Object> mods = spawnPackets[0].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, (int) Math.floor(loc.getX() * 32D));
mods.write(2, (int) Math.floor(loc.getY() * 32D));
mods.write(3, (int) Math.floor(loc.getZ() * 32D));
if (data > 0) {
Vector vec = disguisedEntity.getVelocity();
double d1 = vec.getX();
double d2 = vec.getY();
double d3 = vec.getZ();
double d4 = 3.9D;
if (d1 < -d4)
d1 = -d4;
if (d2 < -d4)
d2 = -d4;
if (d3 < -d4)
d3 = -d4;
if (d1 > d4)
d1 = d4;
if (d2 > d4)
d2 = d4;
if (d3 > d4)
d3 = d4;
mods.write(4, (int) (d1 * 8000.0D));
mods.write(5, (int) (d2 * 8000.0D));
mods.write(6, (int) (d3 * 8000.0D));
}
mods.write(7, (int) MathHelper.floor(loc.getPitch() * 256.0F / 360.0F));
mods.write(8, (int) MathHelper.floor(loc.getYaw() * 256.0F / 360.0F) - 64);
mods.write(9, id);
mods.write(10, data);
}
if (spawnPackets[1] == null) {
// Make a packet to turn his head!
spawnPackets[1] = manager.createPacket(Packets.Server.ENTITY_HEAD_ROTATION);
StructureModifier<Object> mods = spawnPackets[1].getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, (byte) (int) Math.floor(loc.getYaw() * 256.0F / 360.0F));
}
return spawnPackets;
}
private DataWatcher convertDataWatcher(DataWatcher watcher, FlagWatcher flagWatcher) {
DataWatcher newWatcher = new DataWatcher();
try {
Field map = newWatcher.getClass().getDeclaredField("c");
map.setAccessible(true);
HashMap c = (HashMap) map.get(newWatcher);
// Calling c() gets the watchable objects exactly as they are.
List<WatchableObject> list = watcher.c();
for (WatchableObject watchableObject : flagWatcher.convert(list))
c.put(watchableObject.a(), watchableObject);
} catch (Exception ex) {
ex.printStackTrace();
}
return newWatcher;
}
@Override
public void onEnable() {
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") == null) {
@ -219,6 +407,15 @@ public class LibsDisguises extends JavaPlugin {
DisguiseAPI.init(this);
DisguiseAPI.enableSounds(true);
DisguiseAPI.setVelocitySent(true);
try {
// Here I use reflection to set the plugin for Disguise..
// Kinda stupid but I don't want open API calls.
Field field = Disguise.class.getDeclaredField("plugin");
field.setAccessible(true);
field.set(null, this);
} catch (Exception ex) {
ex.printStackTrace();
}
addPacketListeners();
saveDefaultConfig();
if (!getConfig().contains("DisguiseRadiusMax"))