From 7da70dc182070e3cbac1ae8b13a3959a2405cc91 Mon Sep 17 00:00:00 2001 From: samolego <34912839+samolego@users.noreply.github.com> Date: Fri, 18 Feb 2022 10:09:09 +0100 Subject: [PATCH] Improve "/npc select" --- .../taterzens/commands/NpcCommand.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/org/samo_lego/taterzens/commands/NpcCommand.java b/common/src/main/java/org/samo_lego/taterzens/commands/NpcCommand.java index 306f954da..cc77b0380 100644 --- a/common/src/main/java/org/samo_lego/taterzens/commands/NpcCommand.java +++ b/common/src/main/java/org/samo_lego/taterzens/commands/NpcCommand.java @@ -15,7 +15,9 @@ import net.minecraft.network.chat.TextComponent; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.projectile.ProjectileUtil; import net.minecraft.world.phys.AABB; +import net.minecraft.world.phys.Vec3; import org.jetbrains.annotations.NotNull; import org.samo_lego.taterzens.Taterzens; import org.samo_lego.taterzens.api.TaterzensAPI; @@ -26,7 +28,6 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.List; import java.util.function.Consumer; import static net.minecraft.commands.Commands.argument; @@ -40,6 +41,10 @@ public class NpcCommand { public static LiteralCommandNode npcNode; + + private static final double MAX_DISTANCE = 8.02; + private static final double SQRD_DIST = MAX_DISTANCE * MAX_DISTANCE; + public static void register(CommandDispatcher dispatcher) { npcNode = dispatcher.register(literal("npc") .requires(src -> Taterzens.getInstance().getPlatform().checkPermission(src, "taterzens.npc", config.perms.npcCommandPermissionLevel)) @@ -211,16 +216,17 @@ private static int selectTaterzen(CommandContext context) th CommandSourceStack source = context.getSource(); ServerPlayer player = source.getPlayerOrException(); - AABB box = player.getBoundingBox().move(player.getLookAngle().scale(2.0F)).inflate(0.3D); + // Made with help of https://github.com/Patbox/polydex/blob/2e1cd03470c6202bf0522c845caa35b20244f8b9/src/main/java/eu/pb4/polydex/impl/display/PolydexTargetImpl.java#L44 + Vec3 min = player.getEyePosition(0); - TaterzenNPC npc = ((ITaterzenEditor) player).getNpc(); - if(npc != null) { - ((ITaterzenEditor) player).selectNpc(null); - } + Vec3 vec3d2 = player.getViewVector(1.0F); + Vec3 max = min.add(vec3d2.x * MAX_DISTANCE, vec3d2.y * MAX_DISTANCE, vec3d2.z * MAX_DISTANCE); + AABB box = player.getBoundingBox().expandTowards(vec3d2.scale(MAX_DISTANCE)).inflate(1.0D); + + final var hit = ProjectileUtil.getEntityHitResult(player, min, max, box, entity -> entity.isPickable() && entity instanceof TaterzenNPC, SQRD_DIST); - List detectedTaterzens = player.getLevel().getEntitiesOfClass(TaterzenNPC.class, box); - if(!detectedTaterzens.isEmpty()) { - TaterzenNPC taterzen = detectedTaterzens.get(0); + if (hit != null) { + TaterzenNPC taterzen = (TaterzenNPC) hit.getEntity(); boolean selected = ((ITaterzenEditor) player).selectNpc(taterzen); if (selected) { source.sendSuccess( @@ -236,8 +242,6 @@ private static int selectTaterzen(CommandContext context) th source.sendFailure( translate("taterzens.error.404.detected") .withStyle(ChatFormatting.RED) - .append("\n") - .append(translate("taterzens.command.deselect").withStyle(ChatFormatting.GOLD)) ); } return 1;