From a7c52ed1b2c9d3211ee4eb7633d15c4ddc783809 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Mon, 19 Aug 2024 13:42:46 -0600 Subject: [PATCH 1/3] make some change to fake player --- .../common/util/fakeplayer/APFakePlayer.java | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) 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..795a7fb3e 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 @@ -318,7 +318,7 @@ 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; @@ -328,29 +328,20 @@ public HitResult findHit(boolean skipEntity, boolean skipBlock, @Nullable Predic continue; // Add litter bigger that just pick radius AABB box = entityHit.getBoundingBox().inflate(entityHit.getPickRadius() + 0.5); - Optional clipResult = box.clip(origin, target); - if (box.contains(origin)) { - if (closestDistance >= 0.0D) { - closestEntity = (LivingEntity) entityHit; - closestVec = clipResult.orElse(origin); - closestDistance = 0.0D; - } - } else if (clipResult.isPresent()) { + closestEntity = (LivingEntity) entityHit; + closestVec = clipResult.orElse(origin); + closestDistance = 0; + break; + } + Optional clipResult = box.clip(origin, target); + 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; - } + if (distance < closestDistance) { + closestEntity = (LivingEntity) entityHit; + closestVec = clipVec; + closestDistance = distance; } } } From fc79c30f5b6a17cb90b2e2464b965c9a2383b529 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Sat, 24 Aug 2024 13:14:57 -0600 Subject: [PATCH 2/3] fix entity operation does not have enough reach range than expected --- .../plugins/AutomataEntityHandPlugin.java | 2 +- .../plugins/AutomataSoulFeedingPlugin.java | 2 +- .../common/util/fakeplayer/APFakePlayer.java | 61 +++++++++++++------ 3 files changed, 43 insertions(+), 22 deletions(-) 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 795a7fb3e..a98e8fb71 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 @@ -92,7 +92,6 @@ public boolean canAttack(@NotNull LivingEntity livingEntity) { public void openTextEdit(@NotNull SignBlockEntity sign) { } - @Override public boolean isSilent() { return true; @@ -322,33 +321,55 @@ public HitResult findHit(boolean skipEntity, boolean skipBlock, @Nullable Predic 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); + } + + AABB box = entity.getBoundingBox(); + Vec3 clipVec; if (box.contains(origin)) { - closestEntity = (LivingEntity) entityHit; - closestVec = clipResult.orElse(origin); - closestDistance = 0; - break; + clipVec = origin; + } else { + clipVec = box.clip(origin, target).orElse(null); + if (clipVec == null) { + continue; + } + } + double distance = origin.distanceToSqr(clipVec); + // Ignore small enough distance + if (distance <= 1e-6) { + distance = 0; } - Optional clipResult = box.clip(origin, target); - if (clipResult.isPresent()) { - Vec3 clipVec = clipResult.get(); - double distance = origin.distanceTo(clipVec); - if (distance < closestDistance) { - closestEntity = (LivingEntity) entityHit; - closestVec = clipVec; - closestDistance = distance; + 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; } } From 47af5f964e8b04946f29564db5f0af19b4f6537f Mon Sep 17 00:00:00 2001 From: endi Date: Wed, 28 Aug 2024 14:02:07 +0200 Subject: [PATCH 3/3] Fixed Checkstyle violation --- .../advancedperipherals/common/util/fakeplayer/APFakePlayer.java | 1 - 1 file changed, 1 deletion(-) 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 a98e8fb71..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;