Skip to content

Commit

Permalink
Persist active structures when exiting the world
Browse files Browse the repository at this point in the history
Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
  • Loading branch information
solonovamax committed Jun 29, 2024
1 parent 9922400 commit b14a32f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@
import com.google.gson.reflect.TypeToken;
import io.wispforest.lavender.Lavender;
import io.wispforest.lavender.client.LavenderClient;
import io.wispforest.lavender.client.StructureOverlayRenderer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class LavenderClientStorage {

Expand All @@ -26,6 +35,9 @@ public class LavenderClientStorage {
private static final TypeToken<Map<UUID, Map<Identifier, Set<Identifier>>>> VIEWED_ENTRIES_TYPE = new TypeToken<>() {};
private static Map<UUID, Map<Identifier, Set<Identifier>>> viewedEntries;

private static final TypeToken<Map<UUID, List<ActiveStructureOverlay>>> ACTIVE_STRUCTURES_TYPE = new TypeToken<>() {};
private static Map<UUID, List<ActiveStructureOverlay>> activeStructures;

private static final Gson GSON = new GsonBuilder().registerTypeAdapter(Identifier.class, new Identifier.Serializer()).setPrettyPrinting().create();

static {
Expand All @@ -35,14 +47,17 @@ public class LavenderClientStorage {
bookmarks = GSON.fromJson(data.get("bookmarks"), BOOKMARKS_TYPE);
openedBooks = GSON.fromJson(data.get("opened_books"), OPENED_BOOKS_TYPE);
viewedEntries = GSON.fromJson(data.get("viewed_entries"), VIEWED_ENTRIES_TYPE);
activeStructures = GSON.fromJson(data.get("active_structures"), ACTIVE_STRUCTURES_TYPE);

if (bookmarks == null) bookmarks = new HashMap<>();
if (openedBooks == null) openedBooks = new HashMap<>();
if (viewedEntries == null) viewedEntries = new HashMap<>();
if (activeStructures == null) activeStructures = new HashMap<>();
} catch (Exception e) {
bookmarks = new HashMap<>();
openedBooks = new HashMap<>();
viewedEntries = new HashMap<>();
activeStructures = new HashMap<>();
save();
}
}
Expand Down Expand Up @@ -98,12 +113,30 @@ private static Set<Identifier> getOpenedBooksSet() {
return openedBooks.computeIfAbsent(LavenderClient.currentWorldId(), $ -> new HashSet<>());
}

public static List<ActiveStructureOverlay> getActiveStructures() {
return activeStructures.computeIfAbsent(LavenderClient.currentWorldId(), $ -> new ArrayList<>());
}

public static void setActiveStructures(Map<BlockPos, StructureOverlayRenderer.OverlayEntry> activeStructures) {
getActiveStructures().clear();
getActiveStructures().addAll(activeStructures.entrySet()
.stream()
.map((entry) -> {
var pos = entry.getKey();
var overlay = entry.getValue();
return new ActiveStructureOverlay(pos, overlay.structureId, overlay.rotation, overlay.visibleLayer);
})
.toList());
save();
}

private static void save() {
try {
var data = new JsonObject();
data.add("bookmarks", GSON.toJsonTree(bookmarks, BOOKMARKS_TYPE.getType()));
data.add("opened_books", GSON.toJsonTree(openedBooks, OPENED_BOOKS_TYPE.getType()));
data.add("viewed_entries", GSON.toJsonTree(viewedEntries, VIEWED_ENTRIES_TYPE.getType()));
data.add("active_structure", GSON.toJsonTree(activeStructures, ACTIVE_STRUCTURES_TYPE.getType()));

Files.writeString(storageFile(), GSON.toJson(data));
} catch (IOException e) {
Expand All @@ -127,4 +160,8 @@ public enum Type {
};
}
}
}

public record ActiveStructureOverlay(BlockPos pos, Identifier id, BlockRotation rotation, int visibleLayer) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void onInitializeClient() {

ClientPlayNetworking.registerGlobalReceiver(Lavender.WorldUUIDPayload.ID, (payload, context) -> {
currentWorldId = payload.worldUuid();
StructureOverlayRenderer.reloadActiveOverlays();
});

UIParsing.registerFactory(Lavender.id("ingredient"), element -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Suppliers;
import com.mojang.blaze3d.systems.RenderSystem;
import io.wispforest.lavender.Lavender;
import io.wispforest.lavender.book.LavenderClientStorage;
import io.wispforest.lavender.pond.LavenderFramebufferExtension;
import io.wispforest.lavender.structure.BlockStatePredicate;
import io.wispforest.lavender.structure.LavenderStructures;
Expand Down Expand Up @@ -36,13 +37,15 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL30C;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class StructureOverlayRenderer {

Expand Down Expand Up @@ -75,6 +78,7 @@ public static void addPendingOverlay(Identifier structure) {

public static void addOverlay(BlockPos anchorPoint, Identifier structure, BlockRotation rotation) {
ACTIVE_OVERLAYS.put(anchorPoint, new OverlayEntry(structure, rotation));
saveActiveOverlays();
}

public static boolean isShowingOverlay(Identifier structure) {
Expand All @@ -93,6 +97,7 @@ public static void removeAllOverlays(Identifier structure) {
}

ACTIVE_OVERLAYS.values().removeIf(entry -> structure.equals(entry.structureId));
saveActiveOverlays();
}

public static int getLayerRestriction(Identifier structure) {
Expand All @@ -113,10 +118,12 @@ public static void restrictVisibleLayer(Identifier structure, int visibleLayer)
if (!structure.equals(entry.structureId)) continue;
entry.visibleLayer = visibleLayer;
}
saveActiveOverlays();
}

public static void clearOverlays() {
ACTIVE_OVERLAYS.clear();
saveActiveOverlays();
}

public static void rotatePending(boolean clockwise) {
Expand All @@ -128,6 +135,20 @@ public static boolean hasPending() {
return PENDING_OVERLAY != null;
}

private static void saveActiveOverlays() {
LavenderClientStorage.setActiveStructures(Collections.unmodifiableMap(ACTIVE_OVERLAYS));
}

public static void reloadActiveOverlays() {
ACTIVE_OVERLAYS.clear();
ACTIVE_OVERLAYS.putAll(
LavenderClientStorage.getActiveStructures()
.stream()
.map((structure) -> Pair.of(structure.pos(), new OverlayEntry(structure.id(), structure.rotation(), structure.visibleLayer())))
.collect(Collectors.toMap(Pair::getLeft, Pair::getRight))
);
}

public static void initialize() {
Hud.add(HUD_COMPONENT_ID, () -> Containers.verticalFlow(Sizing.content(), Sizing.content()).gap(15).positioning(Positioning.relative(5, 100)));

Expand Down Expand Up @@ -274,6 +295,7 @@ public static void initialize() {
if (!player.isSneaking()) targetPos = targetPos.offset(hitResult.getSide());

ACTIVE_OVERLAYS.put(targetPos, PENDING_OVERLAY);
saveActiveOverlays();
PENDING_OVERLAY = null;

player.swingHand(hand);
Expand Down Expand Up @@ -327,6 +349,12 @@ public OverlayEntry(Identifier structureId, BlockRotation rotation) {
this.rotation = rotation;
}

public OverlayEntry(Identifier structureId, BlockRotation rotation, int visibleLayer) {
this.structureId = structureId;
this.rotation = rotation;
this.visibleLayer = visibleLayer;
}

public @Nullable StructureTemplate fetchStructure() {
return LavenderStructures.get(this.structureId);
}
Expand Down

0 comments on commit b14a32f

Please sign in to comment.