Cleanup, fix a bug

This commit is contained in:
libraryaddict 2016-05-13 08:27:53 +12:00
parent 986dfe6246
commit eff6dc3025
6 changed files with 569 additions and 551 deletions

View File

@ -446,51 +446,48 @@ public class DisguiseUtilities
{ {
continue; continue;
} }
try
{
int chunkX = (int) Math.floor(loc.getX() / 16D) - 17, chunkZ = (int) Math.floor(loc.getZ() / 16D) - 17;
chunkX -= chunkX % 8; int chunkX = (int) Math.floor(loc.getX() / 16D) - 17, chunkZ = (int) Math.floor(loc.getZ() / 16D) - 17;
chunkZ -= chunkZ % 8;
xChunk.set(bedChunk, chunkX); chunkX -= chunkX % 8;
zChunk.set(bedChunk, chunkZ); chunkZ -= chunkZ % 8;
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
// Make unload packets // Make unload packets
try
{ packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.UNLOAD_CHUNK, chunkX, chunkZ)
packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.MAP_CHUNK, bedChunk, 0) .createPacket(bedChunk, 0);
.createPacket(bedChunk, 0);
}
catch (IllegalArgumentException ex)
{
try
{
packets[i] = ProtocolLibrary.getProtocolManager()
.createPacketConstructor(Server.MAP_CHUNK, bedChunk, true, 0, 40).createPacket(bedChunk, true, 0, 48);
}
catch (IllegalArgumentException ex1)
{
packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.MAP_CHUNK, bedChunk, true, 0)
.createPacket(bedChunk, true, 0);
}
}
i++; i++;
// Make load packets // Make load packets
if (oldLoc == null || i > 1) if (oldLoc == null || i > 1)
{ {
try
{
xChunk.set(bedChunk, chunkX);
zChunk.set(bedChunk, chunkZ);
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
// MAP_CHUNK_BULK was replaced in 1.9 with several seperated chunk packets // MAP_CHUNK_BULK was replaced in 1.9 with several seperated chunk packets
// packets[i] = ProtocolLibrary.getProtocolManager() // packets[i] = ProtocolLibrary.getProtocolManager()
// .createPacketConstructor(Server.MAP_CHUNK_BULK, Arrays.asList(bedChunk)) // .createPacketConstructor(Server.MAP_CHUNK_BULK, Arrays.asList(bedChunk))
// .createPacket(Arrays.asList(bedChunk)); // .createPacket(Arrays.asList(bedChunk));
packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.MAP_CHUNK, bedChunk, true, 0) // Make unload packets
.createPacket(bedChunk, true, 0); try
{
packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.MAP_CHUNK, bedChunk, 0)
.createPacket(bedChunk, 0);
}
catch (IllegalArgumentException ex)
{
packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.MAP_CHUNK, bedChunk, true, 0)
.createPacket(bedChunk, true, 0);
}
i++; i++;
} }
} }

View File

@ -53,28 +53,32 @@ public class PacketListenerClientInteract extends PacketAdapter
event.setCancelled(true); event.setCancelled(true);
} }
ItemStack item = observer.getItemInHand(); for (ItemStack item : new ItemStack[]
{
if (item != null && item.getType() == Material.INK_SACK) observer.getInventory().getItemInMainHand(), observer.getInventory().getItemInOffHand()
})
{ {
if (item == null || item.getType() != Material.INK_SACK)
continue;
Disguise disguise = DisguiseAPI.getDisguise(observer, entity); Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
if (disguise != null && (disguise.getType() == DisguiseType.SHEEP || disguise.getType() == DisguiseType.WOLF)) if (disguise == null || (disguise.getType() != DisguiseType.SHEEP && disguise.getType() != DisguiseType.WOLF))
continue;
AnimalColor color = AnimalColor.getColor(item.getDurability());
if (disguise.getType() == DisguiseType.SHEEP)
{ {
AnimalColor color = AnimalColor.getColor(item.getDurability()); SheepWatcher watcher = (SheepWatcher) disguise.getWatcher();
if (disguise.getType() == DisguiseType.SHEEP) watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor());
{ }
SheepWatcher watcher = (SheepWatcher) disguise.getWatcher(); else
{
WolfWatcher watcher = (WolfWatcher) disguise.getWatcher();
watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor()); watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor());
}
else
{
WolfWatcher watcher = (WolfWatcher) disguise.getWatcher();
watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor());
}
} }
} }
} }

View File

@ -41,123 +41,38 @@ public class PacketListenerInventory extends PacketAdapter
if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary
return; return;
if (!(event.getPlayer() instanceof com.comphenix.net.sf.cglib.proxy.Factory) && event.getPlayer().getVehicle() == null) if (event.getPlayer() instanceof com.comphenix.net.sf.cglib.proxy.Factory || event.getPlayer().getVehicle() != null)
{ {
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); return;
}
// If player is disguised, views self disguises and has a inventory modifier Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
if (disguise != null && disguise.isSelfDisguiseVisible()
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) // If player is disguised, views self disguises and has a inventory modifier
if (disguise != null && disguise.isSelfDisguiseVisible()
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()))
{
// If they are in creative and clicked on a slot
if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT)
{ {
// If they are in creative and clicked on a slot int slot = event.getPacket().getIntegers().read(0);
if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT)
if (slot >= 5 && slot <= 8)
{ {
int slot = event.getPacket().getIntegers().read(0); if (disguise.isHidingArmorFromSelf())
if (slot >= 5 && slot <= 8)
{ {
if (disguise.isHidingArmorFromSelf()) int armorSlot = Math.abs((slot - 5) - 3);
{
int armorSlot = Math.abs((slot - 5) - 3);
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot]; org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot];
if (item != null && item.getType() != Material.AIR) if (item != null && item.getType() != Material.AIR)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
}
else if (slot >= 36 && slot <= 44)
{
if (disguise.isHidingHeldItemFromSelf())
{
int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
if (slot + 36 == currentSlot)
{
org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand();
if (item != null && item.getType() != Material.AIR)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
}
}
}
// If the player switched item, aka he moved from slot 1 to slot 2
else if (event.getPacketType() == PacketType.Play.Client.HELD_ITEM_SLOT)
{
if (disguise.isHidingHeldItemFromSelf())
{
// From logging, it seems that both bukkit and nms uses the same thing for the slot switching.
// 0 1 2 3 - 8
// If the packet is coming, then I need to replace the item they are switching to
// As for the old item, I need to restore it.
org.bukkit.inventory.ItemStack currentlyHeld = event.getPlayer().getItemInHand();
// If his old weapon isn't air
if (currentlyHeld != null && currentlyHeld.getType() != Material.AIR)
{ {
PacketContainer packet = new PacketContainer(Server.SET_SLOT); PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
mods.write(1, event.getPlayer().getInventory().getHeldItemSlot() + 36); mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(currentlyHeld));
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
org.bukkit.inventory.ItemStack newHeld = event.getPlayer().getInventory()
.getItem(event.getPacket().getIntegers().read(0));
// If his new weapon isn't air either!
if (newHeld != null && newHeld.getType() != Material.AIR)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, event.getPacket().getIntegers().read(0) + 36);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
try try
@ -171,55 +86,21 @@ public class PacketListenerInventory extends PacketAdapter
} }
} }
} }
else if (event.getPacketType() == PacketType.Play.Client.WINDOW_CLICK) else if (slot >= 36 && slot <= 44)
{ {
int slot = event.getPacket().getIntegers().read(1); if (disguise.isHidingHeldItemFromSelf())
org.bukkit.inventory.ItemStack clickedItem;
if (event.getPacket().getShorts().read(0) == 1)
{ {
// Its a shift click int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
clickedItem = event.getPacket().getItemModifier().read(0);
if (clickedItem != null && clickedItem.getType() != Material.AIR) if (slot + 36 == currentSlot)
{ {
// Rather than predict the clients actions org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand();
// Lets just update the entire inventory..
Bukkit.getScheduler().runTask(libsDisguises, new Runnable()
{
public void run()
{
event.getPlayer().updateInventory();
}
});
}
return; if (item != null && item.getType() != Material.AIR)
}
else
{
// If its not a player inventory click
// Shift clicking is exempted for the item in hand..
if (event.getPacket().getIntegers().read(0) != 0)
{
return;
}
clickedItem = event.getPlayer().getItemOnCursor();
}
if (clickedItem != null && clickedItem.getType() != Material.AIR)
{
// If the slot is a armor slot
if (slot >= 5 && slot <= 8)
{
if (disguise.isHidingArmorFromSelf())
{ {
PacketContainer packet = new PacketContainer(Server.SET_SLOT); PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
mods.write(1, slot); mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
@ -233,83 +114,126 @@ public class PacketListenerInventory extends PacketAdapter
e.printStackTrace(); e.printStackTrace();
} }
} }
// Else if its a hotbar slot
}
else if (slot >= 36 && slot <= 44)
{
if (disguise.isHidingHeldItemFromSelf())
{
int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
// Check if the player is on the same slot as the slot that its setting
if (slot == currentSlot + 36)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
} }
} }
} }
} }
} // If the player switched item, aka he moved from slot 1 to slot 2
} else if (event.getPacketType() == PacketType.Play.Client.HELD_ITEM_SLOT)
@Override
public void onPacketSending(PacketEvent event)
{
// If the inventory is the players inventory
if (!(event.getPlayer() instanceof com.comphenix.net.sf.cglib.proxy.Factory) && event.getPlayer().getVehicle() == null
&& event.getPacket().getIntegers().read(0) == 0)
{
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
// If the player is disguised, views self disguises and is hiding a item.
if (disguise != null && disguise.isSelfDisguiseVisible()
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()))
{ {
// If the server is setting the slot if (disguise.isHidingHeldItemFromSelf())
// Need to set it to air if its in a place it shouldn't be.
// Things such as picking up a item, spawned in item. Plugin sets the item. etc. Will fire this
/**
* Done
*/
if (event.getPacketType() == Server.SET_SLOT)
{ {
// The raw slot // From logging, it seems that both bukkit and nms uses the same thing for the slot switching.
// nms code has the start of the hotbar being 36. // 0 1 2 3 - 8
int slot = event.getPacket().getIntegers().read(1); // If the packet is coming, then I need to replace the item they are switching to
// As for the old item, I need to restore it.
org.bukkit.inventory.ItemStack currentlyHeld = event.getPlayer().getItemInHand();
// If his old weapon isn't air
if (currentlyHeld != null && currentlyHeld.getType() != Material.AIR)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, event.getPlayer().getInventory().getHeldItemSlot() + 36);
mods.write(2, ReflectionManager.getNmsItem(currentlyHeld));
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
org.bukkit.inventory.ItemStack newHeld = event.getPlayer().getInventory()
.getItem(event.getPacket().getIntegers().read(0));
// If his new weapon isn't air either!
if (newHeld != null && newHeld.getType() != Material.AIR)
{
PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, event.getPacket().getIntegers().read(0) + 36);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
}
else if (event.getPacketType() == PacketType.Play.Client.WINDOW_CLICK)
{
int slot = event.getPacket().getIntegers().read(1);
org.bukkit.inventory.ItemStack clickedItem;
if (event.getPacket().getShorts().read(0) == 1)
{
// Its a shift click
clickedItem = event.getPacket().getItemModifier().read(0);
if (clickedItem != null && clickedItem.getType() != Material.AIR)
{
// Rather than predict the clients actions
// Lets just update the entire inventory..
Bukkit.getScheduler().runTask(libsDisguises, new Runnable()
{
public void run()
{
event.getPlayer().updateInventory();
}
});
}
return;
}
else
{
// If its not a player inventory click
// Shift clicking is exempted for the item in hand..
if (event.getPacket().getIntegers().read(0) != 0)
{
return;
}
clickedItem = event.getPlayer().getItemOnCursor();
}
if (clickedItem != null && clickedItem.getType() != Material.AIR)
{
// If the slot is a armor slot // If the slot is a armor slot
if (slot >= 5 && slot <= 8) if (slot >= 5 && slot <= 8)
{ {
if (disguise.isHidingArmorFromSelf()) if (disguise.isHidingArmorFromSelf())
{ {
// Get the bukkit armor slot! PacketContainer packet = new PacketContainer(Server.SET_SLOT);
int armorSlot = Math.abs((slot - 5) - 3);
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot]; StructureModifier<Object> mods = packet.getModifier();
if (item != null && item.getType() != Material.AIR) mods.write(0, 0);
mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
try
{ {
event.setPacket(event.getPacket().shallowClone()); ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
}
event.getPacket().getModifier().write(2, catch (InvocationTargetException e)
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); {
e.printStackTrace();
} }
} }
// Else if its a hotbar slot // Else if its a hotbar slot
@ -323,68 +247,150 @@ public class PacketListenerInventory extends PacketAdapter
// Check if the player is on the same slot as the slot that its setting // Check if the player is on the same slot as the slot that its setting
if (slot == currentSlot + 36) if (slot == currentSlot + 36)
{ {
org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand(); PacketContainer packet = new PacketContainer(Server.SET_SLOT);
if (item != null && item.getType() != Material.AIR) StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
try
{ {
event.setPacket(event.getPacket().shallowClone()); ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
event.getPacket().getModifier().write(2, }
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); catch (InvocationTargetException e)
{
e.printStackTrace();
} }
} }
} }
} }
} }
else if (event.getPacketType() == Server.WINDOW_ITEMS)
{
event.setPacket(event.getPacket().deepClone());
StructureModifier<ItemStack[]> mods = event.getPacket().getItemArrayModifier();
ItemStack[] items = mods.read(0);
for (int slot = 0; slot < items.length; slot++)
{
if (slot >= 5 && slot <= 8)
{
if (disguise.isHidingArmorFromSelf())
{
// Get the bukkit armor slot!
int armorSlot = Math.abs((slot - 5) - 3);
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory()
.getArmorContents()[armorSlot];
if (item != null && item.getType() != Material.AIR)
{
items[slot] = new org.bukkit.inventory.ItemStack(0);
}
}
// Else if its a hotbar slot
}
else if (slot >= 36 && slot <= 44)
{
if (disguise.isHidingHeldItemFromSelf())
{
int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
// Check if the player is on the same slot as the slot that its setting
if (slot == currentSlot + 36)
{
org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand();
if (item != null && item.getType() != Material.AIR)
{
items[slot] = new org.bukkit.inventory.ItemStack(0);
}
}
}
}
}
mods.write(0, items);
}
} }
} }
}
@Override
public void onPacketSending(PacketEvent event)
{
// If the inventory is the players inventory
if (event.getPlayer() instanceof com.comphenix.net.sf.cglib.proxy.Factory || event.getPlayer().getVehicle() != null
|| event.getPacket().getIntegers().read(0) != 0)
{
return;
}
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
if (disguise == null || !disguise.isSelfDisguiseVisible()
|| (!disguise.isHidingArmorFromSelf() && !disguise.isHidingHeldItemFromSelf()))
{
return;
}
// If the player is disguised, views self disguises and is hiding a item.
// If the server is setting the slot
// Need to set it to air if its in a place it shouldn't be.
// Things such as picking up a item, spawned in item. Plugin sets the item. etc. Will fire this
/**
* Done
*/
if (event.getPacketType() == Server.SET_SLOT)
{
// The raw slot
// nms code has the start of the hotbar being 36.
int slot = event.getPacket().getIntegers().read(1);
// If the slot is a armor slot
if (slot >= 5 && slot <= 8)
{
if (disguise.isHidingArmorFromSelf())
{
// Get the bukkit armor slot!
int armorSlot = Math.abs((slot - 5) - 3);
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot];
if (item != null && item.getType() != Material.AIR)
{
event.setPacket(event.getPacket().shallowClone());
event.getPacket().getModifier().write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
}
}
// Else if its a hotbar slot
}
else if (slot >= 36 && slot <= 44)
{
if (disguise.isHidingHeldItemFromSelf())
{
int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
// Check if the player is on the same slot as the slot that its setting
if (slot == currentSlot + 36)
{
org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand();
if (item != null && item.getType() != Material.AIR)
{
event.setPacket(event.getPacket().shallowClone());
event.getPacket().getModifier().write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)));
}
}
}
}
}
else if (event.getPacketType() == Server.WINDOW_ITEMS)
{
event.setPacket(event.getPacket().deepClone());
StructureModifier<ItemStack[]> mods = event.getPacket().getItemArrayModifier();
ItemStack[] items = mods.read(0);
for (int slot = 0; slot < items.length; slot++)
{
if (slot >= 5 && slot <= 8)
{
if (disguise.isHidingArmorFromSelf())
{
// Get the bukkit armor slot!
int armorSlot = Math.abs((slot - 5) - 3);
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot];
if (item != null && item.getType() != Material.AIR)
{
items[slot] = new org.bukkit.inventory.ItemStack(0);
}
}
// Else if its a hotbar slot
}
else if (slot >= 36 && slot <= 44)
{
if (disguise.isHidingHeldItemFromSelf())
{
int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
// Check if the player is on the same slot as the slot that its setting
if (slot == currentSlot + 36)
{
org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand();
if (item != null && item.getType() != Material.AIR)
{
items[slot] = new org.bukkit.inventory.ItemStack(0);
}
}
}
}
}
mods.write(0, items);
}
} }
} }

View File

@ -53,45 +53,50 @@ public class PacketListenerMain extends PacketAdapter
PacketContainer[][] packets = PacketsManager.transformPacket(event.getPacket(), event.getPlayer(), entity); PacketContainer[][] packets = PacketsManager.transformPacket(event.getPacket(), event.getPlayer(), entity);
if (packets != null) if (packets == null)
{ {
event.setCancelled(true); return;
try
{
for (PacketContainer packet : packets[0])
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
final PacketContainer[] delayed = packets[1];
if (delayed.length > 0)
{
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable()
{
public void run()
{
try
{
for (PacketContainer packet : delayed)
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}, 2);
}
}
catch (InvocationTargetException ex)
{
ex.printStackTrace();
}
} }
event.setCancelled(true);
try
{
for (PacketContainer packet : packets[0])
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
final PacketContainer[] delayed = packets[1];
if (delayed.length == 0)
{
return;
}
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable()
{
public void run()
{
try
{
for (PacketContainer packet : delayed)
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}, 2);
}
catch (InvocationTargetException ex)
{
ex.printStackTrace();
}
} }
} }

View File

@ -286,116 +286,118 @@ public class PacketListenerSounds extends PacketAdapter
} }
else if (event.getPacketType() == Server.ENTITY_STATUS) else if (event.getPacketType() == Server.ENTITY_STATUS)
{ {
if ((byte) mods.read(1) == 2) if ((byte) mods.read(1) != 2)
{ {
// It made a damage animation return;
Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0); }
Disguise disguise = DisguiseAPI.getDisguise(observer, entity); // It made a damage animation
Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0);
if (disguise != null && !disguise.getType().isPlayer() Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
&& (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer()))
if (disguise != null && !disguise.getType().isPlayer()
&& (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer()))
{
DisguiseSound disSound = DisguiseSound.getType(entity.getType().name());
if (disSound == null)
return;
SoundType soundType = null;
Object obj = null;
if (entity instanceof LivingEntity)
{ {
DisguiseSound disSound = DisguiseSound.getType(entity.getType().name()); try
if (disSound == null)
return;
SoundType soundType = null;
Object obj = null;
if (entity instanceof LivingEntity)
{ {
try obj = LivingEntity.class.getMethod("getHealth").invoke(entity);
{
obj = LivingEntity.class.getMethod("getHealth").invoke(entity);
if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0)
{
soundType = SoundType.DEATH;
}
else
{
obj = null;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (obj == null)
{
soundType = SoundType.HURT;
}
if (disSound.getSound(soundType) == null
|| (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()))
{
if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())
{
cancelSound = !cancelSound;
if (cancelSound)
return;
}
disSound = DisguiseSound.getType(disguise.getType().name());
if (disSound != null)
{
String sound = disSound.getSound(soundType);
if (sound != null)
{
Location loc = entity.getLocation();
PacketContainer packet = new PacketContainer(Server.NAMED_SOUND_EFFECT);
mods = packet.getModifier();
Object craftSoundEffect = ReflectionManager.getCraftSoundEffect(sound);
mods.write(0, craftSoundEffect);
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); // Meh
mods.write(2, (int) (loc.getX() * 8D));
mods.write(3, (int) (loc.getY() * 8D));
mods.write(4, (int) (loc.getZ() * 8D));
mods.write(5, disSound.getDamageAndIdleSoundVolume());
float pitch;
if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult())
{ {
soundType = SoundType.DEATH; pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F
+ 1.5F;
} }
else else
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F
+ 1.0F;
if (disguise.getType() == DisguiseType.BAT)
pitch *= 95F;
pitch *= 63;
if (pitch < 0)
pitch = 0;
if (pitch > 255)
pitch = 255;
mods.write(6, (int) pitch);
try
{ {
obj = null; ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
} }
} catch (InvocationTargetException e)
catch (Exception e)
{
e.printStackTrace();
}
}
if (obj == null)
{
soundType = SoundType.HURT;
}
if (disSound.getSound(soundType) == null
|| (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()))
{
if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())
{
cancelSound = !cancelSound;
if (cancelSound)
return;
}
disSound = DisguiseSound.getType(disguise.getType().name());
if (disSound != null)
{
String sound = disSound.getSound(soundType);
if (sound != null)
{ {
Location loc = entity.getLocation(); e.printStackTrace();
PacketContainer packet = new PacketContainer(Server.NAMED_SOUND_EFFECT);
mods = packet.getModifier();
Object craftSoundEffect = ReflectionManager.getCraftSoundEffect(sound);
mods.write(0, craftSoundEffect);
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); // Meh
mods.write(2, (int) (loc.getX() * 8D));
mods.write(3, (int) (loc.getY() * 8D));
mods.write(4, (int) (loc.getZ() * 8D));
mods.write(5, disSound.getDamageAndIdleSoundVolume());
float pitch;
if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult())
{
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F
+ 1.5F;
}
else
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F
+ 1.0F;
if (disguise.getType() == DisguiseType.BAT)
pitch *= 95F;
pitch *= 63;
if (pitch < 0)
pitch = 0;
if (pitch > 255)
pitch = 255;
mods.write(6, (int) pitch);
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
} }
} }
} }

View File

@ -47,141 +47,145 @@ public class PacketListenerViewDisguises extends PacketAdapter
if (observer.getName().contains("UNKNOWN[")) // If the player is temporary if (observer.getName().contains("UNKNOWN[")) // If the player is temporary
return; return;
if (event.getPacket().getIntegers().read(0) == observer.getEntityId()) if (event.getPacket().getIntegers().read(0) != observer.getEntityId())
{ {
if (DisguiseAPI.isSelfDisguised(observer)) return;
}
if (!DisguiseAPI.isSelfDisguised(observer))
{
return;
}
// Here I grab the packets to convert them to, So I can display them as if the disguise sent them.
PacketContainer[][] transformed = PacketsManager.transformPacket(event.getPacket(), observer, observer);
PacketContainer[] packets = transformed == null ? null : transformed[0];
final PacketContainer[] delayedPackets = transformed == null ? null : transformed[1];
if (packets == null)
{
packets = new PacketContainer[]
{
event.getPacket()
};
}
for (PacketContainer packet : packets)
{
if (packet.getType() != Server.PLAYER_INFO)
{ {
// Here I grab the packets to convert them to, So I can display them as if the disguise sent them. if (packet.equals(event.getPacket()))
PacketContainer[][] transformed = PacketsManager.transformPacket(event.getPacket(), observer, observer);
PacketContainer[] packets = transformed == null ? null : transformed[0];
final PacketContainer[] delayedPackets = transformed == null ? null : transformed[1];
if (packets == null)
{ {
packets = new PacketContainer[] packet = packet.shallowClone();
{
event.getPacket()
};
} }
for (PacketContainer packet : packets) packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
}
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
if (delayedPackets != null && delayedPackets.length > 0)
{
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable()
{
public void run()
{ {
if (packet.getType() != Server.PLAYER_INFO)
{
if (packet.equals(event.getPacket()))
{
packet = packet.shallowClone();
}
packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
}
try try
{ {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); for (PacketContainer packet : delayedPackets)
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
} }
catch (InvocationTargetException e) catch (InvocationTargetException e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
} }
}, 2);
}
if (delayedPackets != null && delayedPackets.length > 0) if (event.getPacketType() == Server.ENTITY_METADATA)
{
event.setPacket(event.getPacket().deepClone());
for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0))
{
if (watch.getIndex() == 0)
{ {
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() byte b = (byte) watch.getValue();
{
public void run() byte a = (byte) (b | 1 << 5);
{
try if ((b & 1 << 3) != 0)
{ a = (byte) (a | 1 << 3);
for (PacketContainer packet : delayedPackets)
{ watch.setValue(a);
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}, 2);
} }
}
}
else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN)
{
event.setCancelled(true);
if (event.getPacketType() == Server.ENTITY_METADATA) PacketContainer packet = new PacketContainer(Server.ENTITY_METADATA);
{
event.setPacket(event.getPacket().deepClone());
for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0)) StructureModifier<Object> mods = packet.getModifier();
{
if (watch.getIndex() == 0)
{
byte b = (byte) watch.getValue();
byte a = (byte) (b | 1 << 5); mods.write(0, observer.getEntityId());
if ((b & 1 << 3) != 0) List<WrappedWatchableObject> watchableList = new ArrayList<>();
a = (byte) (a | 1 << 3); Byte b = 1 << 5;
watch.setValue(a); if (observer.isSprinting())
} b = (byte) (b | 1 << 3);
}
}
else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN)
{
event.setCancelled(true);
PacketContainer packet = new PacketContainer(Server.ENTITY_METADATA); WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(0, b));
StructureModifier<Object> mods = packet.getModifier(); watchableList.add(watch);
packet.getWatchableCollectionModifier().write(0, watchableList);
mods.write(0, observer.getEntityId()); try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
else if (event.getPacketType() == Server.ANIMATION)
{
if (event.getPacket().getIntegers().read(1) != 2)
{
event.setCancelled(true);
}
}
else if (event.getPacketType() == Server.ATTACH_ENTITY || event.getPacketType() == Server.REL_ENTITY_MOVE
|| event.getPacketType() == Server.REL_ENTITY_MOVE_LOOK || event.getPacketType() == Server.ENTITY_LOOK
|| event.getPacketType() == Server.ENTITY_TELEPORT || event.getPacketType() == Server.ENTITY_HEAD_ROTATION
|| event.getPacketType() == Server.ENTITY_EFFECT || event.getPacketType() == Server.ENTITY_EQUIPMENT)
{
event.setCancelled(true);
}
else if (event.getPacketType() == Server.ENTITY_STATUS)
{
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
List<WrappedWatchableObject> watchableList = new ArrayList<>(); if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer()
Byte b = 1 << 5; && event.getPacket().getBytes().read(0) == 2)
{
if (observer.isSprinting()) event.setCancelled(true);
b = (byte) (b | 1 << 3);
WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(0, b));
watchableList.add(watch);
packet.getWatchableCollectionModifier().write(0, watchableList);
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet);
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
else if (event.getPacketType() == Server.ANIMATION)
{
if (event.getPacket().getIntegers().read(1) != 2)
{
event.setCancelled(true);
}
}
else if (event.getPacketType() == Server.ATTACH_ENTITY || event.getPacketType() == Server.REL_ENTITY_MOVE
|| event.getPacketType() == Server.REL_ENTITY_MOVE_LOOK || event.getPacketType() == Server.ENTITY_LOOK
|| event.getPacketType() == Server.ENTITY_TELEPORT || event.getPacketType() == Server.ENTITY_HEAD_ROTATION
|| event.getPacketType() == Server.ENTITY_EFFECT || event.getPacketType() == Server.ENTITY_EQUIPMENT)
{
event.setCancelled(true);
}
else if (event.getPacketType() == Server.ENTITY_STATUS)
{
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer()
&& event.getPacket().getBytes().read(0) == 2)
{
event.setCancelled(true);
}
}
} }
} }
} }