Skip to content

Commit

Permalink
Add custom waypoints removing
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed Oct 28, 2023
1 parent 58ba4dd commit 16467d7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.google.gson.JsonObject;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
Expand All @@ -32,6 +31,7 @@
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.command.argument.BlockPosArgumentType;
import net.minecraft.command.argument.PosArgument;
import net.minecraft.command.argument.TextArgumentType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.AmbientEntity;
Expand Down Expand Up @@ -178,6 +178,14 @@ public static void addCustomWaypoints(String room, Collection<SecretWaypoint> wa
}
}

/**
* @see #customWaypoints
*/
@Nullable
public static SecretWaypoint removeCustomWaypoint(String room, BlockPos pos) {
return customWaypoints.remove(room, pos);
}

/**
* Loads the dungeon secrets asynchronously from {@code /assets/skyblocker/dungeons}.
* Use {@link #isRoomsLoaded()} to check for completion of loading.
Expand All @@ -202,8 +210,10 @@ public static void init() {
.then(literal("markAsMissing").then(markSecretsCommand(false)))
.then(literal("getRelativePos").executes(DungeonSecrets::getRelativePos))
.then(literal("getRelativeTargetPos").executes(DungeonSecrets::getRelativeTargetPos))
.then(literal("addWaypoint").then(addWaypointCommand(false)))
.then(literal("addWaypointRelatively").then(addWaypointCommand(true)))
.then(literal("addWaypoint").then(addCustomWaypointCommand(false)))
.then(literal("addWaypointRelatively").then(addCustomWaypointCommand(true)))
.then(literal("removeWaypoint").then(removeCustomWaypointCommand(false)))
.then(literal("removeWaypointRelatively").then(removeCustomWaypointCommand(true)))
))));
ClientPlayConnectionEvents.JOIN.register(((handler, sender, client) -> reset()));
}
Expand Down Expand Up @@ -291,8 +301,8 @@ private static void loadJson(BufferedReader reader, Map<String, JsonElement> map
}

private static ArgumentBuilder<FabricClientCommandSource, RequiredArgumentBuilder<FabricClientCommandSource, Integer>> markSecretsCommand(boolean found) {
return argument("secret", IntegerArgumentType.integer()).executes(context -> {
int secretIndex = IntegerArgumentType.getInteger(context, "secret");
return argument("secretIndex", IntegerArgumentType.integer()).executes(context -> {
int secretIndex = IntegerArgumentType.getInteger(context, "secretIndex");
if (markSecrets(secretIndex, found)) {
context.getSource().sendFeedback(Constants.PREFIX.get().append(Text.translatable(found ? "skyblocker.dungeons.secrets.markSecretFound" : "skyblocker.dungeons.secrets.markSecretMissing", secretIndex)));
} else {
Expand Down Expand Up @@ -326,11 +336,11 @@ private static int getRelativePos(FabricClientCommandSource source, BlockPos pos
return Command.SINGLE_SUCCESS;
}

private static ArgumentBuilder<FabricClientCommandSource, RequiredArgumentBuilder<FabricClientCommandSource, PosArgument>> addWaypointCommand(boolean relative) {
private static ArgumentBuilder<FabricClientCommandSource, RequiredArgumentBuilder<FabricClientCommandSource, PosArgument>> addCustomWaypointCommand(boolean relative) {
return argument("pos", BlockPosArgumentType.blockPos())
.then(argument("secretIndex", IntegerArgumentType.integer())
.then(argument("category", SecretWaypoint.Category.CategoryArgumentType.category())
.then(argument("name", StringArgumentType.greedyString()).executes(context -> {
.then(argument("name", TextArgumentType.text()).executes(context -> {
// TODO Less hacky way with custom ClientBlockPosArgumentType
BlockPos pos = context.getArgument("pos", PosArgument.class).toAbsoluteBlockPos(new ServerCommandSource(null, context.getSource().getPosition(), context.getSource().getRotation(), null, 0, null, null, null, null));
return relative ? addCustomWaypointRelative(context, pos) : addCustomWaypoint(context, pos);
Expand Down Expand Up @@ -358,6 +368,34 @@ private static int addCustomWaypointRelative(CommandContext<FabricClientCommandS
return Command.SINGLE_SUCCESS;
}

private static ArgumentBuilder<FabricClientCommandSource, RequiredArgumentBuilder<FabricClientCommandSource, PosArgument>> removeCustomWaypointCommand(boolean relative) {
return argument("pos", BlockPosArgumentType.blockPos())
.executes(context -> {
// TODO Less hacky way with custom ClientBlockPosArgumentType
BlockPos pos = context.getArgument("pos", PosArgument.class).toAbsoluteBlockPos(new ServerCommandSource(null, context.getSource().getPosition(), context.getSource().getRotation(), null, 0, null, null, null, null));
return relative ? removeCustomWaypointRelative(context, pos) : removeCustomWaypoint(context, pos);
});
}

private static int removeCustomWaypoint(CommandContext<FabricClientCommandSource> context, BlockPos pos) {
Room room = getRoomAtPhysical(pos);
if (isRoomMatched(room)) {
room.removeCustomWaypoint(context, room.actualToRelative(pos));
} else {
context.getSource().sendError(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.notMatched")));
}
return Command.SINGLE_SUCCESS;
}

private static int removeCustomWaypointRelative(CommandContext<FabricClientCommandSource> context, BlockPos pos) {
if (isCurrentRoomMatched()) {
currentRoom.removeCustomWaypoint(context, pos);
} else {
context.getSource().sendError(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.notMatched")));
}
return Command.SINGLE_SUCCESS;
}

/**
* Updates the dungeon. The general idea is similar to the Dungeon Rooms Mod.
* <p></p>
Expand Down Expand Up @@ -431,8 +469,7 @@ private static void update() {
}
switch (type) {
case ENTRANCE, PUZZLE, TRAP, MINIBOSS, FAIRY, BLOOD -> room = newRoom(type, physicalPos);
case ROOM ->
room = newRoom(type, DungeonMapUtils.getPhysicalPosFromMap(mapEntrancePos, mapRoomSize, physicalEntrancePos, DungeonMapUtils.getRoomSegments(map, mapPos, mapRoomSize, type.color)));
case ROOM -> room = newRoom(type, DungeonMapUtils.getPhysicalPosFromMap(mapEntrancePos, mapRoomSize, physicalEntrancePos, DungeonMapUtils.getRoomSegments(map, mapPos, mapRoomSize, type.color)));
}
}
if (room != null && currentRoom != room) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import de.hysky.skyblocker.utils.Constants;
import de.hysky.skyblocker.utils.scheduler.Scheduler;
Expand All @@ -32,6 +31,7 @@
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;

Expand Down Expand Up @@ -159,12 +159,12 @@ private Direction[] getPossibleDirections(IntSortedSet segmentsX, IntSortedSet s
}

/**
* @see #addCustomWaypoint(int, SecretWaypoint.Category, String, BlockPos)
* @see #addCustomWaypoint(int, SecretWaypoint.Category, Text, BlockPos)
*/
protected void addCustomWaypoint(CommandContext<FabricClientCommandSource> context, BlockPos pos) {
int secretIndex = IntegerArgumentType.getInteger(context, "secretIndex");
SecretWaypoint.Category category = SecretWaypoint.Category.CategoryArgumentType.getCategory(context, "category");
String waypointName = StringArgumentType.getString(context, "name");
Text waypointName = context.getArgument("name", Text.class);
addCustomWaypoint(secretIndex, category, waypointName, pos);
context.getSource().sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.customWaypointAdded", pos.getX(), pos.getY(), pos.getZ(), name, secretIndex, category, waypointName)));
}
Expand All @@ -178,14 +178,14 @@ protected void addCustomWaypoint(CommandContext<FabricClientCommandSource> conte
* @param pos the position of the secret waypoint relative to this room
*/
@SuppressWarnings("JavadocReference")
private void addCustomWaypoint(int secretIndex, SecretWaypoint.Category category, String waypointName, BlockPos pos) {
private void addCustomWaypoint(int secretIndex, SecretWaypoint.Category category, Text waypointName, BlockPos pos) {
SecretWaypoint waypoint = new SecretWaypoint(secretIndex, category, waypointName, pos);
DungeonSecrets.addCustomWaypoint(name, waypoint);
DungeonSecrets.getRoomsStream().filter(r -> name.equals(r.getName())).forEach(r -> r.addCustomWaypoint(waypoint));
}

/**
* Adds a custom waypoint relative to this room to this room.
* Adds a custom waypoint relative to this room to this instance of the room.
*
* @param relativeWaypoint the secret waypoint relative to this room to add
*/
Expand All @@ -194,6 +194,43 @@ private void addCustomWaypoint(SecretWaypoint relativeWaypoint) {
secretWaypoints.put(actualWaypoint.secretIndex, actualWaypoint.pos, actualWaypoint);
}

/**
* @see #removeCustomWaypoint(BlockPos)
*/
protected void removeCustomWaypoint(CommandContext<FabricClientCommandSource> context, BlockPos pos) {
SecretWaypoint waypoint = removeCustomWaypoint(pos);
if (waypoint != null) {
context.getSource().sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.customWaypointRemoved", pos.getX(), pos.getY(), pos.getZ(), name, waypoint.secretIndex, waypoint.category, waypoint.name)));
} else {
context.getSource().sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.customWaypointNotFound", pos.getX(), pos.getY(), pos.getZ(), name)));
}
}

/**
* Removes a custom waypoint relative to this room from {@link DungeonSecrets#customWaypoints} and all existing instances of this room.
* @param pos the position of the secret waypoint relative to this room
* @return the removed secret waypoint or {@code null} if there was no secret waypoint at the given position
*/
@SuppressWarnings("JavadocReference")
@Nullable
private SecretWaypoint removeCustomWaypoint(BlockPos pos) {
SecretWaypoint waypoint = DungeonSecrets.removeCustomWaypoint(name, pos);
if (waypoint != null) {
DungeonSecrets.getRoomsStream().filter(r -> name.equals(r.getName())).forEach(r -> r.removeCustomWaypoint(waypoint.secretIndex, pos));
}
return waypoint;
}

/**
* Removes a custom waypoint relative to this room from this instance of the room.
* @param secretIndex the index of the secret waypoint
* @param relativePos the position of the secret waypoint relative to this room
*/
private void removeCustomWaypoint(int secretIndex, BlockPos relativePos) {
BlockPos actualPos = relativeToActual(relativePos);
secretWaypoints.remove(secretIndex, actualPos);
}

/**
* Updates the room.
* <p></p>
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/skyblocker/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,11 @@
"skyblocker.dungeons.secrets.markSecretFoundUnable": "§cUnable to mark secret #%d as found.",
"skyblocker.dungeons.secrets.markSecretMissingUnable": "§cUnable to mark secret #%d as missing.",
"skyblocker.dungeons.secrets.posMessage": "§rRoom: %s, X: %d, Y: %d, Z: %d",
"skyblocker.dungeons.secrets.customWaypointAdded": "§rAdded a custom waypoint at X: %d, Y: %d, Z: %d for room %s secret #%d of category %s with name '%s'.",
"skyblocker.dungeons.secrets.noTarget": "§cNo target block found! (Are you pointing at a block in range?)",
"skyblocker.dungeons.secrets.notMatched": "§cThe current room is not matched! (Are you in a dungeon room?)",
"skyblocker.dungeons.secrets.customWaypointAdded": "§rAdded a custom waypoint at X: %d, Y: %d, Z: %d for room %s secret #%d of category %s with name '%s'.",
"skyblocker.dungeons.secrets.customWaypointRemoved": "§rRemoved custom waypoint at X: %d, Y: %d, Z: %d for room %s secret #%d of category %s with name '%s'.",
"skyblocker.dungeons.secrets.customWaypointNotFound": "§cNo custom waypoint found at X: %d, Y: %d, Z: %d for room %s.",

"skyblocker.fishing.reelNow": "Reel in now!",
"skyblocker.rift.healNow": "Heal now!",
Expand Down

0 comments on commit 16467d7

Please sign in to comment.