diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java index 14dd9e6f1..09731f3e2 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java @@ -56,7 +56,7 @@ public final MethodResult useOnAnimal(@NotNull IArguments arguments) throws LuaE if (automataCore.hasAttribute(AutomataCorePeripheral.ATTR_STORING_TOOL_DURABILITY)) selectedTool.setDamageValue(previousDamageValue); - return MethodResult.of(true, result.toString()); + return MethodResult.of(result.consumesAction(), result.toString()); }); } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataSoulFeedingPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataSoulFeedingPlugin.java index 79368507e..6f4035522 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataSoulFeedingPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataSoulFeedingPlugin.java @@ -24,7 +24,7 @@ public final MethodResult feedSoul() { InteractionResult result = owner.withPlayer(APFakePlayer::useOnEntity); automataCore.addRotationCycle(3); - return MethodResult.of(true, result.toString()); + return MethodResult.of(result.consumesAction(), result.toString()); } @Override diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java index d6954e640..a947a4c53 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java @@ -44,7 +44,6 @@ import java.lang.ref.WeakReference; import java.util.List; -import java.util.Optional; import java.util.UUID; import java.util.function.Function; import java.util.function.Predicate; @@ -92,7 +91,6 @@ public boolean canAttack(@NotNull LivingEntity livingEntity) { public void openTextEdit(@NotNull SignBlockEntity sign) { } - @Override public boolean isSilent() { return true; @@ -318,46 +316,59 @@ public HitResult findHit(boolean skipEntity, boolean skipBlock, @Nullable Predic if (skipEntity) return blockHit; - List entities = level.getEntities(this, getBoundingBox().expandTowards(look.x * range, look.y * range, look.z * range).inflate(1, 1, 1), collidablePredicate); + List entities = level.getEntities(this, getBoundingBox().expandTowards(look.x * range, look.y * range, look.z * range).inflate(1), collidablePredicate); LivingEntity closestEntity = null; Vec3 closestVec = null; - double closestDistance = range; + double closestDistance = blockHit.getType() == HitResult.Type.MISS ? range * range : distanceToSqr(blockHit.getLocation()); for (Entity entityHit : entities) { - if (!(entityHit instanceof LivingEntity) || entityFilter != null && !entityFilter.test(entityHit)) + if (!(entityHit instanceof LivingEntity entity)) { + continue; + } + // TODO: maybe let entityFilter returns the priority of the entity, instead of only returns the closest one. + if (entityFilter != null && !entityFilter.test(entity)) { + continue; + } + + // Removed a lot logic here to make Automata cores interact like a player. + // However, the results for some edge cases may change. Need more review and tests. + + // Hit vehicle before passenger + if (entity.isPassenger()) { continue; - // Add litter bigger that just pick radius - AABB box = entityHit.getBoundingBox().inflate(entityHit.getPickRadius() + 0.5); - Optional clipResult = box.clip(origin, target); + } + AABB box = entity.getBoundingBox(); + Vec3 clipVec; if (box.contains(origin)) { - if (closestDistance >= 0.0D) { - closestEntity = (LivingEntity) entityHit; - closestVec = clipResult.orElse(origin); - closestDistance = 0.0D; + clipVec = origin; + } else { + clipVec = box.clip(origin, target).orElse(null); + if (clipVec == null) { + continue; } - } else if (clipResult.isPresent()) { - Vec3 clipVec = clipResult.get(); - double distance = origin.distanceTo(clipVec); - - if (distance < closestDistance || closestDistance == 0.0D) { - if (entityHit == entityHit.getRootVehicle() && !entityHit.canRiderInteract()) { - if (closestDistance == 0.0D) { - closestEntity = (LivingEntity) entityHit; - closestVec = clipVec; - } - } else { - closestEntity = (LivingEntity) entityHit; - closestVec = clipVec; - closestDistance = distance; - } + } + double distance = origin.distanceToSqr(clipVec); + // Ignore small enough distance + if (distance <= 1e-6) { + distance = 0; + } + if (distance > closestDistance) { + continue; + } + if (distance == closestDistance && closestEntity != null) { + // Hit larger entity before smaller + if (closestEntity.getBoundingBox().getSize() >= box.getSize()) { + continue; } } + closestEntity = entity; + closestVec = clipVec; + closestDistance = distance; } - if (closestEntity != null && closestDistance <= range && (blockHit.getType() == HitResult.Type.MISS || distanceToSqr(blockHit.getLocation()) > closestDistance * closestDistance)) { + if (closestEntity != null) { return new EntityHitResult(closestEntity, closestVec); - } else { - return blockHit; } + return blockHit; } }