Skip to content

Instantly share code, notes, and snippets.

@cindyker
Created April 2, 2018 11:18
Show Gist options
  • Save cindyker/20c1fc3f716389d774d1a9a3a3e4d731 to your computer and use it in GitHub Desktop.
Save cindyker/20c1fc3f716389d774d1a9a3a3e4d731 to your computer and use it in GitHub Desktop.
//Part of CraftBukkit
public void a(PacketPlayInUseEntity packetplayinuseentity) {
PlayerConnectionUtils.ensureMainThread(packetplayinuseentity, this, this.player.x());
if (this.player.isFrozen()) return; // CraftBukkit
WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
Entity entity = packetplayinuseentity.a((World) worldserver);
this.player.resetIdleTimer();
if (entity != null) {
boolean flag = this.player.hasLineOfSight(entity);
double d0 = 36.0D;
if (!flag) {
d0 = 9.0D;
}
if (this.player.h(entity) < d0) {
EnumHand enumhand;
ItemStack itemInHand = this.player.b(packetplayinuseentity.b() == null ? EnumHand.MAIN_HAND : packetplayinuseentity.b()); // CraftBukkit
if (packetplayinuseentity.a() == PacketPlayInUseEntity.EnumEntityUseAction.INTERACT
|| packetplayinuseentity.a() == PacketPlayInUseEntity.EnumEntityUseAction.INTERACT_AT) {
// CraftBukkit start
boolean triggerLeashUpdate = itemInHand != null && itemInHand.getItem() == Items.LEAD && entity instanceof EntityInsentient;
Item origItem = this.player.inventory.getItemInHand() == null ? null : this.player.inventory.getItemInHand().getItem();
PlayerInteractEntityEvent event;
if (packetplayinuseentity.a() == PacketPlayInUseEntity.EnumEntityUseAction.INTERACT) {
event = new PlayerInteractEntityEvent((Player) this.getPlayer(), entity.getBukkitEntity(), (packetplayinuseentity.b() == EnumHand.OFF_HAND) ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND);
} else {
Vec3D target = packetplayinuseentity.c();
event = new PlayerInteractAtEntityEvent((Player) this.getPlayer(), entity.getBukkitEntity(), new org.bukkit.util.Vector(target.x, target.y, target.z), (packetplayinuseentity.b() == EnumHand.OFF_HAND) ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND);
}
this.server.getPluginManager().callEvent(event);
if (triggerLeashUpdate && (event.isCancelled() || this.player.inventory.getItemInHand() == null || this.player.inventory.getItemInHand().getItem() != Items.LEAD)) {
// Refresh the current leash state
this.sendPacket(new PacketPlayOutAttachEntity(entity, ((EntityInsentient) entity).getLeashHolder()));
}
if (event.isCancelled() || this.player.inventory.getItemInHand() == null || this.player.inventory.getItemInHand().getItem() != origItem) {
// Refresh the current entity metadata
this.sendPacket(new PacketPlayOutEntityMetadata(entity.getId(), entity.datawatcher, true));
}
if (event.isCancelled()) {
return;
}
// CraftBukkit end
}
if (packetplayinuseentity.a() == PacketPlayInUseEntity.EnumEntityUseAction.INTERACT) {
enumhand = packetplayinuseentity.b();
this.player.a(entity, enumhand);
// CraftBukkit start
if (!itemInHand.isEmpty() && itemInHand.getCount() <= -1) {
this.player.updateInventory(this.player.activeContainer);
}
// CraftBukkit end
} else if (packetplayinuseentity.a() == PacketPlayInUseEntity.EnumEntityUseAction.INTERACT_AT) {
enumhand = packetplayinuseentity.b();
entity.a(this.player, packetplayinuseentity.c(), enumhand);
// CraftBukkit start
if (!itemInHand.isEmpty() && itemInHand.getCount() <= -1) {
this.player.updateInventory(this.player.activeContainer);
}
// CraftBukkit end
} else if (packetplayinuseentity.a() == PacketPlayInUseEntity.EnumEntityUseAction.ATTACK) {
if (entity instanceof EntityItem || entity instanceof EntityExperienceOrb || entity instanceof EntityArrow || (entity == this.player && !player.isSpectator())) { // CraftBukkit
this.disconnect(new ChatMessage("multiplayer.disconnect.invalid_entity_attacked", new Object[0]));
this.minecraftServer.warning("Player " + this.player.getName() + " tried to attack an invalid entity");
return;
}
this.player.attack(entity);
// CraftBukkit start
if (!itemInHand.isEmpty() && itemInHand.getCount() <= -1) {
this.player.updateInventory(this.player.activeContainer);
}
// CraftBukkit end
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment