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

View file

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

View file

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

View file

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