Removed isExp() and now supports playing sounds. ALso no crashing for entitys

This commit is contained in:
Andrew 2013-05-21 12:59:57 +12:00
parent 1739c96e38
commit 2ff2acb3a1
5 changed files with 115 additions and 27 deletions

View File

@ -12,6 +12,7 @@ import org.bukkit.entity.Player;
public class DisguiseAPI { public class DisguiseAPI {
private static ConcurrentHashMap<String, Disguise> disguises = new ConcurrentHashMap<String, Disguise>(); private static ConcurrentHashMap<String, Disguise> disguises = new ConcurrentHashMap<String, Disguise>();
private static boolean playSounds = true;
/** /**
* @param Player * @param Player
@ -79,6 +80,20 @@ public class DisguiseAPI {
return disguises.containsKey(name); return disguises.containsKey(name);
} }
/**
* Does sounds play when the disguiser is hurt
*/
public static boolean playSounds() {
return playSounds;
}
/**
* @param Boolean - Set if sounds play when the disguiser is hurt
*/
public static void setPlaySounds(boolean sounds) {
playSounds = sounds;
}
/** /**
* @param Disguiser * @param Disguiser
* - Undisguises him * - Undisguises him

View File

@ -53,7 +53,7 @@ public class Disguise {
spawnPacket = new Packet20NamedEntitySpawn(entityHuman); spawnPacket = new Packet20NamedEntitySpawn(entityHuman);
((Packet20NamedEntitySpawn) spawnPacket).b = ((PlayerDisguise) this).getName(); ((Packet20NamedEntitySpawn) spawnPacket).b = ((PlayerDisguise) this).getName();
} else if (getType().isExp()) { } else if (getType() == DisguiseType.EXPERIENCE_ORB) {
Entity entity = getEntity(((CraftPlayer) p).getHandle().world, p.getLocation(), p.getEntityId()); Entity entity = getEntity(((CraftPlayer) p).getHandle().world, p.getLocation(), p.getEntityId());
spawnPacket = new Packet26AddExpOrb((EntityExperienceOrb) entity); spawnPacket = new Packet26AddExpOrb((EntityExperienceOrb) entity);

View File

@ -4,7 +4,7 @@ public enum DisguiseType {
ARROW(EntityType.MISC, 60), BAT(EntityType.MOB), BLAZE(EntityType.MOB), BOAT(EntityType.MISC, 1), CAVE_SPIDER(EntityType.MOB), CHICKEN( ARROW(EntityType.MISC, 60), BAT(EntityType.MOB), BLAZE(EntityType.MOB), BOAT(EntityType.MISC, 1), CAVE_SPIDER(EntityType.MOB), CHICKEN(
EntityType.MOB), COW(EntityType.MOB), CREEPER(EntityType.MOB), EGG(EntityType.MISC, 62), ENDER_CRYSTAL( EntityType.MOB), COW(EntityType.MOB), CREEPER(EntityType.MOB), EGG(EntityType.MISC, 62), ENDER_CRYSTAL(
EntityType.MISC, 51), ENDER_DRAGON(EntityType.MOB), ENDER_PEARL(EntityType.MISC, 65), ENDER_SIGNAL(EntityType.MISC, EntityType.MISC, 51), ENDER_DRAGON(EntityType.MOB), ENDER_PEARL(EntityType.MISC, 65), ENDER_SIGNAL(EntityType.MISC,
72), ENDERMAN(EntityType.MOB), EXPERIENCE_ORB(EntityType.EXP), FALLING_BLOCK(EntityType.MISC, 70, 1), FIREWORKS( 72), ENDERMAN(EntityType.MOB), EXPERIENCE_ORB(EntityType.MISC), FALLING_BLOCK(EntityType.MISC, 70, 1), FIREWORKS(
EntityType.MISC, 76), FISHING_HOOK(EntityType.MISC, 90), GHAST(EntityType.MOB), GIANT_ZOMBIE(EntityType.MOB), IRON_GOLEM( EntityType.MISC, 76), FISHING_HOOK(EntityType.MISC, 90), GHAST(EntityType.MOB), GIANT_ZOMBIE(EntityType.MOB), IRON_GOLEM(
EntityType.MOB), ITEM(EntityType.MISC, 2, 1), ITEM_FRAME(EntityType.MISC, 71), LARGE_FIREBALL(EntityType.MISC, 63, 0), MAGMA_CUBE( EntityType.MOB), ITEM(EntityType.MISC, 2, 1), ITEM_FRAME(EntityType.MISC, 71), LARGE_FIREBALL(EntityType.MISC, 63, 0), MAGMA_CUBE(
EntityType.MOB), MINECART_CHEST(EntityType.MISC, 10, 1), MINECART_FURNACE(EntityType.MISC, 10, 2), MINECART_HOPPER( EntityType.MOB), MINECART_CHEST(EntityType.MISC, 10, 1), MINECART_FURNACE(EntityType.MISC, 10, 2), MINECART_HOPPER(
@ -18,7 +18,7 @@ public enum DisguiseType {
EntityType.MOB); EntityType.MOB);
public static enum EntityType { public static enum EntityType {
EXP, MISC, MOB, PLAYER; MISC, MOB, PLAYER;
} }
public static DisguiseType getType(org.bukkit.entity.EntityType entityType) { public static DisguiseType getType(org.bukkit.entity.EntityType entityType) {
@ -64,10 +64,6 @@ public enum DisguiseType {
return entityId; return entityId;
} }
public boolean isExp() {
return entityType == EntityType.EXP;
}
public boolean isMisc() { public boolean isMisc() {
return entityType == EntityType.MISC; return entityType == EntityType.MISC;
} }

View File

@ -9,6 +9,10 @@ public class ZombieWatcher extends FlagWatcher {
setValue(13, (byte) 0); setValue(13, (byte) 0);
} }
public boolean isVillager() {
return (Byte) getValue(13) == 1;
}
public void setVillager(boolean villager) { public void setVillager(boolean villager) {
if (isVillager() != villager) { if (isVillager() != villager) {
setValue(13, (byte) (villager ? 1 : 0)); setValue(13, (byte) (villager ? 1 : 0));
@ -16,8 +20,4 @@ public class ZombieWatcher extends FlagWatcher {
} }
} }
public boolean isVillager() {
return (Byte) getValue(13) == 1;
}
} }

View File

@ -1,11 +1,17 @@
package me.libraryaddict.disguise; package me.libraryaddict.disguise;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List; import java.util.List;
import me.libraryaddict.disguise.DisguiseTypes.Disguise; import me.libraryaddict.disguise.DisguiseTypes.Disguise;
import me.libraryaddict.disguise.DisguiseTypes.DisguiseType;
import me.libraryaddict.disguise.DisguiseTypes.PlayerDisguise; import me.libraryaddict.disguise.DisguiseTypes.PlayerDisguise;
import net.minecraft.server.v1_5_R3.WatchableObject; import net.minecraft.server.v1_5_R3.WatchableObject;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_5_R3.CraftSound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -23,31 +29,102 @@ public class LibsDisguises extends JavaPlugin {
getCommand("disguise").setExecutor(new DisguiseCommand()); getCommand("disguise").setExecutor(new DisguiseCommand());
ProtocolLibrary.getProtocolManager().addPacketListener( ProtocolLibrary.getProtocolManager().addPacketListener(
new PacketAdapter(this, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, Packets.Server.NAMED_ENTITY_SPAWN, new PacketAdapter(this, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, Packets.Server.NAMED_ENTITY_SPAWN,
Packets.Server.ENTITY_METADATA) { Packets.Server.ENTITY_METADATA, Packets.Server.NAMED_SOUND_EFFECT, Packets.Server.ARM_ANIMATION) {
@Override @Override
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
StructureModifier<Object> mods = event.getPacket().getModifier(); StructureModifier<Object> mods = event.getPacket().getModifier();
try { try {
Player observer = event.getPlayer(); Player observer = event.getPlayer();
org.bukkit.entity.Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0); if (event.getPacketID() == Packets.Server.NAMED_SOUND_EFFECT) {
if (entity instanceof Player) { if (!DisguiseAPI.playSounds())
Player watched = (Player) entity; return;
if (DisguiseAPI.isDisguised(watched.getName())) { String soundName = (String) mods.read(0);
Disguise disguise = DisguiseAPI.getDisguise(watched); if (soundName.startsWith("step.") || soundName.equals("damage.hit")) {
if (event.getPacketID() == Packets.Server.NAMED_ENTITY_SPAWN) { Disguise dis = null;
if (disguise.getType().isPlayer()) { Location soundLoc = new Location(observer.getWorld(), (Integer) mods.read(1), (Integer) mods
String name = (String) mods.read(1); .read(2), (Integer) mods.read(3));
if (!name.equals(((PlayerDisguise) disguise).getName())) { 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";
else if (dis.getType() == DisguiseType.OCELOT)
mobName = "cat";
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")) {
if (sounds.contains("mob." + mobName + ".hit"))
sound = "mob." + mobName + ".hit";
else if (sounds.contains("mob." + mobName + ".hitt"))
sound = "mob." + mobName + ".hitt";
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();
}
System.out.print(sound);
if (sound != null) {
mods.write(0, sound);
}
}
}
} else {
org.bukkit.entity.Entity entity = event.getPacket().getEntityModifier(observer.getWorld())
.read(0);
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 {
event.setCancelled(true); event.setCancelled(true);
DisguiseAPI.disguiseToPlayer(watched, observer, disguise); DisguiseAPI.disguiseToPlayer(watched, observer, disguise);
} }
} else { } else {
event.setCancelled(true); // Set the sounds and cancel bad packets.
DisguiseAPI.disguiseToPlayer(watched, observer, disguise); if (disguise.getType().isMisc()) {
} if (event.getPacketID() == Packets.Server.ARM_ANIMATION) {
} else if (!disguise.getType().isPlayer()) { event.setCancelled(true);
if (disguise.hasWatcher()) { }
mods.write(1, disguise.getWatcher().convert((List<WatchableObject>) mods.read(1))); }
} }
} }
} }