Experimental build for client interaction/weapon damage - Needs testing and thread safety, pushed due to time constraints. #368

This commit is contained in:
libraryaddict 2019-06-22 22:06:59 +12:00
parent 7591e3537f
commit a46178b04c
3 changed files with 78 additions and 37 deletions

View File

@ -1826,6 +1826,47 @@ public class DisguiseUtilities {
} }
} }
public static Disguise getDisguise(Player observer, int entityId) {
// If the entity ID is the same as self disguises id, then it needs to be set to the observers id
if (entityId == DisguiseAPI.getSelfDisguiseId()) {
entityId = observer.getEntityId();
}
// TODO Needs to be thread safe, not thread safe atm due to testing
if (getFutureDisguises().containsKey(entityId)) {
HashSet<TargetedDisguise> hashSet = getFutureDisguises().get(entityId);
for (TargetedDisguise dis : hashSet) {
if (!dis.canSee(observer) || !dis.isDisguiseInUse()) {
continue;
}
return dis;
}
}
for (HashSet<TargetedDisguise> disguises : getDisguises().values()) {
for (TargetedDisguise dis : disguises) {
if (dis.getEntity() == null || !dis.isDisguiseInUse()) {
continue;
}
if (dis.getEntity().getEntityId() != entityId) {
continue;
}
if (!dis.canSee(observer)) {
continue;
}
return dis;
}
}
return null;
}
public static Entity getEntity(World world, int entityId) { public static Entity getEntity(World world, int entityId) {
for (Entity e : world.getEntities()) { for (Entity e : world.getEntities()) {
if (e.getEntityId() != entityId) { if (e.getEntityId() != entityId) {

View File

@ -41,47 +41,54 @@ public class PacketListenerClientInteract extends PacketAdapter {
PacketContainer packet = event.getPacket(); PacketContainer packet = event.getPacket();
Entity entity = DisguiseUtilities.getEntity(observer.getWorld(), packet.getIntegers().read(0)); final Disguise disguise = DisguiseUtilities.getDisguise(event.getPlayer(), packet.getIntegers().read(0));
if (entity == null) { if (disguise == null) {
return; return;
} }
if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow || if (disguise.getEntity() == observer) {
entity == observer) {
event.setCancelled(true);
} else if (packet.getIntegers().read(0) == DisguiseAPI.getSelfDisguiseId()) {
// If it's a self-interact // If it's a self-interact
event.setCancelled(true); event.setCancelled(true);
Disguise disguise = DisguiseAPI.getDisguise(observer, observer); // The type of interact, we don't care the difference with "Interact_At" however as it's not
// useful
if (disguise != null) {
// The type of interact, we don't care the difference with "Interact_At" however as it's not useful
// for self disguises // for self disguises
EnumWrappers.EntityUseAction interactType = packet.getEntityUseActions().read(0); EnumWrappers.EntityUseAction interactType = packet.getEntityUseActions().read(0);
EquipmentSlot handUsed = EquipmentSlot.HAND; final EquipmentSlot handUsed;
// Attack has a null hand, which throws an error if you attempt to fetch // Attack has a null hand, which throws an error if you attempt to fetch
if (interactType != EnumWrappers.EntityUseAction.ATTACK) {
// If the hand used wasn't their main hand // If the hand used wasn't their main hand
if (packet.getHands().read(0) == EnumWrappers.Hand.OFF_HAND) { if (interactType != EnumWrappers.EntityUseAction.ATTACK &&
packet.getHands().read(0) == EnumWrappers.Hand.OFF_HAND) {
handUsed = EquipmentSlot.OFF_HAND; handUsed = EquipmentSlot.OFF_HAND;
} else {
handUsed = EquipmentSlot.HAND;
} }
}
DisguiseInteractEvent selfEvent = new DisguiseInteractEvent((TargetedDisguise) disguise, handUsed,
interactType == EnumWrappers.EntityUseAction.ATTACK);
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
// Fire self interact event
DisguiseInteractEvent selfEvent = new DisguiseInteractEvent((TargetedDisguise) disguise, handUsed,
interactType == EnumWrappers.EntityUseAction.ATTACK);
Bukkit.getPluginManager().callEvent(selfEvent); Bukkit.getPluginManager().callEvent(selfEvent);
} }
}.runTask(LibsDisguises.getInstance()); }.runTask(LibsDisguises.getInstance());
} else {
Entity entity = disguise.getEntity();
if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow) {
event.setCancelled(true);
} }
} }
if (disguise.getType() != DisguiseType.SHEEP && disguise.getType() != DisguiseType.WOLF) {
return;
}
// If this is something the player can dye the disguise with
for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(), for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(),
observer.getInventory().getItemInOffHand()}) { observer.getInventory().getItemInOffHand()}) {
if (item == null) { if (item == null) {
@ -94,13 +101,6 @@ public class PacketListenerClientInteract extends PacketAdapter {
continue; continue;
} }
Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
if (disguise == null ||
(disguise.getType() != DisguiseType.SHEEP && disguise.getType() != DisguiseType.WOLF)) {
continue;
}
if (disguise.getType() == DisguiseType.SHEEP) { if (disguise.getType() == DisguiseType.SHEEP) {
SheepWatcher watcher = (SheepWatcher) disguise.getWatcher(); SheepWatcher watcher = (SheepWatcher) disguise.getWatcher();

View File

@ -249,13 +249,13 @@ public class PacketListenerSounds extends PacketAdapter {
} }
// It made a damage animation // It made a damage animation
Entity entity = DisguiseUtilities.getEntity(observer.getWorld(), event.getPacket().getIntegers().read(0)); Disguise disguise = DisguiseUtilities.getDisguise(observer, event.getPacket().getIntegers().read(0));
if (entity == null) { if (disguise == null) {
return; return;
} }
Disguise disguise = DisguiseAPI.getDisguise(observer, entity); Entity entity = disguise.getEntity();
if (disguise != null && !disguise.getType().isPlayer() && if (disguise != null && !disguise.getType().isPlayer() &&
(disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) { (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) {