alloy furnace GUI w.i.p

This commit is contained in:
Halbear
2026-07-28 00:59:23 +01:00
parent 4bc528f3d5
commit c3b176dbeb
8 changed files with 137 additions and 33 deletions
@@ -18,7 +18,7 @@ public class AlloyFurnaceGUIScreen extends AbstractContainerScreen<AlloyFurnaceG
private final int x, y, z;
private final Player entity;
private boolean menuStateUpdateActive = false;
private static final Identifier BACKGROUND = Identifier.parse("ageofthegods:textures/screens/example_gui.png");
private static final Identifier BACKGROUND = Identifier.parse("ageofthegods:textures/screens/alloy_furnace_gui.png");
public AlloyFurnaceGUIScreen(AlloyFurnaceGUI container, Inventory inventory, Component text) {
super(container, inventory, text, 176, 166);
@@ -1,5 +1,6 @@
package net.halbear.aotg.custom.blocks.AlloyFurnace;
import io.netty.buffer.Unpooled;
import net.halbear.aotg.registries.ModBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
@@ -7,11 +8,14 @@ import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.WorldlyContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
@@ -19,6 +23,7 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
@@ -28,16 +33,18 @@ import net.neoforged.neoforge.transfer.item.ItemResource;
import net.neoforged.neoforge.transfer.item.ItemStacksResourceHandler;
import org.jspecify.annotations.Nullable;
import java.util.stream.IntStream;
import static net.halbear.aotg.custom.blocks.AlloyFurnace.AlloyFurnaceMultiBlock.*;
import static net.halbear.aotg.custom.blocks.AlloyFurnace.AlloyFurnaceUpdateHandler.getInverseRotationForDirection;
import static net.halbear.aotg.registries.ModBlockEntities.ALLOY_FURNACE_BLOCK_ENTITY;
public class AlloyFurnaceBlockEntity extends BlockEntity implements MenuProvider {
public class AlloyFurnaceBlockEntity extends RandomizableContainerBlockEntity implements MenuProvider, WorldlyContainer {
private boolean isFormed = false;
private float Fuel = 0.0f;
private int progress = 0;
private final NonNullList<ItemStack> items = NonNullList.withSize(2, ItemStack.EMPTY);
private NonNullList<ItemStack> items = NonNullList.withSize(5, ItemStack.EMPTY);
public AlloyFurnaceBlockEntity(BlockPos worldPosition, BlockState blockState) {
super(ALLOY_FURNACE_BLOCK_ENTITY.get(), worldPosition, blockState);
@@ -58,18 +65,22 @@ public class AlloyFurnaceBlockEntity extends BlockEntity implements MenuProvider
Complete = false;
}
}
if(Complete){
for (BlockPos offset : MULTI_BLOCK_POSITIONS) {
int Part = AlloyFurnaceUpdateHandler.GetModelPart(offset);
BlockPos Rotated = offset.rotate(rotation);
BlockPos targetPos = masterPos.offset(Rotated);
BlockState state = level.getBlockState(targetPos);
if(state.getValue(PARENT_BLOCK) && !(offset.getX() == 0 && offset.getY() == 0 && offset.getZ() == 0)) state = state.setValue(AlloyFurnaceMultiBlock.PARENT_BLOCK,false);
if(state.getValue(MODEL_PART) != Part) state = state.setValue(AlloyFurnaceMultiBlock.MODEL_PART,Part);
if(state.getValue(FACING) != direction) state = state.setValue(FACING,direction);
level.setBlockAndUpdate(targetPos,state);
if (state.getBlock().getName().getString().equals(ModBlocks.ALLOY_FURNACE.get().getName().getString())) {
if (Complete) {
int Part = AlloyFurnaceUpdateHandler.GetModelPart(offset);
if (!state.getValue(COMPLETE_STRUCTURE)) state = state.setValue(COMPLETE_STRUCTURE, true);
if (state.getValue(PARENT_BLOCK) && !(offset.getX() == 0 && offset.getY() == 0 && offset.getZ() == 0)) state = state.setValue(AlloyFurnaceMultiBlock.PARENT_BLOCK, false);
if (state.getValue(MODEL_PART) != Part) state = state.setValue(AlloyFurnaceMultiBlock.MODEL_PART, Part);
if (state.getValue(FACING) != direction) state = state.setValue(FACING, direction);
} else {
state = state.setValue(COMPLETE_STRUCTURE, false);
}
level.setBlockAndUpdate(targetPos, state);
}
}
return Complete;
}
@@ -91,18 +102,24 @@ public class AlloyFurnaceBlockEntity extends BlockEntity implements MenuProvider
@Override
public void loadAdditional(ValueInput input) {
super.loadAdditional(input);
this.Fuel = input.getFloatOr("fuel", 0);
if (!this.tryLoadLootTable(input))
this.items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
ContainerHelper.loadAllItems(input, this.items);
}
@Override
public void saveAdditional(ValueOutput output) {
super.saveAdditional(output);
output.putFloat("fuel", this.Fuel);
if (!this.trySaveLootTable(output))
ContainerHelper.saveAllItems(output, this.items);
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return this.saveWithoutMetadata(registries);
return this.saveWithFullMetadata(registries);
}
@Override
@@ -127,10 +144,21 @@ public class AlloyFurnaceBlockEntity extends BlockEntity implements MenuProvider
}
@Override
public @Nullable AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) {
protected Component getDefaultName() {
return null;
}
@Override
public @Nullable AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) {
return new AlloyFurnaceGUI(i, inventory, new FriendlyByteBuf(Unpooled.buffer()).writeBlockPos(this.worldPosition));
}
@Override
protected AbstractContainerMenu createMenu(int i, Inventory inventory) {
return new AlloyFurnaceGUI(i, inventory, new FriendlyByteBuf(Unpooled.buffer()).writeBlockPos(this.worldPosition));
}
private final StacksResourceHandler<ItemStack, ItemResource> resourceHandler = new ItemStacksResourceHandler(this.items) {
@Override
protected void onContentsChanged(int index, ItemStack stack) {
@@ -142,4 +170,53 @@ public class AlloyFurnaceBlockEntity extends BlockEntity implements MenuProvider
public StacksResourceHandler<ItemStack, ItemResource> getResourceHandler() {
return this.resourceHandler;
}
@Override
public int getContainerSize() {
return items.size();
}
@Override
public boolean isEmpty() {
for (ItemStack itemstack : this.items)
if (!itemstack.isEmpty())
return false;
return true;
}
@Override
protected NonNullList<ItemStack> getItems() {
return this.items;
}
@Override
protected void setItems(NonNullList<ItemStack> stacks) {
this.items = stacks;
}
@Override
public boolean canPlaceItem(int index, ItemStack stack) {
return true;
}
@Override
public int[] getSlotsForFace(Direction side) {
return IntStream.range(0, this.getContainerSize()).toArray();
}
@Override
public boolean canPlaceItemThroughFace(int index, ItemStack itemstack, @Nullable Direction direction) {
return this.canPlaceItem(index, itemstack);
}
@Override
public boolean canTakeItemThroughFace(int index, ItemStack itemstack, Direction direction) {
return true;
}
@Override
public void clearContent() {
items.clear();
}
}
@@ -14,6 +14,7 @@ import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.neoforged.neoforge.capabilities.Capabilities;
@@ -33,7 +34,7 @@ public class AlloyFurnaceGUI extends AbstractContainerMenu implements ModMenus.M
public final Map<String, Object> menuState = new HashMap<>() {
@Override
public Object put(String key, Object value) {
if (!this.containsKey(key) && this.size() >= 4)
if (!this.containsKey(key) && this.size() >= 5)
return null;
return super.put(key, value);
}
@@ -72,25 +73,36 @@ public class AlloyFurnaceGUI extends AbstractContainerMenu implements ModMenus.M
this.bound = true;
}
}
this.customSlots.put(0, this.addSlot(new ResourceHandlerSlot(internal, this::setItemInSlot, 0, 42, 41) {
this.customSlots.put(0, this.addSlot(new ResourceHandlerSlot(internal, this::setItemInSlot, 0, 37, 16) {
private final int slot = 0;
private int x = this.x;
private int y = this.y;
}));
this.customSlots.put(1, this.addSlot(new ResourceHandlerSlot(internal, this::setItemInSlot, 1, 21, 40) {
this.customSlots.put(1, this.addSlot(new ResourceHandlerSlot(internal, this::setItemInSlot, 1, 57, 16) {
private final int slot = 1;
private int x = this.x;
private int y = this.y;
}));
this.customSlots.put(2, this.addSlot(new ResourceHandlerSlot(internal, this::setItemInSlot, 2, 63, 41) {
this.customSlots.put(2, this.addSlot(new ResourceHandlerSlot(internal, this::setItemInSlot, 2, 47, 35) {
private final int slot = 2;
private int x = this.x;
private int y = this.y;
@Override
public boolean mayPlace(ItemStack stack) {
return Blocks.DECORATED_POT.asItem() == stack.getItem();
}
}));
this.customSlots.put(3, this.addSlot(new ResourceHandlerSlot(internal, this::setItemInSlot, 3, 129, 41) {
this.customSlots.put(3, this.addSlot(new ResourceHandlerSlot(internal, this::setItemInSlot, 3, 64, 58) {
private final int slot = 3;
private int x = this.x;
private int y = this.y;
}));
this.customSlots.put(4, this.addSlot(new ResourceHandlerSlot(internal, this::setItemInSlot, 4, 130, 37) {
private final int slot = 4;
private int x = this.x;
private int y = this.y;
@Override
public boolean mayPlace(ItemStack stack) {
@@ -48,10 +48,11 @@ public class AlloyFurnaceMultiBlock extends Block implements EntityBlock {
public static final EnumProperty<Direction> FACING = BlockStateProperties.HORIZONTAL_FACING;
public static final IntegerProperty MODEL_PART = IntegerProperty.create("modelpart",0,8);
public static final BooleanProperty PARENT_BLOCK = BooleanProperty.create("parent");
public static final BooleanProperty COMPLETE_STRUCTURE = BooleanProperty.create("complete_structure");
public AlloyFurnaceMultiBlock(Properties properties) {
super(properties);
this.registerDefaultState(this.defaultBlockState().setValue(MODEL_PART,0).setValue(PARENT_BLOCK,true).setValue(FACING,Direction.NORTH));
this.registerDefaultState(this.defaultBlockState().setValue(MODEL_PART,0).setValue(COMPLETE_STRUCTURE,false).setValue(PARENT_BLOCK,true).setValue(FACING,Direction.NORTH));
}
@Override
@@ -153,12 +154,25 @@ public class AlloyFurnaceMultiBlock extends Block implements EntityBlock {
builder.add(MODEL_PART);
builder.add(FACING);
builder.add(PARENT_BLOCK);
builder.add(COMPLETE_STRUCTURE);
}
@Override
public InteractionResult useWithoutItem(BlockState blockstate, Level world, BlockPos pos, Player entity, BlockHitResult hit) {
super.useWithoutItem(blockstate, world, pos, entity, hit);
if (entity instanceof ServerPlayer player) {
AgeoftheGods.LOGGER.debug("BLOCKSTATE: " + blockstate.getBlock().getName().getString() + " | BLOCK: " + ModBlocks.ALLOY_FURNACE.get().getName().getString());
if(!blockstate.getBlock().getName().getString().equals(ModBlocks.ALLOY_FURNACE.get().getName().getString()) ||
blockstate.getBlock().getName().getString().equals(ModBlocks.ALLOY_FURNACE.get().getName().getString()) && !blockstate.getValue(COMPLETE_STRUCTURE)) return InteractionResult.PASS;
int ModelPart = blockstate.getValue(MODEL_PART);
BlockPos offset = AlloyFurnaceUpdateHandler.parts[ModelPart];
Direction direction = blockstate.getValue(FACING);
Rotation rotation = getInverseRotationForDirection(direction);
BlockPos Rotated = offset.rotate(rotation);
BlockPos TargetPos = new BlockPos(pos.getX() - Rotated.getX(), pos.getY() - Rotated.getY(), pos.getZ() - Rotated.getZ());
AgeoftheGods.LOGGER.debug("TARGET GUI POS: " + TargetPos +"\n INTERACTION POS:" + pos + "\n ROTATION" + Rotated + "\n OFFSET" + offset);
if(world.getBlockState(TargetPos).getBlock().getName().getString().equals(ModBlocks.ALLOY_FURNACE.get().getName().getString()) &&
world.getBlockState(TargetPos).getValue(PARENT_BLOCK)) {
player.openMenu(new MenuProvider() {
@Override
public Component getDisplayName() {
@@ -167,10 +181,11 @@ public class AlloyFurnaceMultiBlock extends Block implements EntityBlock {
@Override
public AbstractContainerMenu createMenu(int id, Inventory inventory, Player player) {
return new AlloyFurnaceGUI(id, inventory, new FriendlyByteBuf(Unpooled.buffer()).writeBlockPos(pos));
return new AlloyFurnaceGUI(id, inventory, new FriendlyByteBuf(Unpooled.buffer()).writeBlockPos(TargetPos));
}
}, pos);
}
}
return InteractionResult.SUCCESS;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 809 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB