Start on json based machine tile + guis
This commit is contained in:
parent
92b3bc6651
commit
7defc53242
14 changed files with 377 additions and 129 deletions
|
@ -0,0 +1,138 @@
|
|||
package techreborn.blockentity.data;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.fabricmc.loader.launch.common.FabricLauncherBase;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.JsonHelper;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.apache.commons.lang3.text.WordUtils;
|
||||
import reborncore.client.containerBuilder.IContainerProvider;
|
||||
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
||||
import reborncore.client.containerBuilder.builder.ContainerBlockEntityInventoryBuilder;
|
||||
import reborncore.client.containerBuilder.builder.ContainerBuilder;
|
||||
import reborncore.common.crafting.RebornRecipeType;
|
||||
import reborncore.common.recipes.RecipeCrafter;
|
||||
import reborncore.common.util.RebornInventory;
|
||||
import reborncore.common.util.serialization.SerializationUtil;
|
||||
import techreborn.blockentity.GenericMachineBlockEntity;
|
||||
import techreborn.init.ModRecipes;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class DataDrivenBEProvider extends BlockEntityType<DataDrivenBEProvider.DataDrivenBlockEntity> implements Supplier<BlockEntity> {
|
||||
|
||||
private final Identifier identifier;
|
||||
private final Block block;
|
||||
private final int energy;
|
||||
private final int maxInput;
|
||||
|
||||
private final List<DataDrivenSlot> slots;
|
||||
|
||||
public static DataDrivenBEProvider create(Block block, Identifier identifier){
|
||||
String location = String.format("%s/machines/%s.json", identifier.getNamespace(), identifier.getPath());
|
||||
JsonObject jsonObject;
|
||||
try {
|
||||
jsonObject = SerializationUtil.GSON.fromJson(IOUtils.toString(FabricLauncherBase.getLauncher().getResourceAsStream(location), StandardCharsets.UTF_8), JsonObject.class);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("failed to read json: " + location, e);
|
||||
}
|
||||
Identifier id = new Identifier(JsonHelper.getString(jsonObject, "name"));
|
||||
DataDrivenBEProvider provider = new DataDrivenBEProvider(block, jsonObject);
|
||||
Registry.register(Registry.BLOCK_ENTITY, id, provider);
|
||||
return provider;
|
||||
}
|
||||
|
||||
private DataDrivenBEProvider(Block block, JsonObject jsonObject) {
|
||||
super(null, ImmutableSet.copyOf(Collections.singletonList(block)), null);
|
||||
this.block = block;
|
||||
this.identifier = new Identifier(JsonHelper.getString(jsonObject, "name"));
|
||||
this.energy = JsonHelper.getInt(jsonObject, "energy");
|
||||
this.maxInput = JsonHelper.getInt(jsonObject, "maxInput");
|
||||
this.slots = DataDrivenSlot.read(JsonHelper.getArray(jsonObject, "slots"));
|
||||
Validate.isTrue(getEnergySlot() > 0); //Ensure there is an energy slot, doing it here so it crashes on game load
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public DataDrivenBlockEntity instantiate() {
|
||||
return new DataDrivenBlockEntity(this);
|
||||
}
|
||||
|
||||
public BuiltContainer createContainer(DataDrivenBlockEntity blockEntity, int syncID, PlayerEntity player) {
|
||||
ContainerBlockEntityInventoryBuilder builder = new ContainerBuilder(identifier.getPath()).player(player.inventory)
|
||||
.inventory().hotbar().addInventory().blockEntity(blockEntity);
|
||||
|
||||
slots.forEach(dataDrivenSlot -> dataDrivenSlot.add(builder));
|
||||
|
||||
builder.syncEnergyValue().syncCrafterValue();
|
||||
|
||||
return builder.addInventory().create(blockEntity, syncID);
|
||||
}
|
||||
|
||||
//Used by the GenericMachineBlock
|
||||
@Override
|
||||
public BlockEntity get() {
|
||||
return instantiate();
|
||||
}
|
||||
|
||||
public static class DataDrivenBlockEntity extends GenericMachineBlockEntity implements IContainerProvider {
|
||||
|
||||
private final DataDrivenBEProvider provider;
|
||||
|
||||
private DataDrivenBlockEntity(DataDrivenBEProvider provider) {
|
||||
super(provider, provider.getSimpleName(), provider.maxInput, provider.energy, provider.block, provider.getEnergySlot());
|
||||
this.provider = provider;
|
||||
|
||||
RebornRecipeType recipeType = ModRecipes.byName(provider.identifier);
|
||||
Validate.notNull(recipeType);
|
||||
|
||||
this.inventory = new RebornInventory<>(provider.slots.size(), provider.getSimpleName() + "BlockEntity", 64, this);
|
||||
this.crafter = new RecipeCrafter(recipeType, this, provider.countOfSlotType(SlotType.INPUT), provider.countOfSlotType(SlotType.OUTPUT), this.inventory, provider.slotIds(SlotType.INPUT), provider.slotIds(SlotType.OUTPUT));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BuiltContainer createContainer(int syncID, PlayerEntity player) {
|
||||
return provider.createContainer(this,syncID, player);
|
||||
}
|
||||
|
||||
public DataDrivenBEProvider getProvider() {
|
||||
return provider;
|
||||
}
|
||||
}
|
||||
|
||||
private int getEnergySlot() {
|
||||
return slots.stream().filter(slot -> slot.getType() == SlotType.ENERGY).findFirst().orElse(null).getId();
|
||||
}
|
||||
|
||||
private int countOfSlotType(SlotType type){
|
||||
return (int) slots.stream()
|
||||
.filter(slot -> slot.getType() == type)
|
||||
.count();
|
||||
}
|
||||
|
||||
private int[] slotIds(SlotType type){
|
||||
return slots.stream()
|
||||
.filter(slot -> slot.getType() == type)
|
||||
.mapToInt(DataDrivenSlot::getId)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
public List<DataDrivenSlot> getSlots() {
|
||||
return Collections.unmodifiableList(slots);
|
||||
}
|
||||
|
||||
private String getSimpleName(){
|
||||
return WordUtils.capitalize(identifier.getPath());
|
||||
}
|
||||
}
|
36
src/main/java/techreborn/blockentity/data/DataDrivenGui.java
Normal file
36
src/main/java/techreborn/blockentity/data/DataDrivenGui.java
Normal file
|
@ -0,0 +1,36 @@
|
|||
package techreborn.blockentity.data;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
||||
import reborncore.client.gui.builder.GuiBase;
|
||||
import reborncore.client.gui.guibuilder.GuiBuilder;
|
||||
|
||||
public class DataDrivenGui extends GuiBase<BuiltContainer> {
|
||||
|
||||
private final DataDrivenBEProvider provider;
|
||||
private final DataDrivenBEProvider.DataDrivenBlockEntity blockEntity;
|
||||
|
||||
public DataDrivenGui(int syncID, final PlayerEntity player, final DataDrivenBEProvider.DataDrivenBlockEntity blockEntity) {
|
||||
super(player, blockEntity, blockEntity.createContainer(syncID, player));
|
||||
this.blockEntity = blockEntity;
|
||||
this.provider = blockEntity.getProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawBackground(final float f, final int mouseX, final int mouseY) {
|
||||
super.drawBackground(f, mouseX, mouseY);
|
||||
final GuiBase.Layer layer = GuiBase.Layer.BACKGROUND;
|
||||
final DataDrivenGui gui = this;
|
||||
|
||||
provider.getSlots().forEach(slot -> slot.draw(gui, layer));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawForeground(final int mouseX, final int mouseY) {
|
||||
super.drawForeground(mouseX, mouseY);
|
||||
final GuiBase.Layer layer = GuiBase.Layer.FOREGROUND;
|
||||
|
||||
builder.drawProgressBar(this, blockEntity.getProgressScaled(100), 100, 76, 48, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
|
||||
builder.drawMultiEnergyBar(this, 9, 19, (int) blockEntity.getEnergy(), (int) blockEntity.getMaxPower(), mouseX, mouseY, 0, layer);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package techreborn.blockentity.data;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.minecraft.util.JsonHelper;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import reborncore.client.containerBuilder.builder.ContainerBlockEntityInventoryBuilder;
|
||||
import reborncore.client.gui.builder.GuiBase;
|
||||
import reborncore.common.util.serialization.SerializationUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DataDrivenSlot {
|
||||
|
||||
public static List<DataDrivenSlot> read(JsonArray jsonArray){
|
||||
AtomicInteger idCount = new AtomicInteger();
|
||||
return SerializationUtil.stream(jsonArray)
|
||||
.map(JsonElement::getAsJsonObject)
|
||||
.map(json -> new DataDrivenSlot(idCount.getAndIncrement(), JsonHelper.getInt(json, "x"), JsonHelper.getInt(json, "y"), SlotType.fromString(JsonHelper.getString(json, "type"))))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private final int id;
|
||||
private final int x;
|
||||
private final int y;
|
||||
private final SlotType type;
|
||||
|
||||
public DataDrivenSlot(int id, int x, int y, SlotType type) {
|
||||
this.id = id;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.type = type;
|
||||
Validate.notNull(type);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public SlotType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void add(ContainerBlockEntityInventoryBuilder inventoryBuilder){
|
||||
type.getSlotBiConsumer().accept(inventoryBuilder, this);
|
||||
}
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public void draw(GuiBase guiBase, GuiBase.Layer layer){
|
||||
//TODO find a better way to do this
|
||||
if(getType() == SlotType.OUTPUT){
|
||||
guiBase.drawOutputSlot(getX(), getY(), layer);
|
||||
} else {
|
||||
guiBase.drawSlot(getX(), getY(), layer);
|
||||
}
|
||||
}
|
||||
}
|
36
src/main/java/techreborn/blockentity/data/SlotType.java
Normal file
36
src/main/java/techreborn/blockentity/data/SlotType.java
Normal file
|
@ -0,0 +1,36 @@
|
|||
package techreborn.blockentity.data;
|
||||
|
||||
import reborncore.client.containerBuilder.builder.ContainerBlockEntityInventoryBuilder;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public enum SlotType {
|
||||
//Im really not a fan of the way I add the slots to the builder here
|
||||
INPUT((builder, slot) -> {
|
||||
builder.slot(slot.getId(), slot.getX(), slot.getY());
|
||||
}),
|
||||
OUTPUT((builder, slot) -> {
|
||||
builder.outputSlot(slot.getId(), slot.getX(), slot.getY());
|
||||
}),
|
||||
ENERGY((builder, slot) -> {
|
||||
builder.energySlot(slot.getId(), slot.getX(), slot.getY());
|
||||
});
|
||||
|
||||
public static SlotType fromString(String string){
|
||||
return Arrays.stream(values())
|
||||
.filter(slotType -> slotType.name().equalsIgnoreCase(string))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private BiConsumer<ContainerBlockEntityInventoryBuilder, DataDrivenSlot> slotBiConsumer;
|
||||
|
||||
SlotType(BiConsumer<ContainerBlockEntityInventoryBuilder, DataDrivenSlot> slotBiConsumer) {
|
||||
this.slotBiConsumer = slotBiConsumer;
|
||||
}
|
||||
|
||||
public BiConsumer<ContainerBlockEntityInventoryBuilder, DataDrivenSlot> getSlotBiConsumer() {
|
||||
return slotBiConsumer;
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
* This file is part of TechReborn, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2018 TechReborn
|
||||
*
|
||||
* 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 techreborn.blockentity.machine.tier1;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import reborncore.client.containerBuilder.IContainerProvider;
|
||||
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
||||
import reborncore.client.containerBuilder.builder.ContainerBuilder;
|
||||
import reborncore.common.recipes.RecipeCrafter;
|
||||
import reborncore.common.util.RebornInventory;
|
||||
import techreborn.blockentity.GenericMachineBlockEntity;
|
||||
import techreborn.config.TechRebornConfig;
|
||||
import techreborn.init.ModRecipes;
|
||||
import techreborn.init.TRBlockEntities;
|
||||
import techreborn.init.TRContent;
|
||||
|
||||
public class GrinderBlockEntity extends GenericMachineBlockEntity implements IContainerProvider {
|
||||
|
||||
public GrinderBlockEntity() {
|
||||
super(TRBlockEntities.GRINDER, "Grinder", TechRebornConfig.grinderMaxInput, TechRebornConfig.grinderMaxEnergy, TRContent.Machine.GRINDER.block, 2);
|
||||
final int[] inputs = new int[] { 0 };
|
||||
final int[] outputs = new int[] { 1 };
|
||||
this.inventory = new RebornInventory<>(3, "GrinderBlockEntity", 64, this);
|
||||
this.crafter = new RecipeCrafter(ModRecipes.GRINDER, this, 2, 1, this.inventory, inputs, outputs);
|
||||
}
|
||||
|
||||
// IContainerProvider
|
||||
@Override
|
||||
public BuiltContainer createContainer(int syncID, final PlayerEntity player) {
|
||||
return new ContainerBuilder("grinder").player(player.inventory).inventory().hotbar().addInventory().blockEntity(this)
|
||||
.slot(0, 55, 45).outputSlot(1, 101, 45).energySlot(2, 8, 72).syncEnergyValue().syncCrafterValue()
|
||||
.addInventory().create(this, syncID);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue