Self disguises now behave when riding something

This commit is contained in:
Andrew 2013-11-06 06:09:44 +13:00
parent acd2da49f8
commit ef7d1e936f
3 changed files with 140 additions and 114 deletions

View File

@ -264,7 +264,7 @@ public class DisguiseAPI {
// Remove the old disguise, else we have weird disguises around the place // Remove the old disguise, else we have weird disguises around the place
removeSelfDisguise(player); removeSelfDisguise(player);
// If the disguised player can't see himself. Return // If the disguised player can't see himself. Return
if (!disguise.viewSelfDisguise() || !PacketsManager.isViewDisguisesListenerEnabled()) if (!disguise.viewSelfDisguise() || !PacketsManager.isViewDisguisesListenerEnabled() || player.getVehicle() != null)
return; return;
try { try {
// Grab the entity ID the fake disguise will use // Grab the entity ID the fake disguise will use

View File

@ -2,19 +2,20 @@ package me.libraryaddict.disguise;
import java.util.HashMap; import java.util.HashMap;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.vehicle.VehicleEnterEvent;
import org.bukkit.event.vehicle.VehicleExitEvent;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
public class DisguiseListener implements Listener { public class DisguiseListener implements Listener {
private String currentVersion; private String currentVersion;
@ -26,6 +27,7 @@ public class DisguiseListener implements Listener {
private String updateMessage = ChatColor.RED + "[LibsDisguises] " + ChatColor.DARK_RED private String updateMessage = ChatColor.RED + "[LibsDisguises] " + ChatColor.DARK_RED
+ "There is a update ready to be downloaded! You are using " + ChatColor.RED + "v%s" + ChatColor.DARK_RED + "There is a update ready to be downloaded! You are using " + ChatColor.RED + "v%s" + ChatColor.DARK_RED
+ ", the new version is " + ChatColor.RED + "%s" + ChatColor.DARK_RED + "!"; + ", the new version is " + ChatColor.RED + "%s" + ChatColor.DARK_RED + "!";
private DisguiseAPI disguiseAPI = new DisguiseAPI();
public DisguiseListener(LibsDisguises libsDisguises) { public DisguiseListener(LibsDisguises libsDisguises) {
plugin = libsDisguises; plugin = libsDisguises;
@ -56,6 +58,26 @@ public class DisguiseListener implements Listener {
} }
} }
@EventHandler(priority = EventPriority.MONITOR)
public void onVechileLeave(VehicleExitEvent event) {
final Disguise disguise = DisguiseAPI.getDisguise(event.getExited());
if (disguise != null) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
disguiseAPI.setupFakeDisguise(disguise);
}
});
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onVechileEnter(VehicleEnterEvent event) {
Disguise disguise = DisguiseAPI.getDisguise(event.getEntered());
if (disguise != null && event.getEntered() instanceof Player) {
disguiseAPI.removeVisibleDisguise((Player) event.getEntered());
}
}
@EventHandler @EventHandler
public void onJoin(PlayerJoinEvent event) { public void onJoin(PlayerJoinEvent event) {
Player p = event.getPlayer(); Player p = event.getPlayer();

View File

@ -697,7 +697,7 @@ public class PacketsManager {
@Override @Override
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
// If the inventory is the players inventory // If the inventory is the players inventory
if (event.getPacket().getIntegers().read(0) == 0) { if (event.getPlayer().getVehicle() == null && event.getPacket().getIntegers().read(0) == 0) {
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer()); Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer());
// If the player is disguised, views self disguises and is hiding a item. // If the player is disguised, views self disguises and is hiding a item.
if (disguise != null && disguise.viewSelfDisguise() if (disguise != null && disguise.viewSelfDisguise()
@ -786,36 +786,19 @@ public class PacketsManager {
Packets.Client.BLOCK_ITEM_SWITCH, Packets.Client.SET_CREATIVE_SLOT, Packets.Client.WINDOW_CLICK) { Packets.Client.BLOCK_ITEM_SWITCH, Packets.Client.SET_CREATIVE_SLOT, Packets.Client.WINDOW_CLICK) {
@Override @Override
public void onPacketReceiving(final PacketEvent event) { public void onPacketReceiving(final PacketEvent event) {
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer()); if (event.getPlayer().getVehicle() == null) {
// If player is disguised, views self disguises and has a inventory modifier Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer());
if (disguise != null && disguise.viewSelfDisguise() // If player is disguised, views self disguises and has a inventory modifier
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) { if (disguise != null && disguise.viewSelfDisguise()
switch (event.getPacketID()) { && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) {
// If they are in creative and clicked on a slot switch (event.getPacketID()) {
case Packets.Client.SET_CREATIVE_SLOT: { // If they are in creative and clicked on a slot
int slot = event.getPacket().getIntegers().read(0); case Packets.Client.SET_CREATIVE_SLOT: {
if (slot >= 5 && slot <= 8) { int slot = event.getPacket().getIntegers().read(0);
if (disguise.isHidingArmorFromSelf()) { if (slot >= 5 && slot <= 8) {
int armorSlot = Math.abs(slot - 9); if (disguise.isHidingArmorFromSelf()) {
org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot]; int armorSlot = Math.abs(slot - 9);
if (item != null && item.getType() != Material.AIR) { org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot];
PacketContainer packet = new PacketContainer(Packets.Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, slot);
mods.write(2, CraftItemStack.asNMSCopy(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) { if (item != null && item.getType() != Material.AIR) {
PacketContainer packet = new PacketContainer(Packets.Server.SET_SLOT); PacketContainer packet = new PacketContainer(Packets.Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
@ -830,82 +813,58 @@ public class PacketsManager {
} }
} }
} }
} } else if (slot >= 36 && slot <= 44) {
} if (disguise.isHidingHeldItemFromSelf()) {
break; int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
} if (slot + 36 == currentSlot) {
// If the player switched item, aka he moved from slot 1 to slot 2 org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand();
case Packets.Client.BLOCK_ITEM_SWITCH: { if (item != null && item.getType() != Material.AIR) {
if (disguise.isHidingHeldItemFromSelf()) { PacketContainer packet = new PacketContainer(Packets.Server.SET_SLOT);
// From logging, it seems that both bukkit and nms uses the same thing for the slot switching. StructureModifier<Object> mods = packet.getModifier();
// 0 1 2 3 - 8 mods.write(0, 0);
// If the packet is coming, then I need to replace the item they are switching to mods.write(1, slot);
// As for the old item, I need to restore it. mods.write(2, CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(0)));
org.bukkit.inventory.ItemStack currentlyHeld = event.getPlayer().getItemInHand(); try {
// If his old weapon isn't air ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet,
if (currentlyHeld != null && currentlyHeld.getType() != Material.AIR) { false);
PacketContainer packet = new PacketContainer(Packets.Server.SET_SLOT); } catch (InvocationTargetException e) {
StructureModifier<Object> mods = packet.getModifier(); e.printStackTrace();
mods.write(0, 0); }
mods.write(1, event.getPlayer().getInventory().getHeldItemSlot() + 36); }
mods.write(2, CraftItemStack.asNMSCopy(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(Packets.Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, event.getPacket().getIntegers().read(0) + 36);
mods.write(2, CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(0)));
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
break;
}
case Packets.Client.WINDOW_CLICK: {
int slot = event.getPacket().getIntegers().read(1);
org.bukkit.inventory.ItemStack clickedItem;
if (event.getPacket().getIntegers().read(3) == 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().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
public void run() {
event.getPlayer().updateInventory();
} }
}); }
} }
return; break;
} 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 player switched item, aka he moved from slot 1 to slot 2
// If the slot is a armor slot case Packets.Client.BLOCK_ITEM_SWITCH: {
if (slot >= 5 && slot <= 8) { if (disguise.isHidingHeldItemFromSelf()) {
if (disguise.isHidingArmorFromSelf()) { // 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(Packets.Server.SET_SLOT); PacketContainer packet = new PacketContainer(Packets.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, event.getPlayer().getInventory().getHeldItemSlot() + 36);
mods.write(2, CraftItemStack.asNMSCopy(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(Packets.Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, event.getPacket().getIntegers().read(0) + 36);
mods.write(2, CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(0))); mods.write(2, CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(0)));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false);
@ -913,12 +872,38 @@ public class PacketsManager {
e.printStackTrace(); e.printStackTrace();
} }
} }
// Else if its a hotbar slot }
} else if (slot >= 36 && slot <= 44) { break;
if (disguise.isHidingHeldItemFromSelf()) { }
int currentSlot = event.getPlayer().getInventory().getHeldItemSlot();
// Check if the player is on the same slot as the slot that its setting case Packets.Client.WINDOW_CLICK: {
if (slot == currentSlot + 36) { int slot = event.getPacket().getIntegers().read(1);
org.bukkit.inventory.ItemStack clickedItem;
if (event.getPacket().getIntegers().read(3) == 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().scheduleSyncDelayedTask(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 (slot >= 5 && slot <= 8) {
if (disguise.isHidingArmorFromSelf()) {
PacketContainer packet = new PacketContainer(Packets.Server.SET_SLOT); PacketContainer packet = new PacketContainer(Packets.Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
@ -931,14 +916,33 @@ public class PacketsManager {
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(Packets.Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, slot);
mods.write(2, CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(0)));
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet,
false);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
} }
} }
break;
} }
break;
}
default: default:
break; break;
}
} }
} }
} }