public class PlayerUseEntityHandler extends MessageHandler<PlayerUseEntityMessage> {
@Override
public void handleServer(ServerSession session, PlayerUseEntityMessage message) {
Player playerEnt = session.getPlayer();
Human player = playerEnt.get(Human.class);
Entity clickedEntity = playerEnt.getWorld().getEntity(message.getTarget());
if (clickedEntity == null || player == null) {
return;
}
Slot held = PlayerUtil.getHeldSlot(playerEnt);
if (held == null) {
return;
}
ItemStack holding = held.get();
Material holdingMat = holding == null ? VanillaMaterials.AIR : holding.getMaterial();
if (holdingMat == null) {
holdingMat = VanillaMaterials.AIR;
}
//TODO VanillaPlayerInteractEntityEvent maybe?
PlayerInteractEntityEvent event;
if (message.isPunching()) {
event = new PlayerInteractEntityEvent(playerEnt, clickedEntity, clickedEntity.getPhysics().getPosition(), Action.LEFT_CLICK);
if (Spout.getEventManager().callEvent(event).isCancelled()) {
return;
}
holdingMat.onInteract(playerEnt, clickedEntity, Action.LEFT_CLICK);
clickedEntity.interact(event);
if (clickedEntity.get(Human.class) != null && !VanillaConfiguration.PLAYER_PVP_ENABLED.getBoolean()) {
return;
}
Living clicked = clickedEntity.get(Living.class);
if (clicked != null) {
//TODO: Reimplement exhaustion values
int damage = 1;
if (holding != null && holdingMat instanceof VanillaMaterial) {
damage = ((VanillaMaterial) holdingMat).getDamage();
if (holdingMat instanceof Tool) {
// This is a bit of a hack due to the way Tool hierarchy is now (Only Swords can have a damage modifier, but Sword must be an interface and therefore is not able to contain getDamageModifier without code duplication)
damage += ((Tool) holdingMat).getDamageBonus(clickedEntity, holding);
// player.getInventory().getQuickbar().getCurrentSlotInventory().addData(1); TODO: Reimplement durability change
}
}
//Potion modification
if (holdingMat.equals(VanillaMaterials.AIR)) {
Effects effect = playerEnt.add(Effects.class);
if (effect.contains(EntityEffectType.STRENGTH)) {
damage += 3;
}
if (effect.contains(EntityEffectType.WEAKNESS)) {
damage -= 2;
}
}
//END Potion modification
// Damage the clicked entity
if (damage > 0 && !PlayerUtil.isCreativePlayer(clickedEntity) && !clicked.getHealth().isDead()) {
clicked.getHealth().damage(damage, new PlayerDamageCause(playerEnt, DamageType.ATTACK), damage > 0);
}
}
} else {
event = new PlayerInteractEntityEvent(playerEnt, clickedEntity, clickedEntity.getPhysics().getPosition(), Action.LEFT_CLICK);
if (Spout.getEventManager().callEvent(event).isCancelled()) {
return;
}
holdingMat.onInteract(playerEnt, clickedEntity, Action.RIGHT_CLICK);
clickedEntity.interact(event);
}
}