Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions docs/ADDING_MACHINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,12 @@ remember that the top, side and bottom textures of a casing must be `modern_indu
For example:
```js
MIMachineEvents.registerCasings(event => {
// Register two casings.
// Register a casing.
// This doesn't register any model! Either add models or add the top/side/bottom textures.
event.register("my_fancy_casing", "my_other_casing");
event.register("fancy_casing", "Fancy");

// This registers a new casing with the same model as a diamond block!
event.registerBlockImitation("my_diamond_casing", "minecraft:diamond_block");
event.registerBlockImitation("diamond_casing", "Diamond", "minecraft:diamond_block");
})
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ protected void registerModels() {
}

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

private void cubeBottomTop(MachineCasing casing, String side, String bottom, String top) {
cubeBottomTop(casing.name, MI.id(side), MI.id(bottom), MI.id(top));
cubeBottomTop(casing.key.toString(), MI.id(side), MI.id(bottom), MI.id(top));
}

private void cubeAll(MachineCasing casing, String side) {
cubeAll(casing.name, MI.id(side));
cubeAll(casing.key.toString(), MI.id(side));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private BakedQuad getCachedQuad(MachineModelClientData data, Direction d) {
var cachedQuads = quadCache.computeIfAbsent(casing, c -> new Object[36]);

if (cachedQuads[cachedQuadIndex] == null) {
TextureAtlasSprite sprite = model == null ? null : MachineBakedModel.getSprite(model.getSprites(casing), d, facing, true);
TextureAtlasSprite sprite = model == null ? null : model.getSprite(model.getSprites(casing), d, facing, true);
if (sprite != null) {
var vc = new QuadBakingVertexConsumer();
cachedQuads[cachedQuadIndex] = ModelHelper.bakeSprite(vc, d, sprite, -2 * MachineBakedModel.Z_OFFSET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package aztech.modern_industrialization.machines.models;

import aztech.modern_industrialization.MI;
import aztech.modern_industrialization.util.ModelHelper;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -39,6 +38,7 @@
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -57,22 +57,25 @@ public class MachineBakedModel implements IDynamicBakedModel {
public static final String CASING_FOLDER = "machine_casing";

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

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

private final MachineCasing baseCasing;
private final int[] outputOverlayIndexes;
private final TextureAtlasSprite[] defaultOverlays;
private final Map<String, TextureAtlasSprite[]> tieredOverlays;
private final Map<ResourceLocation, TextureAtlasSprite[]> tieredOverlays;
private final MachineModelClientData defaultData;

MachineBakedModel(MachineCasing baseCasing,
TextureAtlasSprite[] defaultOverlays,
Map<String, TextureAtlasSprite[]> tieredOverlays) {
public MachineBakedModel(MachineCasing baseCasing,
int[] outputOverlayIndexes, TextureAtlasSprite[] defaultOverlays,
Map<ResourceLocation, TextureAtlasSprite[]> tieredOverlays) {
this.baseCasing = baseCasing;
this.outputOverlayIndexes = outputOverlayIndexes;
this.defaultOverlays = defaultOverlays;
this.tieredOverlays = tieredOverlays;
this.defaultData = new MachineModelClientData(baseCasing, Direction.NORTH);
Expand All @@ -86,14 +89,14 @@ public TextureAtlasSprite[] getSprites(@Nullable MachineCasing casing) {
if (casing == null) {
return defaultOverlays;
}
return tieredOverlays.getOrDefault(casing.name, defaultOverlays);
return tieredOverlays.getOrDefault(casing.key, defaultOverlays);
}

/**
* Returns null if nothing should be rendered.
*/
@Nullable
public static TextureAtlasSprite getSprite(TextureAtlasSprite[] sprites, Direction side, Direction facingDirection, boolean isActive) {
public TextureAtlasSprite getSprite(TextureAtlasSprite[] sprites, Direction side, Direction facingDirection, boolean isActive) {
int spriteId;
if (side.getAxis().isHorizontal()) {
spriteId = (facingDirection.get2DDataValue() - side.get2DDataValue() + 4) % 4 * 2;
Expand Down Expand Up @@ -121,44 +124,50 @@ public ModelData getModelData(BlockAndTintGetter level, BlockPos pos, BlockState
return getCasingModel(casing).getModelData(level, pos, state, modelData);
}

@Override
public @NotNull List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @NotNull RandomSource rand,
@NotNull ModelData extraData, @Nullable RenderType renderType) {
var data = extraData.get(MachineModelClientData.KEY);
if (data == null) {
data = defaultData;
}

MachineCasing casing = Objects.requireNonNullElse(data.casing, baseCasing);
var sprites = getSprites(casing);

protected @NotNull List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @NotNull RandomSource rand,
@NotNull ModelData extraData, @Nullable RenderType renderType,
@NotNull MachineModelClientData data, @NotNull MachineCasing casing,
@NotNull TextureAtlasSprite[] sprites, @NotNull QuadBakingVertexConsumer vertexConsumer) {
List<BakedQuad> quads = new ArrayList<>();
var vc = new QuadBakingVertexConsumer();

if (side != null) {
// Casing
quads.addAll(getCasingModel(casing).getQuads(state, side, rand, extraData, renderType));
// Machine overlays

TextureAtlasSprite sprite = getSprite(sprites, side, data.frontDirection, false);
if (sprite != null) {
quads.add(ModelHelper.bakeSprite(vc, side, sprite, -Z_OFFSET));
quads.add(ModelHelper.bakeSprite(vertexConsumer, side, sprite, -Z_OFFSET));
}
}

// Output overlays
if (data.outputDirection != null && side == data.outputDirection) {
quads.add(ModelHelper.bakeSprite(vc, data.outputDirection, sprites[24], -3 * Z_OFFSET));
quads.add(ModelHelper.bakeSprite(vertexConsumer, data.outputDirection, sprites[outputOverlayIndexes[0]], -3 * Z_OFFSET));
if (data.itemAutoExtract) {
quads.add(ModelHelper.bakeSprite(vc, data.outputDirection, sprites[25], -3 * Z_OFFSET));
quads.add(ModelHelper.bakeSprite(vertexConsumer, data.outputDirection, sprites[outputOverlayIndexes[1]], -3 * Z_OFFSET));
}
if (data.fluidAutoExtract) {
quads.add(ModelHelper.bakeSprite(vc, data.outputDirection, sprites[26], -3 * Z_OFFSET));
quads.add(ModelHelper.bakeSprite(vertexConsumer, data.outputDirection, sprites[outputOverlayIndexes[2]], -3 * Z_OFFSET));
}
}

return quads;
}

@Override
public @NotNull List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @NotNull RandomSource rand,
@NotNull ModelData extraData, @Nullable RenderType renderType) {
var data = extraData.get(MachineModelClientData.KEY);
if (data == null) {
data = defaultData;
}

MachineCasing casing = Objects.requireNonNullElse(data.casing, baseCasing);
var sprites = getSprites(casing);

var vc = new QuadBakingVertexConsumer();

return getQuads(state, side, rand, extraData, renderType, data, casing, sprites, vc);
}

@Override
public boolean useAmbientOcclusion() {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* MIT License
*
* Copyright (c) 2020 Azercoco & Technici4n
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package aztech.modern_industrialization.machines.models;

import java.util.Map;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.resources.ResourceLocation;

public interface MachineModelBaker {
MachineBakedModel bake(MachineCasing baseCasing,
int[] outputOverlayIndexes, TextureAtlasSprite[] defaultOverlays,
Map<ResourceLocation, TextureAtlasSprite[]> tieredOverlays);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* MIT License
*
* Copyright (c) 2020 Azercoco & Technici4n
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package aztech.modern_industrialization.machines.models;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import java.lang.reflect.Field;
import net.minecraft.client.resources.model.Material;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.InventoryMenu;

public interface MachineOverlaysJson {
Material[] toSpriteIds();

int[] getOutputSpriteIndexes();

Gson GSON = new GsonBuilder().registerTypeAdapter(ResourceLocation.class, new ResourceLocation.Serializer()).create();

static <O extends MachineOverlaysJson> O parse(Class<O> clazz, JsonObject json, MachineOverlaysJson defaultOverlay) {
O overlays = GSON.fromJson(json, clazz);

if (defaultOverlay != null) {
try {
for (Field field : clazz.getDeclaredFields()) {
if (field.get(overlays) == null) {
field.set(overlays, field.get(defaultOverlay));
}
}
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to copy fields from default overlay", ex);
}
}

return overlays;
}

default Material select(ResourceLocation... candidates) {
for (ResourceLocation id : candidates) {
if (id != null) {
return new Material(InventoryMenu.BLOCK_ATLAS, id);
}
}
return null;
}
}
Loading
Loading