2013-05-17 23:05:19 +02:00
|
|
|
package me.libraryaddict.disguise;
|
|
|
|
|
2013-08-05 14:04:01 +02:00
|
|
|
import java.io.File;
|
2013-07-29 01:21:25 +02:00
|
|
|
import java.lang.reflect.Field;
|
2013-07-15 09:33:23 +02:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2013-07-22 15:32:05 +02:00
|
|
|
import java.lang.reflect.Method;
|
2013-07-11 20:38:07 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
2013-05-17 23:05:19 +02:00
|
|
|
import java.util.List;
|
2013-07-29 01:21:25 +02:00
|
|
|
import java.util.Random;
|
|
|
|
|
2013-07-16 07:23:11 +02:00
|
|
|
import me.libraryaddict.disguise.Commands.*;
|
2013-05-17 23:05:19 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.Disguise;
|
2013-07-22 15:32:05 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.DisguiseSound;
|
2013-05-21 02:59:57 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.DisguiseType;
|
2013-07-28 01:00:29 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher;
|
2013-07-29 01:21:25 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.MiscDisguise;
|
2013-05-17 23:05:19 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.PlayerDisguise;
|
2013-07-11 20:38:07 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.Values;
|
2013-07-28 01:00:29 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.Watchers.AgeableWatcher;
|
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.Watchers.LivingWatcher;
|
2013-07-11 20:38:07 +02:00
|
|
|
import net.minecraft.server.v1_6_R2.AttributeSnapshot;
|
|
|
|
import net.minecraft.server.v1_6_R2.ChatMessage;
|
|
|
|
import net.minecraft.server.v1_6_R2.ChunkCoordinates;
|
2013-07-29 01:21:25 +02:00
|
|
|
import net.minecraft.server.v1_6_R2.DataWatcher;
|
2013-07-11 20:38:07 +02:00
|
|
|
import net.minecraft.server.v1_6_R2.EntityHuman;
|
|
|
|
import net.minecraft.server.v1_6_R2.EntityLiving;
|
2013-07-29 01:21:25 +02:00
|
|
|
import net.minecraft.server.v1_6_R2.EnumArt;
|
2013-08-05 07:26:56 +02:00
|
|
|
import net.minecraft.server.v1_6_R2.EnumEntitySize;
|
2013-07-11 20:38:07 +02:00
|
|
|
import net.minecraft.server.v1_6_R2.GenericAttributes;
|
2013-07-29 01:21:25 +02:00
|
|
|
import net.minecraft.server.v1_6_R2.ItemStack;
|
|
|
|
import net.minecraft.server.v1_6_R2.MathHelper;
|
2013-07-09 04:51:35 +02:00
|
|
|
import net.minecraft.server.v1_6_R2.WatchableObject;
|
2013-07-11 20:38:07 +02:00
|
|
|
import net.minecraft.server.v1_6_R2.World;
|
2013-05-29 16:25:00 +02:00
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
2013-07-29 01:21:25 +02:00
|
|
|
import org.bukkit.Location;
|
2013-08-05 14:04:01 +02:00
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
2013-07-11 20:38:07 +02:00
|
|
|
import org.bukkit.craftbukkit.v1_6_R2.CraftWorld;
|
2013-07-29 01:21:25 +02:00
|
|
|
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;
|
2013-07-28 01:00:29 +02:00
|
|
|
import org.bukkit.entity.Ageable;
|
2013-05-29 00:29:36 +02:00
|
|
|
import org.bukkit.entity.Arrow;
|
2013-05-27 14:11:12 +02:00
|
|
|
import org.bukkit.entity.Entity;
|
2013-05-29 00:29:36 +02:00
|
|
|
import org.bukkit.entity.ExperienceOrb;
|
|
|
|
import org.bukkit.entity.Item;
|
2013-07-28 01:00:29 +02:00
|
|
|
import org.bukkit.entity.LivingEntity;
|
2013-05-17 23:05:19 +02:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
2013-07-29 01:21:25 +02:00
|
|
|
import org.bukkit.util.Vector;
|
|
|
|
|
2013-05-17 23:05:19 +02:00
|
|
|
import com.comphenix.protocol.Packets;
|
|
|
|
import com.comphenix.protocol.ProtocolLibrary;
|
2013-05-29 00:29:36 +02:00
|
|
|
import com.comphenix.protocol.ProtocolManager;
|
2013-05-17 23:05:19 +02:00
|
|
|
import com.comphenix.protocol.events.ConnectionSide;
|
|
|
|
import com.comphenix.protocol.events.ListenerPriority;
|
|
|
|
import com.comphenix.protocol.events.PacketAdapter;
|
2013-07-06 16:34:12 +02:00
|
|
|
import com.comphenix.protocol.events.PacketContainer;
|
2013-05-17 23:05:19 +02:00
|
|
|
import com.comphenix.protocol.events.PacketEvent;
|
|
|
|
import com.comphenix.protocol.reflect.StructureModifier;
|
|
|
|
|
2013-07-21 05:14:59 +02:00
|
|
|
public class LibsDisguises extends JavaPlugin {
|
2013-07-11 20:38:07 +02:00
|
|
|
private class DisguiseHuman extends EntityHuman {
|
|
|
|
|
|
|
|
public DisguiseHuman(World world) {
|
|
|
|
super(world, "LibsDisguises");
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean a(int arg0, String arg1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChunkCoordinates b() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(ChatMessage arg0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-21 05:14:59 +02:00
|
|
|
private void addPacketListeners() {
|
2013-07-29 04:56:47 +02:00
|
|
|
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
|
2013-07-30 02:44:00 +02:00
|
|
|
manager.addPacketListener(new PacketAdapter(this, ConnectionSide.SERVER_SIDE, ListenerPriority.HIGH,
|
2013-05-29 00:29:36 +02:00
|
|
|
Packets.Server.NAMED_ENTITY_SPAWN, Packets.Server.ENTITY_METADATA, Packets.Server.ARM_ANIMATION,
|
|
|
|
Packets.Server.REL_ENTITY_MOVE_LOOK, Packets.Server.ENTITY_LOOK, Packets.Server.ENTITY_TELEPORT,
|
|
|
|
Packets.Server.ADD_EXP_ORB, Packets.Server.VEHICLE_SPAWN, Packets.Server.MOB_SPAWN,
|
2013-08-01 10:29:04 +02:00
|
|
|
Packets.Server.ENTITY_PAINTING, Packets.Server.COLLECT, Packets.Server.UPDATE_ATTRIBUTES,
|
|
|
|
Packets.Server.ENTITY_EQUIPMENT) {
|
2013-05-29 00:29:36 +02:00
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent event) {
|
2013-07-30 02:44:00 +02:00
|
|
|
Player observer = event.getPlayer();
|
|
|
|
// First get the entity, the one sending this packet
|
|
|
|
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
|
|
|
|
org.bukkit.entity.Entity entity = entityModifer.read((Packets.Server.COLLECT == event.getPacketID() ? 1 : 0));
|
|
|
|
// If the entity is the same as the sender. Don't disguise!
|
|
|
|
// Prevents problems and there is no advantage to be gained.
|
|
|
|
if (entity == observer)
|
|
|
|
return;
|
2013-07-30 05:31:07 +02:00
|
|
|
PacketContainer[] packets = transformPacket(event.getPacket(), event.getPlayer());
|
2013-07-30 02:44:00 +02:00
|
|
|
if (packets.length == 0)
|
|
|
|
event.setCancelled(true);
|
|
|
|
else {
|
|
|
|
event.setPacket(packets[0]);
|
|
|
|
for (int i = 1; i < packets.length; i++) {
|
|
|
|
sendDelayedPacket(packets[i], event.getPlayer());
|
2013-05-17 23:05:19 +02:00
|
|
|
}
|
2013-05-29 00:29:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-07-29 04:56:47 +02:00
|
|
|
// Now add a client listener to cancel them interacting with uninteractable disguised entitys.
|
|
|
|
// You ain't supposed to be allowed to 'interact' with a item that cannot be clicked.
|
2013-05-29 00:29:36 +02:00
|
|
|
manager.addPacketListener(new PacketAdapter(this, ConnectionSide.CLIENT_SIDE, ListenerPriority.NORMAL,
|
|
|
|
Packets.Client.USE_ENTITY) {
|
|
|
|
@Override
|
|
|
|
public void onPacketReceiving(PacketEvent event) {
|
|
|
|
try {
|
|
|
|
Player observer = event.getPlayer();
|
|
|
|
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
|
|
|
|
org.bukkit.entity.Entity entity = entityModifer.read(1);
|
|
|
|
if (DisguiseAPI.isDisguised(entity)
|
|
|
|
&& (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow)) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-07-21 05:14:59 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 01:21:25 +02:00
|
|
|
protected PacketContainer[] constructPacket(Disguise disguise, Entity disguisedEntity) {
|
|
|
|
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
|
|
|
|
net.minecraft.server.v1_6_R2.Entity nmsEntity = ((CraftEntity) disguisedEntity).getHandle();
|
2013-08-01 10:29:04 +02:00
|
|
|
ArrayList<PacketContainer> packets = new ArrayList<PacketContainer>();
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
int slot = i - 1;
|
|
|
|
if (slot < 0)
|
|
|
|
slot = 4;
|
|
|
|
org.bukkit.inventory.ItemStack itemstack = disguise.getWatcher().getItemStack(slot);
|
|
|
|
if (itemstack != null && itemstack.getTypeId() != 0) {
|
|
|
|
ItemStack item = null;
|
|
|
|
if (nmsEntity instanceof EntityLiving)
|
|
|
|
item = ((EntityLiving) nmsEntity).getEquipment(i);
|
|
|
|
if (item == null) {
|
|
|
|
PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_EQUIPMENT);
|
|
|
|
StructureModifier<Object> mods = packet.getModifier();
|
|
|
|
mods.write(0, disguisedEntity.getEntityId());
|
|
|
|
mods.write(1, i);
|
|
|
|
mods.write(2, CraftItemStack.asNMSCopy(itemstack));
|
|
|
|
packets.add(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PacketContainer[] spawnPackets = new PacketContainer[2 + packets.size()];
|
|
|
|
for (int i = 0; i < packets.size(); i++) {
|
|
|
|
spawnPackets[i + 2] = packets.get(i);
|
|
|
|
}
|
2013-07-29 01:21:25 +02:00
|
|
|
Location loc = disguisedEntity.getLocation();
|
2013-07-29 05:25:36 +02:00
|
|
|
byte yaw = getYaw(disguise.getType(), DisguiseType.getType(disguise.getEntity().getType()),
|
|
|
|
(byte) (int) (loc.getYaw() * 256.0F / 360.0F));
|
2013-08-05 07:26:56 +02:00
|
|
|
EnumEntitySize entitySize = Values.getValues(disguise.getType()).getEntitySize();
|
2013-08-01 10:29:04 +02:00
|
|
|
|
2013-07-29 01:21:25 +02:00
|
|
|
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());
|
2013-08-05 07:26:56 +02:00
|
|
|
mods.write(1, (int) Math.floor(entitySize.a(loc.getX()) * 32D));
|
2013-07-29 01:21:25 +02:00
|
|
|
mods.write(2, (int) Math.floor(loc.getY() * 32D));
|
2013-08-05 07:26:56 +02:00
|
|
|
mods.write(3, (int) Math.floor(entitySize.a(loc.getZ()) * 32D));
|
2013-07-29 05:25:36 +02:00
|
|
|
mods.write(4, yaw);
|
2013-07-29 01:21:25 +02:00
|
|
|
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));
|
2013-07-29 05:25:36 +02:00
|
|
|
mods.write(5, yaw);
|
2013-07-29 01:21:25 +02:00
|
|
|
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;
|
2013-08-05 07:26:56 +02:00
|
|
|
mods.write(2, entitySize.a(loc.getX()));
|
2013-07-29 01:21:25 +02:00
|
|
|
mods.write(3, (int) Math.floor(loc.getY() * 32D));
|
2013-08-05 07:26:56 +02:00
|
|
|
mods.write(4, entitySize.a(loc.getZ()));
|
2013-07-29 01:21:25 +02:00
|
|
|
mods.write(5, (int) (d2 * 8000.0D));
|
|
|
|
mods.write(6, (int) (d3 * 8000.0D));
|
|
|
|
mods.write(7, (int) (d4 * 8000.0D));
|
2013-07-29 05:25:36 +02:00
|
|
|
mods.write(8, yaw);
|
2013-07-29 01:21:25 +02:00
|
|
|
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));
|
2013-07-29 05:25:36 +02:00
|
|
|
mods.write(8, yaw);
|
2013-07-29 01:21:25 +02:00
|
|
|
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();
|
2013-07-30 02:44:00 +02:00
|
|
|
for (WatchableObject watchableObject : flagWatcher.convert(list)) {
|
2013-07-29 01:21:25 +02:00
|
|
|
c.put(watchableObject.a(), watchableObject);
|
2013-07-30 02:44:00 +02:00
|
|
|
}
|
2013-07-29 01:21:25 +02:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return newWatcher;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-30 02:44:00 +02:00
|
|
|
private byte getYaw(DisguiseType disguiseType, DisguiseType entityType, byte value) {
|
|
|
|
switch (disguiseType) {
|
|
|
|
case ENDER_DRAGON:
|
|
|
|
value -= 128;
|
|
|
|
break;
|
|
|
|
case ITEM_FRAME:
|
|
|
|
case ARROW:
|
|
|
|
value = (byte) -value;
|
|
|
|
break;
|
|
|
|
case PAINTING:
|
|
|
|
value = (byte) -(value + 128);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (disguiseType.isMisc()) {
|
|
|
|
value -= 64;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (entityType) {
|
|
|
|
case ENDER_DRAGON:
|
|
|
|
value += 128;
|
|
|
|
break;
|
|
|
|
case ITEM_FRAME:
|
|
|
|
case ARROW:
|
|
|
|
value = (byte) -value;
|
|
|
|
break;
|
|
|
|
case PAINTING:
|
|
|
|
value = (byte) -(value - 128);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (entityType.isMisc()) {
|
|
|
|
value += 64;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2013-07-21 05:14:59 +02:00
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
2013-07-31 07:46:24 +02:00
|
|
|
saveDefaultConfig();
|
2013-08-05 14:04:01 +02:00
|
|
|
YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml"));
|
|
|
|
if (!config.contains("DisguiseRadiusMax"))
|
|
|
|
config.set("DisguiseRadiusMax", getConfig().getInt("DisguiseRadiusMax"));
|
|
|
|
if (!config.contains("UndisguiseRadiusMax"))
|
|
|
|
config.set("UndisguiseRadiusMax", getConfig().getInt("UndisguiseRadiusMax"));
|
|
|
|
if (!config.contains("DisguiseSounds"))
|
|
|
|
config.set("DisguiseSounds", getConfig().getBoolean("DisguiseSounds"));
|
|
|
|
if (!config.contains("HearSelfDisguise"))
|
|
|
|
config.set("HearSelfDisguise", getConfig().getBoolean("HearSelfDisguise"));
|
|
|
|
if (!config.contains("SendVelocity"))
|
|
|
|
config.set("SendVelocity", getConfig().getBoolean("SendVelocity"));
|
2013-07-21 05:14:59 +02:00
|
|
|
DisguiseAPI.init(this);
|
2013-07-31 07:46:24 +02:00
|
|
|
DisguiseAPI.enableSounds(getConfig().getBoolean("DisguiseSounds"));
|
|
|
|
DisguiseAPI.setVelocitySent(getConfig().getBoolean("SendVelocity"));
|
2013-07-30 02:44:00 +02:00
|
|
|
DisguiseAPI.setViewDisguises(getConfig().getBoolean("ViewDisguises"));
|
2013-07-31 07:46:24 +02:00
|
|
|
DisguiseAPI.setHearSelfDisguise(getConfig().getBoolean("HearSelfDisguise"));
|
2013-07-29 01:21:25 +02:00
|
|
|
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();
|
|
|
|
}
|
2013-07-21 05:14:59 +02:00
|
|
|
addPacketListeners();
|
2013-07-16 07:23:11 +02:00
|
|
|
DisguiseListener listener = new DisguiseListener(this);
|
|
|
|
Bukkit.getPluginManager().registerEvents(listener, this);
|
2013-05-21 04:23:58 +02:00
|
|
|
getCommand("disguise").setExecutor(new DisguiseCommand());
|
|
|
|
getCommand("undisguise").setExecutor(new UndisguiseCommand());
|
2013-05-29 00:44:55 +02:00
|
|
|
getCommand("disguiseplayer").setExecutor(new DisguisePlayerCommand());
|
|
|
|
getCommand("undisguiseplayer").setExecutor(new UndisguisePlayerCommand());
|
2013-07-16 07:23:11 +02:00
|
|
|
getCommand("undisguiseentity").setExecutor(new UndisguiseEntityCommand(listener));
|
|
|
|
getCommand("disguiseentity").setExecutor(new DisguiseEntityCommand(listener));
|
2013-07-16 08:53:39 +02:00
|
|
|
getCommand("disguiseradius").setExecutor(new DisguiseRadiusCommand(getConfig().getInt("DisguiseRadiusMax")));
|
|
|
|
getCommand("undisguiseradius").setExecutor(new UndisguiseRadiusCommand(getConfig().getInt("UndisguiseRadiusMax")));
|
2013-07-11 20:38:07 +02:00
|
|
|
registerValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void registerValues() {
|
|
|
|
World world = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle();
|
|
|
|
for (DisguiseType disguiseType : DisguiseType.values()) {
|
2013-07-28 01:00:29 +02:00
|
|
|
Class watcherClass = null;
|
|
|
|
try {
|
|
|
|
String name;
|
|
|
|
switch (disguiseType) {
|
|
|
|
case MINECART_FURNACE:
|
|
|
|
case MINECART_HOPPER:
|
|
|
|
case MINECART_MOB_SPAWNER:
|
|
|
|
case MINECART_TNT:
|
|
|
|
case MINECART_CHEST:
|
|
|
|
name = "Minecart";
|
|
|
|
break;
|
|
|
|
case DONKEY:
|
|
|
|
case MULE:
|
|
|
|
case UNDEAD_HORSE:
|
|
|
|
case SKELETON_HORSE:
|
|
|
|
name = "Horse";
|
|
|
|
break;
|
|
|
|
case ZOMBIE_VILLAGER:
|
|
|
|
case PIG_ZOMBIE:
|
|
|
|
name = "Zombie";
|
|
|
|
break;
|
|
|
|
case MAGMA_CUBE:
|
|
|
|
name = "Slime";
|
2013-08-05 14:08:53 +02:00
|
|
|
break;
|
2013-07-28 01:00:29 +02:00
|
|
|
default:
|
|
|
|
name = toReadable(disguiseType.name());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
watcherClass = Class.forName("me.libraryaddict.disguise.DisguiseTypes.Watchers." + name + "Watcher");
|
|
|
|
} catch (Exception ex) {
|
|
|
|
// There is no watcher for this entity, or a error was thrown.
|
|
|
|
try {
|
|
|
|
Class c = disguiseType.getEntityType().getEntityClass();
|
2013-07-28 01:55:59 +02:00
|
|
|
if (Ageable.class.isAssignableFrom(c))
|
2013-07-28 01:00:29 +02:00
|
|
|
watcherClass = AgeableWatcher.class;
|
2013-07-28 01:55:59 +02:00
|
|
|
else if (LivingEntity.class.isAssignableFrom(c))
|
2013-07-28 01:00:29 +02:00
|
|
|
watcherClass = LivingWatcher.class;
|
|
|
|
else
|
|
|
|
watcherClass = FlagWatcher.class;
|
|
|
|
} catch (Exception ex1) {
|
|
|
|
ex1.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disguiseType.setWatcherClass(watcherClass);
|
2013-07-11 20:38:07 +02:00
|
|
|
String name = toReadable(disguiseType.name());
|
2013-07-28 01:00:29 +02:00
|
|
|
switch (disguiseType) {
|
|
|
|
case WITHER_SKELETON:
|
|
|
|
case ZOMBIE_VILLAGER:
|
|
|
|
case DONKEY:
|
|
|
|
case MULE:
|
|
|
|
case UNDEAD_HORSE:
|
|
|
|
case SKELETON_HORSE:
|
2013-08-05 14:21:47 +02:00
|
|
|
continue;
|
2013-07-28 01:00:29 +02:00
|
|
|
case PRIMED_TNT:
|
2013-07-11 20:38:07 +02:00
|
|
|
name = "TNTPrimed";
|
2013-07-28 01:00:29 +02:00
|
|
|
break;
|
|
|
|
case MINECART_TNT:
|
2013-07-11 20:38:07 +02:00
|
|
|
name = "MinecartTNT";
|
2013-07-28 01:00:29 +02:00
|
|
|
break;
|
|
|
|
case MINECART:
|
2013-07-16 13:42:35 +02:00
|
|
|
name = "MinecartRideable";
|
2013-07-28 01:00:29 +02:00
|
|
|
break;
|
|
|
|
case FIREWORK:
|
2013-07-16 13:42:35 +02:00
|
|
|
name = "Fireworks";
|
2013-07-28 01:00:29 +02:00
|
|
|
break;
|
|
|
|
case SPLASH_POTION:
|
2013-07-11 20:38:07 +02:00
|
|
|
name = "Potion";
|
2013-07-28 01:00:29 +02:00
|
|
|
break;
|
|
|
|
case GIANT:
|
2013-07-11 20:38:07 +02:00
|
|
|
name = "GiantZombie";
|
2013-07-28 01:00:29 +02:00
|
|
|
break;
|
|
|
|
case DROPPED_ITEM:
|
2013-07-11 20:38:07 +02:00
|
|
|
name = "Item";
|
2013-07-28 01:00:29 +02:00
|
|
|
break;
|
|
|
|
case FIREBALL:
|
2013-07-11 20:38:07 +02:00
|
|
|
name = "LargeFireball";
|
2013-07-28 01:00:29 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-07-11 20:38:07 +02:00
|
|
|
try {
|
|
|
|
net.minecraft.server.v1_6_R2.Entity entity = null;
|
2013-07-15 09:33:23 +02:00
|
|
|
Class entityClass;
|
2013-07-11 20:38:07 +02:00
|
|
|
if (disguiseType == DisguiseType.PLAYER) {
|
2013-07-15 09:33:23 +02:00
|
|
|
entityClass = EntityHuman.class;
|
2013-07-11 20:38:07 +02:00
|
|
|
entity = new DisguiseHuman(world);
|
|
|
|
} else {
|
2013-07-15 09:33:23 +02:00
|
|
|
entityClass = Class.forName("net.minecraft.server.v1_6_R2.Entity" + name);
|
2013-07-11 20:38:07 +02:00
|
|
|
entity = (net.minecraft.server.v1_6_R2.Entity) entityClass.getConstructor(World.class).newInstance(world);
|
|
|
|
}
|
2013-08-05 07:26:56 +02:00
|
|
|
Values value = new Values(disguiseType, entityClass, entity.at);
|
2013-07-11 20:38:07 +02:00
|
|
|
List<WatchableObject> watchers = entity.getDataWatcher().c();
|
|
|
|
for (WatchableObject watch : watchers)
|
|
|
|
value.setMetaValue(watch.a(), watch.b());
|
|
|
|
if (entity instanceof EntityLiving) {
|
|
|
|
EntityLiving livingEntity = (EntityLiving) entity;
|
2013-07-12 22:19:24 +02:00
|
|
|
value.setAttributesValue(GenericAttributes.d.a(), livingEntity.getAttributeInstance(GenericAttributes.d)
|
2013-07-11 20:38:07 +02:00
|
|
|
.getValue());
|
|
|
|
}
|
2013-07-22 15:32:05 +02:00
|
|
|
DisguiseSound sound = DisguiseSound.getType(disguiseType.name());
|
|
|
|
if (sound != null) {
|
|
|
|
Method soundStrength = EntityLiving.class.getDeclaredMethod("aZ");
|
|
|
|
soundStrength.setAccessible(true);
|
|
|
|
sound.setDamageSoundVolume((Float) soundStrength.invoke(entity));
|
|
|
|
}
|
2013-07-11 20:38:07 +02:00
|
|
|
} catch (Exception e1) {
|
|
|
|
System.out.print("[LibsDisguises] Trouble while making values for " + name + ": " + e1.getMessage());
|
|
|
|
System.out.print("[LibsDisguises] Please report this to LibsDisguises author");
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2013-05-31 10:55:55 +02:00
|
|
|
}
|
|
|
|
|
2013-07-30 02:44:00 +02:00
|
|
|
private void sendDelayedPacket(final PacketContainer packet, final Player player) {
|
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-11 20:38:38 +02:00
|
|
|
private String toReadable(String string) {
|
2013-07-28 01:00:29 +02:00
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
for (String s : string.split("_")) {
|
|
|
|
builder.append(s.substring(0, 1) + s.substring(1).toLowerCase());
|
|
|
|
}
|
|
|
|
return builder.toString();
|
2013-05-17 23:05:19 +02:00
|
|
|
}
|
2013-08-01 10:29:04 +02:00
|
|
|
|
|
|
|
protected PacketContainer[] transformPacket(PacketContainer sentPacket, Player observer) {
|
|
|
|
PacketContainer[] packets = new PacketContainer[] { sentPacket };
|
|
|
|
try {
|
|
|
|
// First get the entity, the one sending this packet
|
|
|
|
StructureModifier<Entity> entityModifer = sentPacket.getEntityModifier(observer.getWorld());
|
|
|
|
org.bukkit.entity.Entity entity = entityModifer.read((Packets.Server.COLLECT == sentPacket.getID() ? 1 : 0));
|
|
|
|
Disguise disguise = DisguiseAPI.getDisguise(entity);
|
|
|
|
// If disguised.
|
|
|
|
if (disguise != null) {
|
2013-08-05 14:21:47 +02:00
|
|
|
// If packet is Packets.Server.UPDATE_ATTRIBUTES
|
2013-08-01 10:29:04 +02:00
|
|
|
// This packet sends attributes
|
|
|
|
|
|
|
|
switch (sentPacket.getID()) {
|
|
|
|
case Packets.Server.UPDATE_ATTRIBUTES:
|
|
|
|
|
|
|
|
{
|
|
|
|
// Grab the values which are 'approved' to be sent for this entity
|
|
|
|
HashMap<String, Double> values = Values.getAttributesValues(disguise.getType());
|
|
|
|
Collection collection = new ArrayList<AttributeSnapshot>();
|
|
|
|
for (AttributeSnapshot att : (List<AttributeSnapshot>) sentPacket.getModifier().read(1)) {
|
|
|
|
if (values.containsKey(att.a())) {
|
|
|
|
collection.add(new AttributeSnapshot(null, att.a(), values.get(att.a()), att.c()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (collection.size() > 0) {
|
|
|
|
packets[0] = new PacketContainer(sentPacket.getID());
|
|
|
|
StructureModifier<Object> mods = packets[0].getModifier();
|
|
|
|
mods.write(0, entity.getEntityId());
|
|
|
|
mods.write(1, collection);
|
|
|
|
} else {
|
|
|
|
packets = new PacketContainer[0];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else if the packet is sending entity metadata
|
|
|
|
case Packets.Server.ENTITY_METADATA:
|
|
|
|
|
|
|
|
{
|
|
|
|
List<WatchableObject> watchableObjects = disguise.getWatcher().convert(
|
|
|
|
(List<WatchableObject>) packets[0].getModifier().read(1));
|
|
|
|
packets[0] = new PacketContainer(sentPacket.getID());
|
|
|
|
StructureModifier<Object> newMods = packets[0].getModifier();
|
|
|
|
newMods.write(0, entity.getEntityId());
|
|
|
|
newMods.write(1, watchableObjects);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else if the packet is spawning..
|
|
|
|
case Packets.Server.NAMED_ENTITY_SPAWN:
|
|
|
|
case Packets.Server.MOB_SPAWN:
|
|
|
|
case Packets.Server.ADD_EXP_ORB:
|
|
|
|
case Packets.Server.VEHICLE_SPAWN:
|
|
|
|
case Packets.Server.ENTITY_PAINTING:
|
|
|
|
|
|
|
|
{
|
|
|
|
packets = constructPacket(disguise, entity);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else if the disguise is attempting to send players a forbidden packet
|
|
|
|
case Packets.Server.ARM_ANIMATION:
|
|
|
|
case Packets.Server.COLLECT:
|
|
|
|
|
|
|
|
{
|
|
|
|
if (disguise.getType().isMisc())
|
|
|
|
packets = new PacketContainer[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
// Else if the disguise is moving.
|
|
|
|
case Packets.Server.REL_ENTITY_MOVE_LOOK:
|
|
|
|
case Packets.Server.ENTITY_LOOK:
|
|
|
|
case Packets.Server.ENTITY_TELEPORT:
|
|
|
|
|
|
|
|
{
|
|
|
|
packets[0] = sentPacket.shallowClone();
|
|
|
|
StructureModifier<Object> mods = packets[0].getModifier();
|
|
|
|
byte value = (Byte) mods.read(4);
|
|
|
|
mods.write(4, getYaw(disguise.getType(), DisguiseType.getType(entity.getType()), value));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Packets.Server.ENTITY_EQUIPMENT:
|
|
|
|
|
|
|
|
{
|
|
|
|
int slot = (Integer) packets[0].getModifier().read(1) - 1;
|
|
|
|
if (slot < 0)
|
|
|
|
slot = 4;
|
|
|
|
org.bukkit.inventory.ItemStack itemstack = disguise.getWatcher().getItemStack(slot);
|
|
|
|
if (itemstack != null) {
|
|
|
|
packets[0] = packets[0].shallowClone();
|
|
|
|
packets[0].getModifier().write(2,
|
|
|
|
(itemstack.getTypeId() == 0 ? null : CraftItemStack.asNMSCopy(itemstack)));
|
|
|
|
}
|
2013-08-05 14:21:47 +02:00
|
|
|
break;
|
2013-08-01 10:29:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return packets;
|
|
|
|
}
|
2013-05-17 23:05:19 +02:00
|
|
|
}
|