Skip to content

Commit df526c6

Browse files
committed
Make machine model api more extensible and make machine casings use resource locations
1 parent a52a091 commit df526c6

File tree

11 files changed

+195
-98
lines changed

11 files changed

+195
-98
lines changed

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/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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
default Material select(ResourceLocation... candidates) {
60+
for (ResourceLocation id : candidates) {
61+
if (id != null) {
62+
return new Material(InventoryMenu.BLOCK_ATLAS, id);
63+
}
64+
}
65+
return null;
66+
}
67+
}

0 commit comments

Comments
 (0)