|
| 1 | +package cloudburst.rejects.commands; |
| 2 | + |
| 3 | +import baritone.api.BaritoneAPI; |
| 4 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 5 | +import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; |
| 6 | +import meteordevelopment.meteorclient.MeteorClient; |
| 7 | +import meteordevelopment.meteorclient.events.packets.PacketEvent; |
| 8 | +import meteordevelopment.meteorclient.systems.commands.Command; |
| 9 | +import cloudburst.rejects.arguments.EnumArgumentType; |
| 10 | +import meteordevelopment.meteorclient.utils.Utils; |
| 11 | +import meteordevelopment.meteorclient.utils.player.ChatUtils; |
| 12 | +import meteordevelopment.meteorclient.utils.player.FindItemResult; |
| 13 | +import meteordevelopment.meteorclient.utils.player.InvUtils; |
| 14 | +import cloudburst.rejects.utils.WorldGenUtils; |
| 15 | +import meteordevelopment.orbit.EventHandler; |
| 16 | +import net.minecraft.command.CommandSource; |
| 17 | +import net.minecraft.entity.EntityType; |
| 18 | +import net.minecraft.item.ItemStack; |
| 19 | +import net.minecraft.item.Items; |
| 20 | +import net.minecraft.nbt.NbtCompound; |
| 21 | +import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket; |
| 22 | +import net.minecraft.network.packet.s2c.play.PlaySoundS2CPacket; |
| 23 | +import net.minecraft.sound.SoundEvents; |
| 24 | +import net.minecraft.text.BaseText; |
| 25 | +import net.minecraft.text.LiteralText; |
| 26 | +import net.minecraft.util.math.BlockPos; |
| 27 | +import net.minecraft.util.math.Vec3d; |
| 28 | + |
| 29 | +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; |
| 30 | + |
| 31 | +public class LocateCommand extends Command { |
| 32 | + |
| 33 | + private final static DynamicCommandExceptionType NOT_FOUND = new DynamicCommandExceptionType(o -> { |
| 34 | + if (o instanceof WorldGenUtils.Feature) { |
| 35 | + return new LiteralText(String.format( |
| 36 | + "%s not found.", |
| 37 | + Utils.nameToTitle(o.toString().replaceAll("_", "-"))) |
| 38 | + ); |
| 39 | + } |
| 40 | + return new LiteralText("Not found."); |
| 41 | + }); |
| 42 | + |
| 43 | + private Vec3d firstStart; |
| 44 | + private Vec3d firstEnd; |
| 45 | + private Vec3d secondStart; |
| 46 | + private Vec3d secondEnd; |
| 47 | + |
| 48 | + public LocateCommand() { |
| 49 | + super("locate", "Locates structures", "loc"); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void build(LiteralArgumentBuilder<CommandSource> builder) { |
| 54 | + builder.then(literal("lodestone").executes(ctx -> { |
| 55 | + ItemStack stack = mc.player.getInventory().getMainHandStack(); |
| 56 | + if (stack.getItem() != Items.COMPASS) { |
| 57 | + error("You need to hold a lodestone compass"); |
| 58 | + return SINGLE_SUCCESS; |
| 59 | + } |
| 60 | + NbtCompound tag = stack.getTag(); |
| 61 | + if (tag == null) { |
| 62 | + error("Couldn't get the NBT data. Are you holding a (highlight)lodestone(default) compass?"); |
| 63 | + return SINGLE_SUCCESS; |
| 64 | + } |
| 65 | + NbtCompound nbt1 = tag.getCompound("LodestonePos"); |
| 66 | + if (nbt1 == null) { |
| 67 | + error("Couldn't get the NBT data. Are you holding a (highlight)lodestone(default) compass?"); |
| 68 | + return SINGLE_SUCCESS; |
| 69 | + } |
| 70 | + |
| 71 | + Vec3d coords = new Vec3d(nbt1.getDouble("X"),nbt1.getDouble("Y"),nbt1.getDouble("Z")); |
| 72 | + BaseText text = new LiteralText("Lodestone located at "); |
| 73 | + text.append(ChatUtils.formatCoords(coords)); |
| 74 | + text.append("."); |
| 75 | + info(text); |
| 76 | + return SINGLE_SUCCESS; |
| 77 | + })); |
| 78 | + |
| 79 | + builder.then(argument("feature", EnumArgumentType.enumArgument(WorldGenUtils.Feature.stronghold)).executes(ctx -> { |
| 80 | + WorldGenUtils.Feature feature = EnumArgumentType.getEnum(ctx, "feature", WorldGenUtils.Feature.stronghold); |
| 81 | + BlockPos pos = WorldGenUtils.locateFeature(feature, mc.player.getBlockPos()); |
| 82 | + if (pos != null) { |
| 83 | + BaseText text = new LiteralText(String.format( |
| 84 | + "%s located at ", |
| 85 | + Utils.nameToTitle(feature.toString().replaceAll("_", "-")) |
| 86 | + )); |
| 87 | + Vec3d coords = new Vec3d(pos.getX(), pos.getY(), pos.getZ()); |
| 88 | + text.append(ChatUtils.formatCoords(coords)); |
| 89 | + text.append("."); |
| 90 | + info(text); |
| 91 | + return SINGLE_SUCCESS; |
| 92 | + } |
| 93 | + if (feature == WorldGenUtils.Feature.stronghold) { |
| 94 | + FindItemResult eye = InvUtils.findInHotbar(Items.ENDER_EYE); |
| 95 | + if (eye.found()) { |
| 96 | + BaritoneAPI.getProvider().getPrimaryBaritone().getCommandManager().execute("follow entity minecraft:eye_of_ender"); |
| 97 | + firstStart = null; |
| 98 | + firstEnd = null; |
| 99 | + secondStart = null; |
| 100 | + secondEnd = null; |
| 101 | + MeteorClient.EVENT_BUS.subscribe(this); |
| 102 | + info("Please throw the first Eye of Ender"); |
| 103 | + } |
| 104 | + } |
| 105 | + throw NOT_FOUND.create(feature); |
| 106 | + })); |
| 107 | + |
| 108 | + builder.then(literal("cancel").executes(s -> { |
| 109 | + cancel(); |
| 110 | + return SINGLE_SUCCESS; |
| 111 | + })); |
| 112 | + } |
| 113 | + |
| 114 | + private void cancel() { |
| 115 | + warning("Locate canceled"); |
| 116 | + MeteorClient.EVENT_BUS.unsubscribe(this); |
| 117 | + } |
| 118 | + |
| 119 | + @EventHandler |
| 120 | + private void onReadPacket(PacketEvent.Receive event) { |
| 121 | + if (event.packet instanceof EntitySpawnS2CPacket) { |
| 122 | + EntitySpawnS2CPacket packet = (EntitySpawnS2CPacket) event.packet; |
| 123 | + if (packet.getEntityTypeId() == EntityType.EYE_OF_ENDER) { |
| 124 | + firstPosition(packet.getX(),packet.getY(),packet.getZ()); |
| 125 | + } |
| 126 | + } |
| 127 | + if (event.packet instanceof PlaySoundS2CPacket) { |
| 128 | + PlaySoundS2CPacket packet = (PlaySoundS2CPacket) event.packet; |
| 129 | + if (packet.getSound() == SoundEvents.ENTITY_ENDER_EYE_DEATH) { |
| 130 | + lastPosition(packet.getX(), packet.getY(), packet.getZ()); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private void firstPosition(double x, double y, double z) { |
| 136 | + Vec3d pos = new Vec3d(x, y, z); |
| 137 | + if (this.firstStart == null) { |
| 138 | + this.firstStart = pos; |
| 139 | + } |
| 140 | + else { |
| 141 | + this.secondStart = pos; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void lastPosition(double x, double y, double z) { |
| 146 | + info("%s Eye of Ender's trajectory saved.", (this.firstEnd == null) ? "First" : "Second"); |
| 147 | + Vec3d pos = new Vec3d(x, y, z); |
| 148 | + if (this.firstEnd == null) { |
| 149 | + this.firstEnd = pos; |
| 150 | + info("Please throw the second Eye Of Ender from a different location."); |
| 151 | + } |
| 152 | + else { |
| 153 | + this.secondEnd = pos; |
| 154 | + findStronghold(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private void findStronghold() { |
| 159 | + if (this.firstStart == null || this.firstEnd == null || this.secondStart == null || this.secondEnd == null) { |
| 160 | + error("Missing position data"); |
| 161 | + cancel(); |
| 162 | + return; |
| 163 | + } |
| 164 | + final double[] start = new double[]{this.secondStart.x, this.secondStart.z, this.secondEnd.x, this.secondEnd.z}; |
| 165 | + final double[] end = new double[]{this.firstStart.x, this.firstStart.z, this.firstEnd.x, this.firstEnd.z}; |
| 166 | + final double[] intersection = calcIntersection(start, end); |
| 167 | + if (Double.isNaN(intersection[0]) || Double.isNaN(intersection[1]) || Double.isInfinite(intersection[0]) || Double.isInfinite(intersection[1])) { |
| 168 | + error("Lines are parallel"); |
| 169 | + cancel(); |
| 170 | + return; |
| 171 | + } |
| 172 | + BaritoneAPI.getProvider().getPrimaryBaritone().getCommandManager().execute("stop"); |
| 173 | + MeteorClient.EVENT_BUS.unsubscribe(this); |
| 174 | + Vec3d coords = new Vec3d(intersection[0],0,intersection[1]); |
| 175 | + BaseText text = new LiteralText("Stronghold roughly located at "); |
| 176 | + text.append(ChatUtils.formatCoords(coords)); |
| 177 | + text.append("."); |
| 178 | + info(text); |
| 179 | + } |
| 180 | + |
| 181 | + private double[] calcIntersection(double[] line, double[] line2) { |
| 182 | + final double a1 = line[3] - line[1]; |
| 183 | + final double b1 = line[0] - line[2]; |
| 184 | + final double c1 = a1 * line[0] + b1 * line[1]; |
| 185 | + |
| 186 | + final double a2 = line2[3] - line2[1]; |
| 187 | + final double b2 = line2[0] - line2[2]; |
| 188 | + final double c2 = a2 * line2[0] + b2 * line2[1]; |
| 189 | + |
| 190 | + final double delta = a1 * b2 - a2 * b1; |
| 191 | + |
| 192 | + return new double[]{(b2 * c1 - b1 * c2) / delta, (a1 * c2 - a2 * c1) / delta}; |
| 193 | + } |
| 194 | +} |
0 commit comments