Skip to content

Commit

Permalink
Improve "/npc select"
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Feb 18, 2022
1 parent b6cdcc8 commit 7da70dc
Showing 1 changed file with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -40,6 +41,10 @@

public class NpcCommand {
public static LiteralCommandNode<CommandSourceStack> npcNode;

private static final double MAX_DISTANCE = 8.02;
private static final double SQRD_DIST = MAX_DISTANCE * MAX_DISTANCE;

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
npcNode = dispatcher.register(literal("npc")
.requires(src -> Taterzens.getInstance().getPlatform().checkPermission(src, "taterzens.npc", config.perms.npcCommandPermissionLevel))
Expand Down Expand Up @@ -211,16 +216,17 @@ private static int selectTaterzen(CommandContext<CommandSourceStack> 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<TaterzenNPC> 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(
Expand All @@ -236,8 +242,6 @@ private static int selectTaterzen(CommandContext<CommandSourceStack> context) th
source.sendFailure(
translate("taterzens.error.404.detected")
.withStyle(ChatFormatting.RED)
.append("\n")
.append(translate("taterzens.command.deselect").withStyle(ChatFormatting.GOLD))
);
}
return 1;
Expand Down

0 comments on commit 7da70dc

Please sign in to comment.