feat: menu sync client server
Some checks are pending
Build / build (push) Waiting to run

This commit is contained in:
SushiCannibale 2025-08-03 21:11:37 +02:00
parent 1ac26e92f7
commit 8be2b08e64
4 changed files with 44 additions and 38 deletions

View file

@ -11,7 +11,6 @@ import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider; import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleMenuProvider; import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.BlockGetter;
@ -51,8 +50,8 @@ public class FloralWorkbench extends BaseEntityBlock
private static final Map<Direction.Axis, VoxelShape> SHAPES = private static final Map<Direction.Axis, VoxelShape> SHAPES =
Shapes.rotateHorizontalAxis(Shapes.or(LEGS, TABLE, SUPPORT)); Shapes.rotateHorizontalAxis(Shapes.or(LEGS, TABLE, SUPPORT));
public static final Component FLORAL_WB_TITLE = public static final Component FLORAL_WB_TITLE = Component.translatable(
Component.translatable("container.floral_workbench.title"); "container.charmsnfabrics.floral_workbench.title");
public FloralWorkbench(BlockBehaviour.Properties properties) public FloralWorkbench(BlockBehaviour.Properties properties)
{ {
@ -114,10 +113,6 @@ public class FloralWorkbench extends BaseEntityBlock
// award stat : "Flowers !" // award stat : "Flowers !"
} }
} }
else
{
player.displayClientMessage(Component.literal("pos:" + pos), false);
}
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
} }
@ -126,8 +121,7 @@ public class FloralWorkbench extends BaseEntityBlock
Level level, BlockPos pos) Level level, BlockPos pos)
{ {
return new SimpleMenuProvider( return new SimpleMenuProvider(
(id, inv, player) -> new FloralWorkbenchMenu(id, inv, (id, inv, player) -> new FloralWorkbenchMenu(id, inv),
ContainerLevelAccess.create(level, pos)),
FLORAL_WB_TITLE); FLORAL_WB_TITLE);
} }
} }

View file

@ -7,21 +7,17 @@ import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList; import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet; import net.minecraft.world.ContainerHelper;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity; import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable; import net.neoforged.neoforge.items.ItemStackHandler;
import java.util.List;
public class FloralWorkbenchBlockEntity extends BaseContainerBlockEntity public class FloralWorkbenchBlockEntity extends BaseContainerBlockEntity
{ {
ItemStack crown; private NonNullList<ItemStack> items = NonNullList.withSize(1, ItemStack.EMPTY);
public FloralWorkbenchBlockEntity(BlockPos pos, BlockState blockState) public FloralWorkbenchBlockEntity(BlockPos pos, BlockState blockState)
{ {
@ -29,29 +25,49 @@ public class FloralWorkbenchBlockEntity extends BaseContainerBlockEntity
blockState); blockState);
} }
@Override
protected void saveAdditional(CompoundTag nbt,
HolderLookup.Provider levelRegistry)
{
super.saveAdditional(nbt, levelRegistry);
ContainerHelper.saveAllItems(nbt, this.items, levelRegistry);
}
@Override
protected void loadAdditional(CompoundTag nbt,
HolderLookup.Provider levelRegistry)
{
super.loadAdditional(nbt, levelRegistry);
this.items = NonNullList.withSize(1, ItemStack.EMPTY);
ContainerHelper.loadAllItems(nbt, this.items, levelRegistry);
// this.crown = items.getFirst();
}
@Override @Override
protected Component getDefaultName() protected Component getDefaultName()
{ {
return Component.translatable("container.floral_workbench"); return Component.translatable(
"container.charmsnfabrics.floral_workbench");
} }
@Override @Override
protected NonNullList<ItemStack> getItems() protected NonNullList<ItemStack> getItems()
{ {
return NonNullList.of(this.crown); return this.items;
} }
@Override @Override
protected void setItems(NonNullList<ItemStack> items) protected void setItems(NonNullList<ItemStack> items)
{ {
this.crown = items.getFirst(); this.items = items;
} }
@Override @Override
protected AbstractContainerMenu createMenu(int containerId, protected AbstractContainerMenu createMenu(int containerId,
Inventory inventory) Inventory inventory)
{ {
return new FloralWorkbenchMenu(containerId, inventory); return new FloralWorkbenchMenu(containerId, inventory,
new ItemStackHandler(this.items));
} }
@Override @Override

View file

@ -1,6 +1,8 @@
package fr.sushi.charmsnfabrics.common.inventory; package fr.sushi.charmsnfabrics.common.inventory;
import fr.sushi.charmsnfabrics.common.CnFRegistries; import fr.sushi.charmsnfabrics.common.CnFRegistries;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AbstractContainerMenu;
@ -11,30 +13,23 @@ import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.ItemStackHandler; import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.items.SlotItemHandler; import net.neoforged.neoforge.items.SlotItemHandler;
import java.util.List;
public class FloralWorkbenchMenu extends AbstractContainerMenu public class FloralWorkbenchMenu extends AbstractContainerMenu
{ {
private final ContainerLevelAccess access; private final IItemHandler items;
private final IItemHandler crownContainer = new ItemStackHandler(1)
{
@Override
public boolean isItemValid(int slot, ItemStack stack)
{
return stack.is(CnFRegistries.Items.FLOWER_CROWN.asItem());
}
};;
public FloralWorkbenchMenu(int containerId, Inventory playerInv) public FloralWorkbenchMenu(int containerId, Inventory playerInv)
{ {
this(containerId, playerInv, ContainerLevelAccess.NULL); this(containerId, playerInv, new ItemStackHandler(1));
} }
public FloralWorkbenchMenu(int containerId, Inventory playerInv, public FloralWorkbenchMenu(int containerId, Inventory playerInv, IItemHandler itemHandler)
final ContainerLevelAccess access)
{ {
super(CnFRegistries.MenuTypes.FLORAL_WB_MENU.get(), containerId); super(CnFRegistries.MenuTypes.FLORAL_WB_MENU.get(), containerId);
this.access = access; this.items = itemHandler;
this.addSlot(new SlotItemHandler(this.crownContainer, 0, 80, 35)); this.addSlot(new SlotItemHandler(itemHandler, 0, 80, 35));
this.addStandardInventorySlots(playerInv, 8, 84); this.addStandardInventorySlots(playerInv, 8, 84);
} }
@ -64,13 +59,14 @@ public class FloralWorkbenchMenu extends AbstractContainerMenu
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
} }
slot.setChanged();
slot.onTake(player, stack);
return stack.copy(); return stack.copy();
} }
@Override @Override
public boolean stillValid(Player player) public boolean stillValid(Player player)
{ {
return stillValid(this.access, player, return true;
CnFRegistries.Blocks.FLORAL_WORKBENCH.get());
} }
} }

View file

@ -2,8 +2,8 @@
"itemGroup.charmsnfabrics": "Charms & Fabrics", "itemGroup.charmsnfabrics": "Charms & Fabrics",
"item.charmsnfabrics.flower_crown": "Flower Crown", "item.charmsnfabrics.flower_crown": "Flower Crown",
"item.charmsnfabrics.floral_workbench": "Floral Workbench", "item.charmsnfabrics.floral_workbench": "Floral Workbench",
"container.floral_workbench": "Floral Workbench", "container.charmsnfabrics.floral_workbench": "Floral Workbench",
"container.floral_workbench.title": "Floral Workbench", "container.charmsnfabrics.floral_workbench.title": "Floral Workbench",
"charmsnfabrics.configuration.title": "Charms & Fabrics Configs", "charmsnfabrics.configuration.title": "Charms & Fabrics Configs",
"charmsnfabrics.configuration.section.charmsnfabrics.common.toml": "Charms & Fabrics Configs", "charmsnfabrics.configuration.section.charmsnfabrics.common.toml": "Charms & Fabrics Configs",