Skip to content

Commit 48d5120

Browse files
committed
Merge "Various internal machine api improvements" (#900)
1 parent a52a091 commit 48d5120

37 files changed

+560
-400
lines changed

src/client/java/aztech/modern_industrialization/compat/viewer/impl/MachineScreenPredicateTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@
2424
package aztech.modern_industrialization.compat.viewer.impl;
2525

2626
import aztech.modern_industrialization.compat.rei.machines.ReiMachineRecipes;
27-
import aztech.modern_industrialization.machines.gui.GuiComponentClient;
2827
import aztech.modern_industrialization.machines.gui.MachineScreen;
2928
import aztech.modern_industrialization.machines.guicomponents.CraftingMultiblockGuiClient;
29+
import java.util.Optional;
3030

3131
public class MachineScreenPredicateTest {
3232
public static boolean test(ReiMachineRecipes.MachineScreenPredicate predicate, MachineScreen screen) {
3333
return switch (predicate) {
3434
case ANY -> true;
35-
case MULTIBLOCK -> {
36-
for (GuiComponentClient client : screen.getMenu().components) {
37-
if (client instanceof CraftingMultiblockGuiClient cmGui) {
38-
if (cmGui.isShapeValid) {
39-
yield true;
35+
case MULTIBLOCK -> screen.getMenu().components.findOrDefault(
36+
client -> {
37+
if (client instanceof CraftingMultiblockGuiClient cmGui) {
38+
if (cmGui.isShapeValid) {
39+
return Optional.of(true);
40+
}
4041
}
41-
}
42-
}
43-
yield false;
44-
}
42+
return Optional.empty();
43+
},
44+
false);
4545
};
4646
}
4747
}

src/client/java/aztech/modern_industrialization/datagen/model/MachineCasingsProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ protected void registerModels() {
8181
}
8282

8383
private void imitateBlock(MachineCasing casing, Block block) {
84-
getBuilder(casing.name)
84+
getBuilder(casing.key.toString())
8585
.customLoader((bmb, existingFileHelper) -> new UseBlockModelModelBuilder<>(block, bmb, existingFileHelper));
8686
}
8787

8888
private void cubeBottomTop(MachineCasing casing, String side, String bottom, String top) {
89-
cubeBottomTop(casing.name, MI.id(side), MI.id(bottom), MI.id(top));
89+
cubeBottomTop(casing.key.toString(), MI.id(side), MI.id(bottom), MI.id(top));
9090
}
9191

9292
private void cubeAll(MachineCasing casing, String side) {
93-
cubeAll(casing.name, MI.id(side));
93+
cubeAll(casing.key.toString(), MI.id(side));
9494
}
9595

9696
@Override

src/client/java/aztech/modern_industrialization/machines/MachineBlockEntityRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private BakedQuad getCachedQuad(MachineModelClientData data, Direction d) {
6969
var cachedQuads = quadCache.computeIfAbsent(casing, c -> new Object[36]);
7070

7171
if (cachedQuads[cachedQuadIndex] == null) {
72-
TextureAtlasSprite sprite = model == null ? null : MachineBakedModel.getSprite(model.getSprites(casing), d, facing, true);
72+
TextureAtlasSprite sprite = model == null ? null : model.getSprite(model.getSprites(casing), d, facing, true);
7373
if (sprite != null) {
7474
var vc = new QuadBakingVertexConsumer();
7575
cachedQuads[cachedQuadIndex] = ModelHelper.bakeSprite(vc, d, sprite, -2 * MachineBakedModel.Z_OFFSET);

src/client/java/aztech/modern_industrialization/machines/gui/MachineMenuClient.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import aztech.modern_industrialization.inventory.ConfigurableItemStack;
2828
import aztech.modern_industrialization.inventory.MIInventory;
2929
import aztech.modern_industrialization.inventory.SlotPositions;
30+
import aztech.modern_industrialization.machines.ComponentStorage;
3031
import aztech.modern_industrialization.machines.GuiComponentsClient;
3132
import aztech.modern_industrialization.util.NbtHelper;
3233
import java.util.ArrayList;
@@ -52,34 +53,29 @@ public static MachineMenuClient create(int syncId, Inventory playerInventory, Re
5253
SlotPositions fluidPositions = SlotPositions.read(buf);
5354
MIInventory inventory = new MIInventory(itemStacks, fluidStacks, itemPositions, fluidPositions);
5455
// Components
55-
List<GuiComponentClient> components = new ArrayList<>();
56+
ComponentStorage<GuiComponentClient> components = new ComponentStorage<>();
5657
int componentCount = buf.readInt();
5758
for (int i = 0; i < componentCount; ++i) {
5859
ResourceLocation id = buf.readResourceLocation();
59-
components.add(GuiComponentsClient.get(id).createFromInitialData(buf));
60+
components.register(GuiComponentsClient.get(id).createFromInitialData(buf));
6061
}
6162
// GUI params
6263
MachineGuiParameters guiParams = MachineGuiParameters.read(buf);
6364

6465
return new MachineMenuClient(syncId, playerInventory, inventory, components, guiParams);
6566
}
6667

67-
public final List<GuiComponentClient> components;
68+
public final ComponentStorage<GuiComponentClient> components;
6869

69-
private MachineMenuClient(int syncId, Inventory playerInventory, MIInventory inventory, List<GuiComponentClient> components,
70+
private MachineMenuClient(int syncId, Inventory playerInventory, MIInventory inventory, ComponentStorage<GuiComponentClient> components,
7071
MachineGuiParameters guiParams) {
7172
super(syncId, playerInventory, inventory, guiParams, components);
7273
this.components = components;
7374
}
7475

7576
@Nullable
7677
public <T extends GuiComponentClient> T getComponent(Class<T> klass) {
77-
for (GuiComponentClient component : components) {
78-
if (klass.isInstance(component)) {
79-
return (T) component;
80-
}
81-
}
82-
return null;
78+
return components.get(klass).orElse(null);
8379
}
8480

8581
@Override

src/client/java/aztech/modern_industrialization/machines/gui/MachineScreen.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ public class MachineScreen extends MIHandledScreen<MachineMenuClient> implements
6464
public MachineScreen(MachineMenuClient handler, Inventory inventory, Component title) {
6565
super(handler, inventory, title);
6666

67-
for (GuiComponentClient component : handler.components) {
68-
renderers.add(component.createRenderer(this));
69-
}
67+
handler.components.forEach(component -> renderers.add(component.createRenderer(this)));
7068

7169
this.imageHeight = handler.guiParams.backgroundHeight;
7270
this.imageWidth = handler.guiParams.backgroundWidth;
@@ -211,10 +209,15 @@ protected void renderBg(GuiGraphics guiGraphics, float delta, int mouseX, int mo
211209

212210
private void renderConfigurableSlotBackgrounds(GuiGraphics guiGraphics) {
213211
for (Slot slot : this.menu.slots) {
214-
if (slot instanceof BackgroundRenderedSlot brs) {
212+
if (slot.isActive() && slot instanceof BackgroundRenderedSlot brs) {
215213
int px = leftPos + slot.x - 1;
216214
int py = topPos + slot.y - 1;
217-
guiGraphics.blit(SLOT_ATLAS, px, py, brs.getBackgroundU(), brs.getBackgroundV(), 18, 18);
215+
if (slot.getItem().isEmpty()) {
216+
var atlas = brs.getBackgroundAtlasLocation();
217+
guiGraphics.blit(atlas == null ? SLOT_ATLAS : atlas, px, py, brs.getBackgroundU(), brs.getBackgroundV(), 18, 18);
218+
} else {
219+
guiGraphics.blit(SLOT_ATLAS, px, py, 0, 0, 18, 18);
220+
}
218221
}
219222
}
220223
}

src/client/java/aztech/modern_industrialization/machines/models/MachineBakedModel.java

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package aztech.modern_industrialization.machines.models;
2525

26-
import aztech.modern_industrialization.MI;
2726
import aztech.modern_industrialization.util.ModelHelper;
2827
import java.util.ArrayList;
2928
import java.util.List;
@@ -39,6 +38,7 @@
3938
import net.minecraft.client.resources.model.ModelResourceLocation;
4039
import net.minecraft.core.BlockPos;
4140
import net.minecraft.core.Direction;
41+
import net.minecraft.resources.ResourceLocation;
4242
import net.minecraft.util.RandomSource;
4343
import net.minecraft.world.level.BlockAndTintGetter;
4444
import net.minecraft.world.level.block.state.BlockState;
@@ -57,22 +57,25 @@ public class MachineBakedModel implements IDynamicBakedModel {
5757
public static final String CASING_FOLDER = "machine_casing";
5858

5959
public static ModelResourceLocation getCasingModelId(MachineCasing casing) {
60-
return ModelResourceLocation.standalone(MI.id(CASING_FOLDER + "/" + casing.name));
60+
return ModelResourceLocation
61+
.standalone(ResourceLocation.fromNamespaceAndPath(casing.key.getNamespace(), CASING_FOLDER + "/" + casing.key.getPath()));
6162
}
6263

6364
public static BakedModel getCasingModel(MachineCasing casing) {
6465
return Minecraft.getInstance().getModelManager().getModel(getCasingModelId(casing));
6566
}
6667

6768
private final MachineCasing baseCasing;
69+
private final int[] outputOverlayIndexes;
6870
private final TextureAtlasSprite[] defaultOverlays;
69-
private final Map<String, TextureAtlasSprite[]> tieredOverlays;
71+
private final Map<ResourceLocation, TextureAtlasSprite[]> tieredOverlays;
7072
private final MachineModelClientData defaultData;
7173

72-
MachineBakedModel(MachineCasing baseCasing,
73-
TextureAtlasSprite[] defaultOverlays,
74-
Map<String, TextureAtlasSprite[]> tieredOverlays) {
74+
public MachineBakedModel(MachineCasing baseCasing,
75+
int[] outputOverlayIndexes, TextureAtlasSprite[] defaultOverlays,
76+
Map<ResourceLocation, TextureAtlasSprite[]> tieredOverlays) {
7577
this.baseCasing = baseCasing;
78+
this.outputOverlayIndexes = outputOverlayIndexes;
7679
this.defaultOverlays = defaultOverlays;
7780
this.tieredOverlays = tieredOverlays;
7881
this.defaultData = new MachineModelClientData(baseCasing, Direction.NORTH);
@@ -86,14 +89,14 @@ public TextureAtlasSprite[] getSprites(@Nullable MachineCasing casing) {
8689
if (casing == null) {
8790
return defaultOverlays;
8891
}
89-
return tieredOverlays.getOrDefault(casing.name, defaultOverlays);
92+
return tieredOverlays.getOrDefault(casing.key, defaultOverlays);
9093
}
9194

9295
/**
9396
* Returns null if nothing should be rendered.
9497
*/
9598
@Nullable
96-
public static TextureAtlasSprite getSprite(TextureAtlasSprite[] sprites, Direction side, Direction facingDirection, boolean isActive) {
99+
public TextureAtlasSprite getSprite(TextureAtlasSprite[] sprites, Direction side, Direction facingDirection, boolean isActive) {
97100
int spriteId;
98101
if (side.getAxis().isHorizontal()) {
99102
spriteId = (facingDirection.get2DDataValue() - side.get2DDataValue() + 4) % 4 * 2;
@@ -121,44 +124,50 @@ public ModelData getModelData(BlockAndTintGetter level, BlockPos pos, BlockState
121124
return getCasingModel(casing).getModelData(level, pos, state, modelData);
122125
}
123126

124-
@Override
125-
public @NotNull List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @NotNull RandomSource rand,
126-
@NotNull ModelData extraData, @Nullable RenderType renderType) {
127-
var data = extraData.get(MachineModelClientData.KEY);
128-
if (data == null) {
129-
data = defaultData;
130-
}
131-
132-
MachineCasing casing = Objects.requireNonNullElse(data.casing, baseCasing);
133-
var sprites = getSprites(casing);
134-
127+
protected @NotNull List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @NotNull RandomSource rand,
128+
@NotNull ModelData extraData, @Nullable RenderType renderType,
129+
@NotNull MachineModelClientData data, @NotNull MachineCasing casing,
130+
@NotNull TextureAtlasSprite[] sprites, @NotNull QuadBakingVertexConsumer vertexConsumer) {
135131
List<BakedQuad> quads = new ArrayList<>();
136-
var vc = new QuadBakingVertexConsumer();
137132

138133
if (side != null) {
139-
// Casing
140134
quads.addAll(getCasingModel(casing).getQuads(state, side, rand, extraData, renderType));
141-
// Machine overlays
135+
142136
TextureAtlasSprite sprite = getSprite(sprites, side, data.frontDirection, false);
143137
if (sprite != null) {
144-
quads.add(ModelHelper.bakeSprite(vc, side, sprite, -Z_OFFSET));
138+
quads.add(ModelHelper.bakeSprite(vertexConsumer, side, sprite, -Z_OFFSET));
145139
}
146140
}
147141

148-
// Output overlays
149142
if (data.outputDirection != null && side == data.outputDirection) {
150-
quads.add(ModelHelper.bakeSprite(vc, data.outputDirection, sprites[24], -3 * Z_OFFSET));
143+
quads.add(ModelHelper.bakeSprite(vertexConsumer, data.outputDirection, sprites[outputOverlayIndexes[0]], -3 * Z_OFFSET));
151144
if (data.itemAutoExtract) {
152-
quads.add(ModelHelper.bakeSprite(vc, data.outputDirection, sprites[25], -3 * Z_OFFSET));
145+
quads.add(ModelHelper.bakeSprite(vertexConsumer, data.outputDirection, sprites[outputOverlayIndexes[1]], -3 * Z_OFFSET));
153146
}
154147
if (data.fluidAutoExtract) {
155-
quads.add(ModelHelper.bakeSprite(vc, data.outputDirection, sprites[26], -3 * Z_OFFSET));
148+
quads.add(ModelHelper.bakeSprite(vertexConsumer, data.outputDirection, sprites[outputOverlayIndexes[2]], -3 * Z_OFFSET));
156149
}
157150
}
158151

159152
return quads;
160153
}
161154

155+
@Override
156+
public @NotNull List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @NotNull RandomSource rand,
157+
@NotNull ModelData extraData, @Nullable RenderType renderType) {
158+
var data = extraData.get(MachineModelClientData.KEY);
159+
if (data == null) {
160+
data = defaultData;
161+
}
162+
163+
MachineCasing casing = Objects.requireNonNullElse(data.casing, baseCasing);
164+
var sprites = getSprites(casing);
165+
166+
var vc = new QuadBakingVertexConsumer();
167+
168+
return getQuads(state, side, rand, extraData, renderType, data, casing, sprites, vc);
169+
}
170+
162171
@Override
163172
public boolean useAmbientOcclusion() {
164173
return true;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.machines.models;
25+
26+
import java.util.Map;
27+
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
28+
import net.minecraft.resources.ResourceLocation;
29+
30+
public interface MachineModelBaker {
31+
MachineBakedModel bake(MachineCasing baseCasing,
32+
int[] outputOverlayIndexes, TextureAtlasSprite[] defaultOverlays,
33+
Map<ResourceLocation, TextureAtlasSprite[]> tieredOverlays);
34+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.machines.models;
25+
26+
import com.google.gson.Gson;
27+
import com.google.gson.GsonBuilder;
28+
import com.google.gson.JsonObject;
29+
import java.lang.reflect.Field;
30+
import net.minecraft.client.resources.model.Material;
31+
import net.minecraft.resources.ResourceLocation;
32+
import net.minecraft.world.inventory.InventoryMenu;
33+
34+
public interface MachineOverlaysJson {
35+
Material[] toSpriteIds();
36+
37+
int[] getOutputSpriteIndexes();
38+
39+
Gson GSON = new GsonBuilder().registerTypeAdapter(ResourceLocation.class, new ResourceLocation.Serializer()).create();
40+
41+
static <O extends MachineOverlaysJson> O parse(Class<O> clazz, JsonObject json, MachineOverlaysJson defaultOverlay) {
42+
O overlays = GSON.fromJson(json, clazz);
43+
44+
if (defaultOverlay != null) {
45+
try {
46+
for (Field field : clazz.getDeclaredFields()) {
47+
if (field.get(overlays) == null) {
48+
field.set(overlays, field.get(defaultOverlay));
49+
}
50+
}
51+
} catch (IllegalAccessException ex) {
52+
throw new RuntimeException("Failed to copy fields from default overlay", ex);
53+
}
54+
}
55+
56+
return overlays;
57+
}
58+
59+
/**
60+
* Select first non-null id, and convert it to a sprite id.
61+
*/
62+
default Material select(ResourceLocation... candidates) {
63+
for (ResourceLocation id : candidates) {
64+
if (id != null) {
65+
return new Material(InventoryMenu.BLOCK_ATLAS, id);
66+
}
67+
}
68+
return null;
69+
}
70+
}

0 commit comments

Comments
 (0)