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
// for self disguises
EnumWrappers.EntityUseAction interactType = packet.getEntityUseActions().read(0);
final EquipmentSlot handUsed;
if (disguise != null) { // Attack has a null hand, which throws an error if you attempt to fetch
// The type of interact, we don't care the difference with "Interact_At" however as it's not useful // If the hand used wasn't their main hand
// for self disguises if (interactType != EnumWrappers.EntityUseAction.ATTACK &&
EnumWrappers.EntityUseAction interactType = packet.getEntityUseActions().read(0); packet.getHands().read(0) == EnumWrappers.Hand.OFF_HAND) {
EquipmentSlot handUsed = EquipmentSlot.HAND; handUsed = EquipmentSlot.OFF_HAND;
} else {
handUsed = EquipmentSlot.HAND;
}
// Attack has a null hand, which throws an error if you attempt to fetch new BukkitRunnable() {
if (interactType != EnumWrappers.EntityUseAction.ATTACK) { @Override
// If the hand used wasn't their main hand public void run() {
if (packet.getHands().read(0) == EnumWrappers.Hand.OFF_HAND) { // Fire self interact event
handUsed = EquipmentSlot.OFF_HAND; DisguiseInteractEvent selfEvent = new DisguiseInteractEvent((TargetedDisguise) disguise, handUsed,
} interactType == EnumWrappers.EntityUseAction.ATTACK);
Bukkit.getPluginManager().callEvent(selfEvent);
} }
}.runTask(LibsDisguises.getInstance());
} else {
Entity entity = disguise.getEntity();
DisguiseInteractEvent selfEvent = new DisguiseInteractEvent((TargetedDisguise) disguise, handUsed, if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow) {
interactType == EnumWrappers.EntityUseAction.ATTACK); event.setCancelled(true);
new BukkitRunnable() {
@Override
public void run() {
Bukkit.getPluginManager().callEvent(selfEvent);
}
}.runTask(LibsDisguises.getInstance());
} }
} }
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())) {