Skip to content

Commit

Permalink
feat: expose current previewed multiblock to API
Browse files Browse the repository at this point in the history
  • Loading branch information
klikli-dev committed Sep 12, 2024
1 parent 62a25c4 commit 1463e45
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
import com.klikli_dev.modonomicon.api.datagen.BookContextHelper;
import com.klikli_dev.modonomicon.api.datagen.CategoryEntryMap;
import com.klikli_dev.modonomicon.api.multiblock.Multiblock;
import com.klikli_dev.modonomicon.api.multiblock.MultiblockPreviewData;
import com.klikli_dev.modonomicon.api.stub.ModonomiconAPIStub;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Rotation;
import org.apache.logging.log4j.LogManager;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

Expand Down Expand Up @@ -44,6 +49,15 @@ static ModonomiconAPI get() {

Multiblock getMultiblock(ResourceLocation id);

/**
* Gets the multiblock currently previewed by the player, or null if none.
* Make sure to check for isAnchored(), a non-anchored multiblock can move any tick if the player looks around.
* Use {@link Multiblock#simulate(Level, BlockPos, Rotation, boolean, boolean)} to get the simulation results.
* Client side only!
*/
@Nullable
MultiblockPreviewData getCurrentPreviewMultiblock();

class Helper {
private static final Supplier<ModonomiconAPI> lazyInstance = Suppliers.memoize(() -> {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.klikli_dev.modonomicon.api.multiblock;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Rotation;

public record MultiblockPreviewData(Multiblock multiblock, BlockPos anchor, Rotation facing, boolean isAnchored) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import com.klikli_dev.modonomicon.api.datagen.BookContextHelper;
import com.klikli_dev.modonomicon.api.datagen.CategoryEntryMap;
import com.klikli_dev.modonomicon.api.multiblock.Multiblock;
import com.klikli_dev.modonomicon.api.multiblock.MultiblockPreviewData;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

public class ModonomiconAPIStub implements ModonomiconAPI {
private static final ModonomiconAPIStub instance = new ModonomiconAPIStub();
Expand Down Expand Up @@ -42,4 +44,9 @@ public CategoryEntryMap getEntryMap() {
public Multiblock getMultiblock(ResourceLocation id) {
return null;
}

@Override
public @Nullable MultiblockPreviewData getCurrentPreviewMultiblock() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import com.klikli_dev.modonomicon.api.datagen.BookContextHelper;
import com.klikli_dev.modonomicon.api.datagen.CategoryEntryMap;
import com.klikli_dev.modonomicon.api.multiblock.Multiblock;
import com.klikli_dev.modonomicon.api.multiblock.MultiblockPreviewData;
import com.klikli_dev.modonomicon.client.render.MultiblockPreviewRenderer;
import com.klikli_dev.modonomicon.data.MultiblockDataManager;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

public class ModonomiconAPIImpl implements ModonomiconAPI {
public boolean isStub() {
Expand All @@ -32,4 +35,9 @@ public CategoryEntryMap getEntryMap() {
public Multiblock getMultiblock(ResourceLocation id) {
return MultiblockDataManager.get().getMultiblock(id);
}

@Override
public @Nullable MultiblockPreviewData getCurrentPreviewMultiblock() {
return MultiblockPreviewRenderer.getMultiblockPreviewData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.klikli_dev.modonomicon.api.ModonomiconAPI;
import com.klikli_dev.modonomicon.api.ModonomiconConstants;
import com.klikli_dev.modonomicon.api.multiblock.Multiblock;
import com.klikli_dev.modonomicon.api.multiblock.MultiblockPreviewData;
import com.klikli_dev.modonomicon.client.ClientTicks;
import com.klikli_dev.modonomicon.multiblock.AbstractMultiblock;
import com.klikli_dev.modonomicon.multiblock.matcher.DisplayOnlyMatcher;
Expand Down Expand Up @@ -50,6 +51,7 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;

import java.awt.*;
Expand All @@ -58,10 +60,9 @@

public class MultiblockPreviewRenderer {

public static boolean hasMultiblock;

private static final Map<BlockPos, BlockEntity> blockEntityCache = new Object2ObjectOpenHashMap<>();
private static final Set<BlockEntity> erroredBlockEntities = Collections.newSetFromMap(new WeakHashMap<>());
public static boolean hasMultiblock;
private static Multiblock multiblock;
private static Component name;
private static BlockPos pos;
Expand Down Expand Up @@ -337,6 +338,14 @@ public static void renderBlock(Level world, BlockState state, BlockPos pos, Mult
}
}

@Nullable
public static MultiblockPreviewData getMultiblockPreviewData() {
if (!hasMultiblock) {
return null;
}
return new MultiblockPreviewData(multiblock, pos, facingRotation, isAnchored);
}

public static Multiblock getMultiblock() {
return multiblock;
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ group=com.klikli_dev
mod_id=modonomicon
mod_name=Modonomicon
mod_license=MIT AND CC-BY-4.0
mod_version=1.105.0
mod_version=1.106.0
mod_authors=Kli Kli
mod_description=Data-driven minecraft in-game documentation with progress visualization.

Expand Down

0 comments on commit 1463e45

Please sign in to comment.