Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various changes to StructureTemplates, 1.20.3 branch. #12

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -57,7 +57,7 @@ public void draw(OwoUIDrawContext context, int mouseX, int mouseY, float partial
var entityBuffers = client.getBufferBuilders().getEntityVertexConsumers();

float scale = Math.min(this.width, this.height);
scale /= Math.max(structure.xSize, Math.max(structure.ySize, structure.zSize));
scale /= Math.max(this.structure.xSize(), Math.max(this.structure.ySize(), this.structure.zSize()));
scale /= 1.625f;

var matrices = context.getMatrices();
Expand All @@ -68,7 +68,7 @@ public void draw(OwoUIDrawContext context, int mouseX, int mouseY, float partial

matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(this.displayAngle));
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(this.rotation));
matrices.translate(this.structure.xSize / -2f, this.structure.ySize / -2f, this.structure.zSize / -2f);
matrices.translate(this.structure.xSize() / -2.0f, this.structure.ySize() / -2.0f, this.structure.zSize() / -2.0f);

structure.forEachPredicate((blockPos, predicate) -> {
if (this.visibleLayer != -1 && this.visibleLayer != blockPos.getY()) return;
Expand All @@ -92,7 +92,7 @@ public void draw(OwoUIDrawContext context, int mouseX, int mouseY, float partial
DiffuseLighting.enableGuiDepthLighting();

if (this.placeable) {
if (StructureOverlayRenderer.isShowingOverlay(this.structure.id)) {
if (StructureOverlayRenderer.isShowingOverlay(this.structure.id())) {
context.drawText(client.textRenderer, Text.translatable("text.lavender.structure_component.active_overlay_hint"), this.x + this.width - 5 - client.textRenderer.getWidth("⚓"), this.y + this.height - 9 - 5, 0, false);
this.tooltip(Text.translatable("text.lavender.structure_component.hide_hint"));
} else {
Expand All @@ -106,11 +106,11 @@ public boolean onMouseDown(double mouseX, double mouseY, int button) {
var result = super.onMouseDown(mouseX, mouseY, button);
if (!this.placeable || button != GLFW.GLFW_MOUSE_BUTTON_LEFT || !Screen.hasShiftDown()) return result;

if (StructureOverlayRenderer.isShowingOverlay(this.structure.id)) {
StructureOverlayRenderer.removeAllOverlays(this.structure.id);
if (StructureOverlayRenderer.isShowingOverlay(this.structure.id())) {
StructureOverlayRenderer.removeAllOverlays(this.structure.id());
} else {
StructureOverlayRenderer.addPendingOverlay(this.structure.id);
StructureOverlayRenderer.restrictVisibleLayer(this.structure.id, this.visibleLayer);
StructureOverlayRenderer.addPendingOverlay(this.structure.id());
StructureOverlayRenderer.restrictVisibleLayer(this.structure.id(), this.visibleLayer);

MinecraftClient.getInstance().setScreen(null);
}
Expand All @@ -135,7 +135,7 @@ public boolean canFocus(FocusSource source) {
}

public StructureComponent visibleLayer(int visibleLayer) {
StructureOverlayRenderer.restrictVisibleLayer(this.structure.id, visibleLayer);
StructureOverlayRenderer.restrictVisibleLayer(this.structure.id(), visibleLayer);

this.visibleLayer = visibleLayer;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public void onInitializeClient() {

ClientPlayNetworking.registerGlobalReceiver(Lavender.WORLD_ID_CHANNEL, (client, handler, buf, responseSender) -> {
currentWorldId = buf.readUuid();
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.structure.BlockStatePredicate;
import io.wispforest.lavender.structure.LavenderStructures;
import io.wispforest.lavender.structure.StructureTemplate;
Expand Down Expand Up @@ -35,12 +36,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 All @@ -59,12 +63,17 @@ public class StructureOverlayRenderer {
private static final Identifier HUD_COMPONENT_ID = Lavender.id("structure_overlay");
private static final Identifier BARS_TEXTURE = Lavender.id("textures/gui/structure_overlay_bars.png");

public static Map<BlockPos, OverlayEntry> getActiveOverlays() {
return Collections.unmodifiableMap(ACTIVE_OVERLAYS);
}

public static void addPendingOverlay(Identifier structure) {
PENDING_OVERLAY = new OverlayEntry(structure, BlockRotation.NONE);
}

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 @@ -83,6 +92,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 @@ -103,10 +113,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 @@ -118,6 +130,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 @@ -264,6 +290,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 All @@ -278,8 +305,8 @@ private static Vec3i getPendingOffset(StructureTemplate structure) {
return switch (PENDING_OVERLAY.rotation) {
case NONE -> new Vec3i(-structure.anchor().getX(), -structure.anchor().getY(), -structure.anchor().getZ());
case CLOCKWISE_90 -> new Vec3i(-structure.anchor().getZ(), -structure.anchor().getY(), -structure.anchor().getX());
case CLOCKWISE_180 -> new Vec3i(-structure.xSize + structure.anchor.getX() + 1, -structure.anchor().getY(), -structure.zSize + structure.anchor.getZ() + 1);
case COUNTERCLOCKWISE_90 -> new Vec3i(-structure.zSize + structure.anchor.getZ() + 1, -structure.anchor().getY(), -structure.xSize + structure.anchor.getX() + 1);
case CLOCKWISE_180 -> new Vec3i(-structure.xSize() + structure.anchor().getX() + 1, -structure.anchor().getY(), -structure.zSize() + structure.anchor().getZ() + 1);
case COUNTERCLOCKWISE_90 -> new Vec3i(-structure.zSize() + structure.anchor().getZ() + 1, -structure.anchor().getY(), -structure.xSize() + structure.anchor().getX() + 1);
};
// @formatter:on
}
Expand All @@ -302,7 +329,7 @@ private static void renderOverlayBlock(MatrixStack matrices, VertexConsumerProvi
matrices.pop();
}

private static class OverlayEntry {
public static class OverlayEntry {

public final Identifier structureId;

Expand All @@ -317,6 +344,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
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,23 @@ public StructureNode(StructureTemplate structure, int angle, boolean placeable)
protected void visitStart(MarkdownCompiler<?> compiler) {
var structureComponent = StructureFeature.this.bookComponentSource.builtinTemplate(
ParentComponent.class,
this.structure.ySize > 1 ? "structure-preview-with-layers" : "structure-preview",
Map.of("structure", this.structure.id.toString(), "angle", String.valueOf(this.angle))
this.structure.ySize() > 1 ? "structure-preview-with-layers" : "structure-preview",
Map.of("structure", this.structure.id().toString(), "angle", String.valueOf(this.angle))
);

var structurePreview = structureComponent.childById(StructureComponent.class, "structure").placeable(this.placeable);
var layerSlider = structureComponent.childById(SlimSliderComponent.class, "layer-slider");

if (layerSlider != null) {
layerSlider.max(0).min(this.structure.ySize).tooltipSupplier(layer -> {
layerSlider.max(0).min(this.structure.ySize()).tooltipSupplier(layer -> {
return layer > 0
? Text.translatable("text.lavender.structure_component.layer_tooltip", layer.intValue())
: Text.translatable("text.lavender.structure_component.all_layers_tooltip");
}).onChanged().subscribe(layer -> {
structurePreview.visibleLayer((int) layer - 1);
});

layerSlider.value(StructureOverlayRenderer.getLayerRestriction(this.structure.id) + 1);
layerSlider.value(StructureOverlayRenderer.getLayerRestriction(this.structure.id()) + 1);
}

((OwoUICompiler) compiler).visitComponent(structureComponent);
Expand Down
Loading