diff options
author | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2023-10-22 22:02:05 -0400 |
---|---|---|
committer | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2023-10-22 22:39:49 -0400 |
commit | 12d3086e9506e6e120c492ea99dcc6e84738ce3b (patch) | |
tree | 80f3b8c4f3afa203a605745629111fbf37011971 /src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets | |
parent | 4e3c09b51dff976a7680d887c487830e2445684b (diff) | |
download | Skyblocker-12d3086e9506e6e120c492ea99dcc6e84738ce3b.tar.gz Skyblocker-12d3086e9506e6e120c492ea99dcc6e84738ce3b.tar.bz2 Skyblocker-12d3086e9506e6e120c492ea99dcc6e84738ce3b.zip |
Refactor DungeonSecrets
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonSecrets.java | 66 | ||||
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/Room.java | 48 |
2 files changed, 58 insertions, 56 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonSecrets.java b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonSecrets.java index 96b4a7c9..07cb0395 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonSecrets.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonSecrets.java @@ -7,18 +7,14 @@ import com.mojang.brigadier.Command; import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; - -import it.unimi.dsi.fastutil.ints.IntRBTreeSet; -import it.unimi.dsi.fastutil.ints.IntSortedSet; -import it.unimi.dsi.fastutil.ints.IntSortedSets; -import it.unimi.dsi.fastutil.objects.Object2ByteMap; -import it.unimi.dsi.fastutil.objects.Object2ByteOpenHashMap; -import it.unimi.dsi.fastutil.objects.ObjectIntPair; import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.scheduler.Scheduler; +import it.unimi.dsi.fastutil.objects.Object2ByteMap; +import it.unimi.dsi.fastutil.objects.Object2ByteOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectIntPair; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents; @@ -156,8 +152,8 @@ public class DungeonSecrets { ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(literal(SkyblockerMod.NAMESPACE).then(literal("dungeons").then(literal("secrets") .then(literal("markAsFound").then(markSecretsCommand(true))) .then(literal("markAsMissing").then(markSecretsCommand(false))) - .then(literal("getPos").executes(context -> registerPosCommand(context.getSource(), false))) - .then(literal("getFacingPos").executes(context -> registerPosCommand(context.getSource(), true))))))); + .then(literal("getRelativePos").executes(context -> getRelativePos(context.getSource()))) + .then(literal("getRelativeTargetPos").executes(context -> getRelativeTargetPos(context.getSource()))))))); ClientPlayConnectionEvents.JOIN.register(((handler, sender, client) -> reset())); } @@ -212,8 +208,9 @@ public class DungeonSecrets { /** * Loads the json from the given {@link BufferedReader} into the given {@link Map}. + * * @param reader the reader to read the json from - * @param map the map to load into + * @param map the map to load into */ private static void loadJson(BufferedReader reader, Map<String, JsonElement> map) { SkyblockerMod.GSON.fromJson(reader, JsonObject.class).asMap().forEach((room, jsonElement) -> map.put(room.toLowerCase().replaceAll(" ", "-"), jsonElement)); @@ -230,30 +227,27 @@ public class DungeonSecrets { return Command.SINGLE_SUCCESS; }); } - - //TODO This logic can be split into a separate method for when we want to allow for adding custom waypoints - private static int registerPosCommand(FabricClientCommandSource source, boolean facing) { - if (currentRoom != null && currentRoom.getDirection() != null) { - MinecraftClient client = MinecraftClient.getInstance(); - - if (facing && (client.crosshairTarget == null || client.crosshairTarget.getType() != HitResult.Type.BLOCK)) { - source.sendError(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.unableToFindPos"))); - - return Command.SINGLE_SUCCESS; - } - BlockPos blockToCheck = facing && client.crosshairTarget instanceof BlockHitResult blockHitResult ? blockHitResult.getBlockPos() : client.player.getBlockPos(); - IntSortedSet segmentsX = IntSortedSets.unmodifiable(new IntRBTreeSet(currentRoom.getSegments().stream().mapToInt(Vector2ic::x).toArray())); - IntSortedSet segmentsY = IntSortedSets.unmodifiable(new IntRBTreeSet(currentRoom.getSegments().stream().mapToInt(Vector2ic::y).toArray())); - Vector2ic physicalCornerPos = DungeonMapUtils.getPhysicalCornerPos(currentRoom.getDirection(), segmentsX, segmentsY); + private static int getRelativePos(FabricClientCommandSource source) { + return getRelativePos(source, source.getPlayer().getBlockPos()); + } - BlockPos relativePos = DungeonMapUtils.actualToRelative(currentRoom.getDirection(), physicalCornerPos, blockToCheck); + private static int getRelativeTargetPos(FabricClientCommandSource source) { + if (MinecraftClient.getInstance().crosshairTarget instanceof BlockHitResult blockHitResult && blockHitResult.getType() == HitResult.Type.BLOCK) { + return getRelativePos(source, blockHitResult.getBlockPos()); + } else { + source.sendError(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.noTarget"))); + } + return Command.SINGLE_SUCCESS; + } + private static int getRelativePos(FabricClientCommandSource source, BlockPos pos) { + if (isCurrentRoomMatched()) { + BlockPos relativePos = DungeonMapUtils.actualToRelative(currentRoom.getDirection(), currentRoom.getPhysicalCornerPos(), pos); source.sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.posMessage", currentRoom.getName(), relativePos.getX(), relativePos.getY(), relativePos.getZ()))); - } else { - source.sendError(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.unableToFindPos"))); - } - + } else { + source.sendError(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.notMatched"))); + } return Command.SINGLE_SUCCESS; } @@ -374,16 +368,16 @@ public class DungeonSecrets { * Used to detect when all secrets in a room are found. */ private static void onChatMessage(Text text, boolean overlay) { + String message = text.getString(); + if (overlay && isCurrentRoomMatched()) { - currentRoom.onChatMessage(text.getString()); + currentRoom.onChatMessage(message); } - String message = text.getString(); - if (message.equals("[BOSS] Bonzo: Gratz for making it this far, but I'm basically unbeatable.") || message.equals("[BOSS] Scarf: This is where the journey ends for you, Adventurers.") - || message.equals("[BOSS] The Professor: I was burdened with terrible news recently...") || message.equals("[BOSS] Thorn: Welcome Adventurers! I am Thorn, the Spirit! And host of the Vegan Trials!") - || message.equals("[BOSS] Livid: Welcome, you've arrived right on time. I am Livid, the Master of Shadows.") || message.equals("[BOSS] Sadan: So you made it all the way here... Now you wish to defy me? Sadan?!") - || message.equals("[BOSS] Maxor: WELL! WELL! WELL! LOOK WHO'S HERE!")) reset(); + || message.equals("[BOSS] The Professor: I was burdened with terrible news recently...") || message.equals("[BOSS] Thorn: Welcome Adventurers! I am Thorn, the Spirit! And host of the Vegan Trials!") + || message.equals("[BOSS] Livid: Welcome, you've arrived right on time. I am Livid, the Master of Shadows.") || message.equals("[BOSS] Sadan: So you made it all the way here... Now you wish to defy me? Sadan?!") + || message.equals("[BOSS] Maxor: WELL! WELL! WELL! LOOK WHO'S HERE!")) reset(); } /** diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/Room.java b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/Room.java index 79b13d3b..8d8d0757 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/Room.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/Room.java @@ -5,10 +5,10 @@ import com.google.common.collect.ImmutableTable; import com.google.common.collect.Table; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import de.hysky.skyblocker.utils.scheduler.Scheduler; import it.unimi.dsi.fastutil.ints.IntRBTreeSet; import it.unimi.dsi.fastutil.ints.IntSortedSet; import it.unimi.dsi.fastutil.ints.IntSortedSets; -import de.hysky.skyblocker.utils.scheduler.Scheduler; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; import net.fabricmc.fabric.api.util.TriState; import net.minecraft.block.BlockState; @@ -28,7 +28,6 @@ import net.minecraft.world.World; import org.apache.commons.lang3.tuple.MutableTriple; import org.apache.commons.lang3.tuple.Triple; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.joml.Vector2i; import org.joml.Vector2ic; @@ -73,8 +72,10 @@ public class Room { */ private TriState matched = TriState.DEFAULT; private Table<Integer, BlockPos, SecretWaypoint> secretWaypoints; - private Direction direction = null; - private String name = null; + private String name; + private Direction direction; + + private Vector2ic physicalCornerPos; public Room(@NotNull Type type, @NotNull Vector2ic... physicalPositions) { this.type = type; @@ -91,23 +92,29 @@ public class Room { return type; } - @NotNull - public Set<Vector2ic> getSegments() { - return segments; - } - public boolean isMatched() { return matched == TriState.TRUE; } - - @Nullable + + /** + * Not null if {@link #isMatched()}. + */ + public String getName() { + return name; + } + + /** + * Not null if {@link #isMatched()}. + */ public Direction getDirection() { return direction; } - - @Nullable - public String getName() { - return name; + + /** + * Not null if {@link #isMatched()}. + */ + public Vector2ic getPhysicalCornerPos() { + return physicalCornerPos; } @Override @@ -235,7 +242,7 @@ public class Room { * </ul> * <li> If there are exactly one room matching: </li> * <ul> - * <li> Call {@link #roomMatched(String, Direction, Vector2ic)}. </li> + * <li> Call {@link #roomMatched()}. </li> * <li> Discard the no longer needed fields to save memory. </li> * <li> Return {@code true} </li> * </ul> @@ -274,7 +281,10 @@ public class Room { // If one room matches, load the secrets for that room and discard the no longer needed fields. for (Triple<Direction, Vector2ic, List<String>> directionRooms : possibleRooms) { if (directionRooms.getRight().size() == 1) { - roomMatched(directionRooms.getRight().get(0), directionRooms.getLeft(), directionRooms.getMiddle()); + name = directionRooms.getRight().get(0); + direction = directionRooms.getLeft(); + physicalCornerPos = directionRooms.getMiddle(); + roomMatched(); discard(); return true; } @@ -304,7 +314,7 @@ public class Room { * @param directionRooms the direction, position, and name of the room */ @SuppressWarnings("JavadocReference") - private void roomMatched(String name, Direction direction, Vector2ic physicalCornerPos) { + private void roomMatched() { Table<Integer, BlockPos, SecretWaypoint> secretWaypointsMutable = HashBasedTable.create(); for (JsonElement waypointElement : DungeonSecrets.getRoomWaypoints(name)) { JsonObject waypoint = waypointElement.getAsJsonObject(); @@ -315,8 +325,6 @@ public class Room { } secretWaypoints = ImmutableTable.copyOf(secretWaypointsMutable); matched = TriState.TRUE; - this.direction = direction; - this.name = name; DungeonSecrets.LOGGER.info("[Skyblocker] Room {} matched after checking {} block(s)", name, checkedBlocks.size()); } |