Skip to content

Commit 10d4c81

Browse files
committed
Render error boxes on misconfigured structure blocks when trying to save
1 parent 5653a1e commit 10d4c81

File tree

17 files changed

+396
-122
lines changed

17 files changed

+396
-122
lines changed

src/client/java/aztech/modern_industrialization/MIClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import aztech.modern_industrialization.blocks.storage.barrel.DeferredBarrelTextRenderer;
3030
import aztech.modern_industrialization.blocks.storage.barrel.client.BarrelTooltipComponent;
3131
import aztech.modern_industrialization.blocks.storage.tank.TankRenderer;
32+
import aztech.modern_industrialization.blocks.structure.StructureMultiblockBER;
3233
import aztech.modern_industrialization.blocks.structure.StructureMultiblockControllerBER;
3334
import aztech.modern_industrialization.datagen.MIDatagenClient;
3435
import aztech.modern_industrialization.datagen.MIDatagenServer;
@@ -104,6 +105,7 @@
104105
import net.neoforged.neoforge.data.event.GatherDataEvent;
105106
import net.neoforged.neoforge.event.AddPackFindersEvent;
106107
import net.neoforged.neoforge.event.entity.player.ItemTooltipEvent;
108+
import net.neoforged.neoforge.event.level.LevelEvent;
107109

108110
@EventBusSubscriber(value = Dist.CLIENT, modid = MI.ID, bus = EventBusSubscriber.Bus.MOD)
109111
public class MIClient {
@@ -150,6 +152,11 @@ private static void init(FMLConstructModEvent ignored) {
150152
}
151153
}
152154
});
155+
NeoForge.EVENT_BUS.addListener(LevelEvent.Unload.class, event -> {
156+
if (event.getLevel().isClientSide()) {
157+
StructureMultiblockBER.setMisconfigured(List.of());
158+
}
159+
});
153160

154161
modBus.addListener(GatherDataEvent.class, event -> {
155162
MIDatagenClient.configure(
@@ -236,6 +243,8 @@ private static void registerBlockEntityRenderers(FMLClientSetupEvent event) {
236243
BlockEntityRenderers.register(MIRegistries.CREATIVE_BARREL_BE.get(), context -> new BarrelRenderer(0x000000));
237244
BlockEntityRenderers.register(MIRegistries.CREATIVE_TANK_BE.get(), context -> new TankRenderer(0x000000));
238245
BlockEntityRenderers.register(MIRegistries.STRUCTURE_MULTIBLOCK_CONTROLLER_BE.get(), context -> new StructureMultiblockControllerBER());
246+
BlockEntityRenderers.register(MIRegistries.STRUCTURE_MULTIBLOCK_HATCH_BE.get(), context -> new StructureMultiblockBER<>());
247+
BlockEntityRenderers.register(MIRegistries.STRUCTURE_MULTIBLOCK_MEMBER_BE.get(), context -> new StructureMultiblockBER<>());
239248

240249
blockEntityRendererRegistrations.forEach(Runnable::run);
241250
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020 Azercoco & Technici4n
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package aztech.modern_industrialization.blocks.structure;
25+
26+
import aztech.modern_industrialization.util.RenderHelper;
27+
import com.mojang.blaze3d.vertex.PoseStack;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
import net.minecraft.client.renderer.MultiBufferSource;
31+
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
32+
import net.minecraft.core.BlockPos;
33+
import net.minecraft.world.level.block.entity.BlockEntity;
34+
import net.minecraft.world.phys.AABB;
35+
36+
public class StructureMultiblockBER<T extends BlockEntity> implements BlockEntityRenderer<T> {
37+
private static List<BlockPos> MISCONFIGURED_BLOCKS = new ArrayList<>();
38+
39+
public static void setMisconfigured(List<BlockPos> positions) {
40+
MISCONFIGURED_BLOCKS = new ArrayList<>(positions);
41+
}
42+
43+
public static void forgetMisconfigured(List<BlockPos> positions) {
44+
MISCONFIGURED_BLOCKS.removeAll(positions);
45+
}
46+
47+
@Override
48+
public void render(T be, float tickDelta, PoseStack matrices, MultiBufferSource vcp, int light, int overlay) {
49+
BlockPos pos = be.getBlockPos();
50+
if (MISCONFIGURED_BLOCKS.contains(pos) && (System.currentTimeMillis() / 500L) % 2 == 0) {
51+
matrices.pushPose();
52+
matrices.translate(-0.005f, -0.005f, -0.005f);
53+
matrices.scale(1.01f, 1.01f, 1.01f);
54+
RenderHelper.drawOverlay(matrices, vcp, 1.0f, 111f / 256f, 111f / 256f, RenderHelper.FULL_LIGHT, overlay, false);
55+
matrices.popPose();
56+
}
57+
}
58+
59+
@Override
60+
public boolean shouldRenderOffScreen(T be) {
61+
return true;
62+
}
63+
64+
@Override
65+
public AABB getRenderBoundingBox(T be) {
66+
return AABB.INFINITE;
67+
}
68+
}

src/client/java/aztech/modern_industrialization/blocks/structure/StructureMultiblockControllerBER.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,26 @@
2828
import net.minecraft.client.renderer.LevelRenderer;
2929
import net.minecraft.client.renderer.MultiBufferSource;
3030
import net.minecraft.client.renderer.RenderType;
31-
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
3231
import net.minecraft.core.BlockPos;
3332
import net.minecraft.world.phys.AABB;
3433

35-
public class StructureMultiblockControllerBER implements BlockEntityRenderer<StructureMultiblockControllerBlockEntity> {
34+
public class StructureMultiblockControllerBER extends StructureMultiblockBER<StructureMultiblockControllerBlockEntity> {
3635
@Override
37-
public void render(StructureMultiblockControllerBlockEntity controller, float tickDelta, PoseStack matrices, MultiBufferSource vcs, int light,
36+
public void render(StructureMultiblockControllerBlockEntity be, float tickDelta, PoseStack matrices, MultiBufferSource vcp, int light,
3837
int overlay) {
39-
if (!controller.shouldShowBounds()) {
38+
super.render(be, tickDelta, matrices, vcp, light, overlay);
39+
40+
if (!be.shouldShowBounds()) {
4041
return;
4142
}
42-
BlockPos pos = controller.getBlockPos();
43-
StructureControllerBounds bounds = controller.getBounds();
43+
BlockPos pos = be.getBlockPos();
44+
StructureControllerBounds bounds = be.getBounds();
4445
AABB box = bounds.aabb();
4546
if (box != null) {
4647
matrices.pushPose();
47-
VertexConsumer buffer = vcs.getBuffer(RenderType.lines());
48+
VertexConsumer buffer = vcp.getBuffer(RenderType.lines());
4849
LevelRenderer.renderLineBox(matrices, buffer, box, 1, 1, 1, 1);
4950
matrices.popPose();
5051
}
5152
}
52-
53-
@Override
54-
public boolean shouldRenderOffScreen(StructureMultiblockControllerBlockEntity controller) {
55-
return true;
56-
}
57-
58-
@Override
59-
public AABB getRenderBoundingBox(StructureMultiblockControllerBlockEntity controller) {
60-
return AABB.INFINITE;
61-
}
6253
}

src/client/java/aztech/modern_industrialization/client/MIRenderTypes.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525

2626
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
2727
import com.mojang.blaze3d.vertex.VertexFormat;
28+
import net.minecraft.client.renderer.RenderStateShard;
2829
import net.minecraft.client.renderer.RenderType;
2930

3031
public class MIRenderTypes {
3132
private static RenderType MACHINE_WRENCH_OVERLAY;
32-
private static RenderType SOLID_HIGHLIGHT; // used by hatch preview and wrong block highlight
33+
// used by hatch preview and wrong block highlight
34+
private static RenderType SOLID_HIGHLIGHT_DEPTH;
35+
private static RenderType SOLID_HIGHLIGHT_NO_DEPTH;
3336

3437
public static RenderType machineOverlay() {
3538
if (MACHINE_WRENCH_OVERLAY == null) {
@@ -38,11 +41,18 @@ public static RenderType machineOverlay() {
3841
return MACHINE_WRENCH_OVERLAY;
3942
}
4043

41-
public static RenderType solidHighlight() {
42-
if (SOLID_HIGHLIGHT == null) {
43-
SOLID_HIGHLIGHT = Factory.makeSolidHighlight();
44+
public static RenderType solidHighlight(boolean depth) {
45+
if (depth) {
46+
if (SOLID_HIGHLIGHT_DEPTH == null) {
47+
SOLID_HIGHLIGHT_DEPTH = Factory.makeSolidHighlight(true);
48+
}
49+
return SOLID_HIGHLIGHT_DEPTH;
50+
} else {
51+
if (SOLID_HIGHLIGHT_NO_DEPTH == null) {
52+
SOLID_HIGHLIGHT_NO_DEPTH = Factory.makeSolidHighlight(false);
53+
}
54+
return SOLID_HIGHLIGHT_NO_DEPTH;
4455
}
45-
return SOLID_HIGHLIGHT;
4656
}
4757

4858
// This is a subclass to get access to a bunch of fields and classes.
@@ -63,14 +73,17 @@ private static RenderType makeMachineOverlay() {
6373
.createCompositeState(false));
6474
}
6575

66-
private static RenderType makeSolidHighlight() {
67-
return create("solid_highlight", DefaultVertexFormat.POSITION_COLOR_NORMAL, VertexFormat.Mode.QUADS, 65536, false, false,
68-
CompositeState.builder()
69-
.setTransparencyState(NO_TRANSPARENCY)
70-
.setTextureState(NO_TEXTURE)
71-
.setLightmapState(NO_LIGHTMAP)
72-
.setShaderState(POSITION_COLOR_SHADER)
73-
.createCompositeState(false));
76+
private static RenderType makeSolidHighlight(boolean depth) {
77+
var builder = CompositeState.builder()
78+
.setTransparencyState(NO_TRANSPARENCY)
79+
.setTextureState(NO_TEXTURE)
80+
.setLightmapState(NO_LIGHTMAP)
81+
.setShaderState(POSITION_COLOR_SHADER);
82+
if (!depth) {
83+
builder = builder.setDepthTestState(RenderStateShard.NO_DEPTH_TEST);
84+
}
85+
return create("solid_highlight" + (depth ? "_depth" : "_no_depth"), DefaultVertexFormat.POSITION_COLOR_NORMAL, VertexFormat.Mode.QUADS,
86+
65536, false, false, builder.createCompositeState(false));
7487
}
7588
}
7689
}

src/client/java/aztech/modern_industrialization/proxy/ClientProxy.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import aztech.modern_industrialization.blocks.storage.barrel.BarrelRenderer;
3030
import aztech.modern_industrialization.blocks.storage.tank.AbstractTankBlockEntity;
3131
import aztech.modern_industrialization.blocks.storage.tank.TankRenderer;
32+
import aztech.modern_industrialization.blocks.structure.StructureMultiblockBER;
3233
import aztech.modern_industrialization.blocks.structure.StructureMultiblockControllerBlockEntity;
3334
import aztech.modern_industrialization.blocks.structure.StructureMultiblockHatchBlockEntity;
3435
import aztech.modern_industrialization.blocks.structure.StructureMultiblockMemberBlockEntity;
@@ -43,6 +44,8 @@
4344
import aztech.modern_industrialization.machines.models.UseBlockModelBakedModel;
4445
import aztech.modern_industrialization.machines.multiblocks.HatchBlockEntity;
4546
import aztech.modern_industrialization.machines.multiblocks.MultiblockMachineBlockEntity;
47+
import aztech.modern_industrialization.network.BasePacket;
48+
import aztech.modern_industrialization.network.structure.StructureMisconfiguredBlocksPacket;
4649
import aztech.modern_industrialization.textures.TextureHelper;
4750
import aztech.modern_industrialization.util.RenderHelper;
4851
import java.util.Objects;
@@ -179,4 +182,13 @@ public void openStructureMultiblockMemberScreen(Player player, StructureMultiblo
179182
Minecraft.getInstance().setScreen(new StructureMultiblockMemberEditScreen(member));
180183
}
181184
}
185+
186+
@Override
187+
public void receiveStructureMisconfiguredBlocksPacket(StructureMisconfiguredBlocksPacket packet, BasePacket.Context ctx) {
188+
if (packet.forget()) {
189+
StructureMultiblockBER.forgetMisconfigured(packet.positions());
190+
} else {
191+
StructureMultiblockBER.setMisconfigured(packet.positions());
192+
}
193+
}
182194
}

src/client/java/aztech/modern_industrialization/util/RenderHelper.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,17 @@ public class RenderHelper {
7575
private static final float W = 0.05f;
7676
private static final ResourceLocation LOCKED_TEXTURE_LOCATION = MI.id("block/locked");
7777

78-
public static void drawOverlay(PoseStack ms, MultiBufferSource vcp, float r, float g, float b, int light, int overlay) {
79-
VertexConsumer vc = vcp.getBuffer(MIRenderTypes.solidHighlight());
78+
public static void drawOverlay(PoseStack ms, MultiBufferSource vcp, float r, float g, float b, int light, int overlay, boolean depth) {
79+
VertexConsumer vc = vcp.getBuffer(MIRenderTypes.solidHighlight(depth));
8080
for (BakedQuad overlayQuad : OVERLAY_QUADS.get()) {
8181
vc.putBulkData(ms.last(), overlayQuad, r, g, b, 1.0f, light, overlay);
8282
}
8383
}
8484

85+
public static void drawOverlay(PoseStack ms, MultiBufferSource vcp, float r, float g, float b, int light, int overlay) {
86+
drawOverlay(ms, vcp, r, g, b, light, overlay, true);
87+
}
88+
8589
static {
8690
OVERLAY_QUADS = Suppliers.memoize(() -> {
8791
var overlayQuads = new BakedQuad[24];
@@ -103,13 +107,17 @@ public static void drawOverlay(PoseStack ms, MultiBufferSource vcp, float r, flo
103107

104108
private static final Supplier<BakedQuad[]> CUBE_QUADS;
105109

106-
public static void drawCube(PoseStack ms, MultiBufferSource vcp, float r, float g, float b, int light, int overlay) {
107-
VertexConsumer vc = vcp.getBuffer(MIRenderTypes.solidHighlight());
110+
public static void drawCube(PoseStack ms, MultiBufferSource vcp, float r, float g, float b, int light, int overlay, boolean depth) {
111+
VertexConsumer vc = vcp.getBuffer(MIRenderTypes.solidHighlight(depth));
108112
for (BakedQuad cubeQuad : CUBE_QUADS.get()) {
109113
vc.putBulkData(ms.last(), cubeQuad, r, g, b, 1.0f, light, overlay);
110114
}
111115
}
112116

117+
public static void drawCube(PoseStack ms, MultiBufferSource vcp, float r, float g, float b, int light, int overlay) {
118+
drawCube(ms, vcp, r, g, b, light, overlay, true);
119+
}
120+
113121
static {
114122
CUBE_QUADS = Suppliers.memoize(() -> {
115123
var cubeQuads = new BakedQuad[6];

src/generated/resources/assets/modern_industrialization/lang/en_us.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,7 @@
16991699
"text.modern_industrialization.StructureMultiblockMemberMembers": "Members",
17001700
"text.modern_industrialization.StructureMultiblockMemberPreview": "Preview",
17011701
"text.modern_industrialization.StructureMultiblockSaveFailInvalidBounds": "Failed to save structure. The bounds provided are not valid.",
1702-
"text.modern_industrialization.StructureMultiblockSaveFailMisconfiguredBlock": "Failed to save structure. The block at (%s) is not properly configured.",
1702+
"text.modern_industrialization.StructureMultiblockSaveFailMisconfiguredBlock": "Failed to save structure. Some blocks are not properly configured.",
17031703
"text.modern_industrialization.StructureMultiblockSaveFailNoController": "Failed to save structure. No controller could be found within the bounds.",
17041704
"text.modern_industrialization.StructureMultiblockSaveFailNoHatches": "Failed to save structure. No hatches could be found within the bounds.",
17051705
"text.modern_industrialization.StructureMultiblockSaveFailTooManyControllers": "Failed to save structure. Too many controllers exist within the bounds.",

src/main/java/aztech/modern_industrialization/MIText.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public enum MIText {
252252
StructureMultiblockMemberPreview("Preview"),
253253
StructureMultiblockStructureName("Structure Name"),
254254
StructureMultiblockSaveFailInvalidBounds("Failed to save structure. The bounds provided are not valid."),
255-
StructureMultiblockSaveFailMisconfiguredBlock("Failed to save structure. The block at (%s) is not properly configured."),
255+
StructureMultiblockSaveFailMisconfiguredBlock("Failed to save structure. Some blocks are not properly configured."),
256256
StructureMultiblockSaveFailTooManyControllers("Failed to save structure. Too many controllers exist within the bounds."),
257257
StructureMultiblockSaveFailNoController("Failed to save structure. No controller could be found within the bounds."),
258258
StructureMultiblockSaveFailNoHatches("Failed to save structure. No hatches could be found within the bounds."),

0 commit comments

Comments
 (0)