2013-05-17 23:05:19 +02:00
|
|
|
package me.libraryaddict.disguise;
|
|
|
|
|
2013-05-21 02:59:57 +02:00
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.util.Arrays;
|
2013-05-17 23:05:19 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.Disguise;
|
2013-05-21 02:59:57 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.DisguiseType;
|
2013-05-17 23:05:19 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseTypes.PlayerDisguise;
|
2013-05-19 05:44:13 +02:00
|
|
|
import net.minecraft.server.v1_5_R3.WatchableObject;
|
|
|
|
|
2013-05-21 02:59:57 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.craftbukkit.v1_5_R3.CraftSound;
|
2013-05-27 14:11:12 +02:00
|
|
|
import org.bukkit.entity.Entity;
|
2013-05-17 23:05:19 +02:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
|
|
|
import com.comphenix.protocol.Packets;
|
|
|
|
import com.comphenix.protocol.ProtocolLibrary;
|
|
|
|
import com.comphenix.protocol.events.ConnectionSide;
|
|
|
|
import com.comphenix.protocol.events.ListenerPriority;
|
|
|
|
import com.comphenix.protocol.events.PacketAdapter;
|
|
|
|
import com.comphenix.protocol.events.PacketEvent;
|
|
|
|
import com.comphenix.protocol.reflect.StructureModifier;
|
|
|
|
|
|
|
|
public class LibsDisguises extends JavaPlugin {
|
|
|
|
|
|
|
|
public void onEnable() {
|
|
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(
|
|
|
|
new PacketAdapter(this, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, Packets.Server.NAMED_ENTITY_SPAWN,
|
2013-05-27 14:11:12 +02:00
|
|
|
Packets.Server.ENTITY_METADATA, Packets.Server.NAMED_SOUND_EFFECT, Packets.Server.ARM_ANIMATION,
|
|
|
|
Packets.Server.REL_ENTITY_MOVE_LOOK, Packets.Server.ENTITY_LOOK, Packets.Server.ENTITY_TELEPORT) {
|
2013-05-20 13:00:59 +02:00
|
|
|
@Override
|
2013-05-17 23:05:19 +02:00
|
|
|
public void onPacketSending(PacketEvent event) {
|
|
|
|
StructureModifier<Object> mods = event.getPacket().getModifier();
|
|
|
|
try {
|
|
|
|
Player observer = event.getPlayer();
|
2013-05-21 02:59:57 +02:00
|
|
|
if (event.getPacketID() == Packets.Server.NAMED_SOUND_EFFECT) {
|
|
|
|
if (!DisguiseAPI.playSounds())
|
|
|
|
return;
|
|
|
|
String soundName = (String) mods.read(0);
|
|
|
|
if (soundName.startsWith("step.") || soundName.equals("damage.hit")) {
|
|
|
|
Disguise dis = null;
|
|
|
|
Location soundLoc = new Location(observer.getWorld(), (Integer) mods.read(1), (Integer) mods
|
|
|
|
.read(2), (Integer) mods.read(3));
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
|
|
if (DisguiseAPI.isDisguised(player)) {
|
|
|
|
Location loc = player.getLocation();
|
|
|
|
Location dLoc = new Location(observer.getWorld(), (int) (loc.getX() * 8), (int) (loc
|
|
|
|
.getY() * 8), (int) (loc.getZ() * 8));
|
|
|
|
if (dLoc.equals(soundLoc)) {
|
|
|
|
dis = DisguiseAPI.getDisguise(player);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dis != null) {
|
|
|
|
if (dis.getType().isPlayer())
|
|
|
|
return;
|
|
|
|
if (dis.getType().isMisc()) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String sound = null;
|
|
|
|
try {
|
|
|
|
Field field = CraftSound.class.getDeclaredField("sounds");
|
|
|
|
field.setAccessible(true);
|
|
|
|
List<String> sounds = Arrays.asList((String[]) field.get(null));
|
|
|
|
String mobName = dis.getType().name().toLowerCase().replace("_", "");
|
|
|
|
if (dis.getType() == DisguiseType.WITHER_SKELETON)
|
|
|
|
mobName = "skeleton";
|
2013-05-21 03:19:46 +02:00
|
|
|
else if (dis.getType() == DisguiseType.CAVE_SPIDER)
|
|
|
|
mobName = "spider";
|
|
|
|
else if (dis.getType() == DisguiseType.ENDERMAN)
|
|
|
|
mobName = "endermen";
|
|
|
|
else if (dis.getType() == DisguiseType.MUSHROOM_COW)
|
|
|
|
mobName = "cow";
|
2013-05-21 02:59:57 +02:00
|
|
|
if (soundName.startsWith("step.")) {
|
|
|
|
if (sounds.contains("mob." + mobName + ".walk"))
|
|
|
|
sound = "mob." + mobName + ".walk";
|
|
|
|
else if (sounds.contains("mob." + mobName + ".step"))
|
|
|
|
sound = "mob." + mobName + ".step";
|
|
|
|
} else if (soundName.equals("damage.hit")) {
|
2013-05-21 03:19:46 +02:00
|
|
|
if (dis.getType() == DisguiseType.SNOWMAN
|
|
|
|
|| dis.getType() == DisguiseType.VILLAGER
|
|
|
|
|| dis.getType() == DisguiseType.WITCH) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (dis.getType() == DisguiseType.PIG_ZOMBIE)
|
|
|
|
sound = "mob.zombiepig.zpighurt";
|
|
|
|
else if (dis.getType() == DisguiseType.GHAST)
|
|
|
|
sound = "mob.ghast.scream";
|
|
|
|
else if (dis.getType() == DisguiseType.OCELOT)
|
|
|
|
sound = "mob.cat.hitt";
|
|
|
|
else if (mobName.equals("slime"))
|
|
|
|
sound = "mob.slime.attack";
|
|
|
|
else if (sounds.contains("mob." + mobName + ".hit"))
|
2013-05-21 02:59:57 +02:00
|
|
|
sound = "mob." + mobName + ".hit";
|
|
|
|
else if (sounds.contains("mob." + mobName + ".hurt"))
|
|
|
|
sound = "mob." + mobName + ".hurt";
|
|
|
|
else if (sounds.contains("mob." + mobName + ".say"))
|
|
|
|
sound = "mob." + mobName + ".say";
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
if (sound != null) {
|
|
|
|
mods.write(0, sound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-27 14:11:12 +02:00
|
|
|
StructureModifier<Entity> entityModifer = event.getPacket()
|
|
|
|
.getEntityModifier(observer.getWorld());
|
|
|
|
org.bukkit.entity.Entity entity = entityModifer.read(0);
|
2013-05-21 02:59:57 +02:00
|
|
|
if (entity instanceof Player) {
|
|
|
|
Player watched = (Player) entity;
|
|
|
|
if (DisguiseAPI.isDisguised(watched.getName())) {
|
|
|
|
Disguise disguise = DisguiseAPI.getDisguise(watched);
|
|
|
|
if (event.getPacketID() == Packets.Server.ENTITY_METADATA
|
|
|
|
&& !disguise.getType().isPlayer()) {
|
|
|
|
if (disguise.hasWatcher()) {
|
|
|
|
mods.write(1, disguise.getWatcher().convert((List<WatchableObject>) mods.read(1)));
|
|
|
|
}
|
|
|
|
} else if (event.getPacketID() == Packets.Server.NAMED_ENTITY_SPAWN) {
|
|
|
|
if (disguise.getType().isPlayer()) {
|
|
|
|
String name = (String) mods.read(1);
|
|
|
|
if (!name.equals(((PlayerDisguise) disguise).getName())) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
DisguiseAPI.disguiseToPlayer(watched, observer, disguise);
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-17 23:05:19 +02:00
|
|
|
event.setCancelled(true);
|
2013-05-20 13:00:59 +02:00
|
|
|
DisguiseAPI.disguiseToPlayer(watched, observer, disguise);
|
2013-05-17 23:05:19 +02:00
|
|
|
}
|
2013-05-27 14:11:12 +02:00
|
|
|
} else if (event.getPacketID() == Packets.Server.ARM_ANIMATION) {
|
2013-05-21 02:59:57 +02:00
|
|
|
if (disguise.getType().isMisc()) {
|
2013-05-27 14:11:12 +02:00
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (disguise.getType() == DisguiseType.ENDER_DRAGON) {
|
|
|
|
byte value = (Byte) mods.read(4);
|
|
|
|
mods.write(4, (byte) (value - 128));
|
|
|
|
} else if (disguise.getType().isMisc()) {
|
|
|
|
byte value = (Byte) mods.read(4);
|
|
|
|
mods.write(4, (byte) (value + 64));
|
2013-05-21 02:59:57 +02:00
|
|
|
}
|
2013-05-20 13:00:59 +02:00
|
|
|
}
|
2013-05-17 23:05:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-05-21 04:23:58 +02:00
|
|
|
getCommand("disguise").setExecutor(new DisguiseCommand());
|
|
|
|
getCommand("undisguise").setExecutor(new UndisguiseCommand());
|
2013-05-17 23:05:19 +02:00
|
|
|
}
|
|
|
|
}
|