Skip to content

Commit

Permalink
Add secret waypoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed Nov 12, 2023
1 parent 2abfcec commit 35d0259
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;

public class SecretWaypoint extends Waypoint {
protected static final Logger LOGGER = LoggerFactory.getLogger(SecretWaypoint.class);
public static final Codec<SecretWaypoint> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("secretIndex").forGetter(secretWaypoint -> secretWaypoint.secretIndex),
Category.CODEC.fieldOf("category").forGetter(secretWaypoint -> secretWaypoint.category),
Expand All @@ -35,8 +38,8 @@ public class SecretWaypoint extends Waypoint {
).apply(instance, SecretWaypoint::new));
public static final Codec<List<SecretWaypoint>> LIST_CODEC = CODEC.listOf();
static final List<String> SECRET_ITEMS = List.of("Decoy", "Defuse Kit", "Dungeon Chest Key", "Healing VIII", "Inflatable Jerry", "Spirit Leap", "Training Weights", "Trap", "Treasure Talisman");
private static final SkyblockerConfig.SecretWaypoints CONFIG = SkyblockerConfigManager.get().locations.dungeons.secretWaypoints;
private static final Supplier<Type> TYPE_SUPPLIER = () -> CONFIG.waypointType;
private static final Supplier<SkyblockerConfig.SecretWaypoints> CONFIG = () -> SkyblockerConfigManager.get().locations.dungeons.secretWaypoints;
private static final Supplier<Type> TYPE_SUPPLIER = () -> CONFIG.get().waypointType;
final int secretIndex;
final Category category;
final Text name;
Expand Down Expand Up @@ -95,7 +98,7 @@ public void render(WorldRenderContext context) {
//TODO In the future, shrink the box for wither essence and items so its more realistic
super.render(context);

if (CONFIG.showSecretText) {
if (CONFIG.get().showSecretText) {
Vec3d posUp = centerPos.add(0, 1, 0);
RenderHelper.renderText(context, name, posUp, true);
double distance = context.camera().getPos().distanceTo(centerPos);
Expand Down Expand Up @@ -135,8 +138,8 @@ enum Category implements StringIdentifiable {
}
}

private static Category get(JsonObject waypointJson) {
return CODEC.parse(JsonOps.INSTANCE, waypointJson.get("category")).resultOrPartial(DungeonSecrets.LOGGER::error).orElse(Category.DEFAULT);
static Category get(JsonObject waypointJson) {
return CODEC.parse(JsonOps.INSTANCE, waypointJson.get("category")).resultOrPartial(LOGGER::error).orElse(Category.DEFAULT);
}

boolean needsInteraction() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package de.hysky.skyblocker.skyblock.dungeon.secrets;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import net.minecraft.util.math.BlockPos;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;

public class SecretWaypointTest {
private final Gson gson = new Gson();

@Test
void testCodecSerialize() {
SecretWaypoint waypoint = new SecretWaypoint(0, SecretWaypoint.Category.DEFAULT, "name", BlockPos.ORIGIN);
JsonElement json = SecretWaypoint.CODEC.encodeStart(JsonOps.INSTANCE, waypoint).result().orElseThrow();
String expectedJson = "{\"secretIndex\":0,\"category\":\"default\",\"name\":{\"text\":\"name\"},\"pos\":[0,0,0]}";

Assertions.assertEquals(expectedJson, json.toString());
}

@Test
void testCodecDeserialize() {
String json = "{\"secretIndex\":0,\"category\":\"default\",\"name\":{\"text\":\"name\"},\"pos\":[0,0,0]}";
SecretWaypoint waypoint = SecretWaypoint.CODEC.parse(JsonOps.INSTANCE, gson.fromJson(json, JsonElement.class)).result().orElseThrow();
SecretWaypoint expectedWaypoint = new SecretWaypoint(0, SecretWaypoint.Category.DEFAULT, "name", BlockPos.ORIGIN);

equal(expectedWaypoint, waypoint);
}

@Test
void testListCodecSerialize() {
List<SecretWaypoint> waypoints = List.of(new SecretWaypoint(0, SecretWaypoint.Category.DEFAULT, "name", BlockPos.ORIGIN), new SecretWaypoint(1, SecretWaypoint.Category.CHEST, "name", new BlockPos(-1, 0, 1)));
JsonElement json = SecretWaypoint.LIST_CODEC.encodeStart(JsonOps.INSTANCE, waypoints).result().orElseThrow();
String expectedJson = "[{\"secretIndex\":0,\"category\":\"default\",\"name\":{\"text\":\"name\"},\"pos\":[0,0,0]},{\"secretIndex\":1,\"category\":\"chest\",\"name\":{\"text\":\"name\"},\"pos\":[-1,0,1]}]";

Assertions.assertEquals(expectedJson, json.toString());
}

@Test
void testListCodecDeserialize() {
String json = "[{\"secretIndex\":0,\"category\":\"default\",\"name\":{\"text\":\"name\"},\"pos\":[0,0,0]},{\"secretIndex\":1,\"category\":\"chest\",\"name\":{\"text\":\"name\"},\"pos\":[-1,0,1]}]";
List<SecretWaypoint> waypoints = SecretWaypoint.LIST_CODEC.parse(JsonOps.INSTANCE, gson.fromJson(json, JsonElement.class)).result().orElseThrow();
List<SecretWaypoint> expectedWaypoints = List.of(new SecretWaypoint(0, SecretWaypoint.Category.DEFAULT, "name", BlockPos.ORIGIN), new SecretWaypoint(1, SecretWaypoint.Category.CHEST, "name", new BlockPos(-1, 0, 1)));

Assertions.assertEquals(expectedWaypoints.size(), waypoints.size());
for (int i = 0; i < expectedWaypoints.size(); i++) {
SecretWaypoint expectedWaypoint = expectedWaypoints.get(i);
SecretWaypoint waypoint = waypoints.get(i);
equal(expectedWaypoint, waypoint);
}
}

@Test
void testGetCategory() {
JsonObject waypointJson = new JsonObject();
waypointJson.addProperty("category", "chest");
SecretWaypoint.Category category = SecretWaypoint.Category.get(waypointJson);
Assertions.assertEquals(SecretWaypoint.Category.CHEST, category);
}

@Test
void testGetCategoryDefault() {
JsonObject waypointJson = new JsonObject();
waypointJson.addProperty("category", "");
SecretWaypoint.Category category = SecretWaypoint.Category.get(waypointJson);
Assertions.assertEquals(SecretWaypoint.Category.DEFAULT, category);
}

private static void equal(SecretWaypoint expectedWaypoint, SecretWaypoint waypoint) {
Assertions.assertEquals(expectedWaypoint.secretIndex, waypoint.secretIndex);
Assertions.assertEquals(expectedWaypoint.category, waypoint.category);
Assertions.assertEquals(expectedWaypoint.name, waypoint.name);
Assertions.assertEquals(expectedWaypoint.pos, waypoint.pos);
}
}

0 comments on commit 35d0259

Please sign in to comment.