Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@
package aztech.modern_industrialization.compat.viewer.impl;

import aztech.modern_industrialization.compat.rei.machines.ReiMachineRecipes;
import aztech.modern_industrialization.machines.gui.GuiComponentClient;
import aztech.modern_industrialization.machines.gui.MachineScreen;
import aztech.modern_industrialization.machines.guicomponents.CraftingMultiblockGuiClient;
import java.util.Optional;

public class MachineScreenPredicateTest {
public static boolean test(ReiMachineRecipes.MachineScreenPredicate predicate, MachineScreen screen) {
return switch (predicate) {
case ANY -> true;
case MULTIBLOCK -> {
for (GuiComponentClient client : screen.getMenu().components) {
if (client instanceof CraftingMultiblockGuiClient cmGui) {
if (cmGui.isShapeValid) {
yield true;
case MULTIBLOCK -> screen.getMenu().components.findOrDefault(
client -> {
if (client instanceof CraftingMultiblockGuiClient cmGui) {
if (cmGui.isShapeValid) {
return Optional.of(true);
}
}
}
}
yield false;
}
return Optional.empty();
},
false);
};
}
}
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 @@ -27,6 +27,7 @@
import aztech.modern_industrialization.inventory.ConfigurableItemStack;
import aztech.modern_industrialization.inventory.MIInventory;
import aztech.modern_industrialization.inventory.SlotPositions;
import aztech.modern_industrialization.machines.ComponentStorage;
import aztech.modern_industrialization.machines.GuiComponentsClient;
import aztech.modern_industrialization.util.NbtHelper;
import java.util.ArrayList;
Expand All @@ -52,34 +53,29 @@ public static MachineMenuClient create(int syncId, Inventory playerInventory, Re
SlotPositions fluidPositions = SlotPositions.read(buf);
MIInventory inventory = new MIInventory(itemStacks, fluidStacks, itemPositions, fluidPositions);
// Components
List<GuiComponentClient> components = new ArrayList<>();
ComponentStorage<GuiComponentClient> components = new ComponentStorage<>();
int componentCount = buf.readInt();
for (int i = 0; i < componentCount; ++i) {
ResourceLocation id = buf.readResourceLocation();
components.add(GuiComponentsClient.get(id).createFromInitialData(buf));
components.register(GuiComponentsClient.get(id).createFromInitialData(buf));
}
// GUI params
MachineGuiParameters guiParams = MachineGuiParameters.read(buf);

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

public final List<GuiComponentClient> components;
public final ComponentStorage<GuiComponentClient> components;

private MachineMenuClient(int syncId, Inventory playerInventory, MIInventory inventory, List<GuiComponentClient> components,
private MachineMenuClient(int syncId, Inventory playerInventory, MIInventory inventory, ComponentStorage<GuiComponentClient> components,
MachineGuiParameters guiParams) {
super(syncId, playerInventory, inventory, guiParams, components);
this.components = components;
}

@Nullable
public <T extends GuiComponentClient> T getComponent(Class<T> klass) {
for (GuiComponentClient component : components) {
if (klass.isInstance(component)) {
return (T) component;
}
}
return null;
return components.get(klass).orElse(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public class MachineScreen extends MIHandledScreen<MachineMenuClient> implements
public MachineScreen(MachineMenuClient handler, Inventory inventory, Component title) {
super(handler, inventory, title);

for (GuiComponentClient component : handler.components) {
renderers.add(component.createRenderer(this));
}
handler.components.forEach(component -> renderers.add(component.createRenderer(this)));

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

private void renderConfigurableSlotBackgrounds(GuiGraphics guiGraphics) {
for (Slot slot : this.menu.slots) {
if (slot instanceof BackgroundRenderedSlot brs) {
if (slot.isActive() && slot instanceof BackgroundRenderedSlot brs) {
int px = leftPos + slot.x - 1;
int py = topPos + slot.y - 1;
guiGraphics.blit(SLOT_ATLAS, px, py, brs.getBackgroundU(), brs.getBackgroundV(), 18, 18);
if (slot.getItem().isEmpty()) {
var atlas = brs.getBackgroundAtlasLocation();
guiGraphics.blit(atlas == null ? SLOT_ATLAS : atlas, px, py, brs.getBackgroundU(), brs.getBackgroundV(), 18, 18);
} else {
guiGraphics.blit(SLOT_ATLAS, px, py, 0, 0, 18, 18);
}
}
}
}
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,70 @@
/*
* 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;
}

/**
* Select first non-null id, and convert it to a sprite id.
*/
default Material select(ResourceLocation... candidates) {
for (ResourceLocation id : candidates) {
if (id != null) {
return new Material(InventoryMenu.BLOCK_ATLAS, id);
}
}
return null;
}
}
Loading
Loading