Skip to content

Commit

Permalink
implement backpack menu helper
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Jan 13, 2024
1 parent 3527920 commit 4026982
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package dev.cammiescorner.camsbackpacks.neoforge.services;

import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity;
import dev.cammiescorner.camsbackpacks.menu.BackpackMenu;
import dev.cammiescorner.camsbackpacks.util.platform.service.MenuHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.network.NetworkHooks;

public class NFMenuHelper implements MenuHelper {
Expand All @@ -17,4 +22,19 @@ public MenuProvider getMenuProvider(BackpackBlockEntity backpack) {
public void openMenu(ServerPlayer player, MenuProvider menu, BackpackBlockEntity backpack) {
NetworkHooks.openScreen(player, menu, buf -> backpack.writeInitMenuData(player, buf));
}

@Override
public void openMenu(ServerPlayer serverPlayer, ItemStack backpackStack, Container inventory) {
BlockPos pos = serverPlayer.blockPosition();

NetworkHooks.openScreen(serverPlayer, new SimpleMenuProvider(
(syncId, playerInventory, player) -> new BackpackMenu(syncId, playerInventory, inventory, ContainerLevelAccess.create(player.level(), pos), pos, false),
backpackStack.getHoverName()
),
buf -> {
buf.writeBlockPos(pos);
buf.writeBoolean(false);
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.cammiescorner.camsbackpacks.menu.BackpackMenu;
import dev.cammiescorner.camsbackpacks.util.platform.service.MenuHelper;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -45,10 +46,11 @@ public void openMenu(ServerPlayer player, MenuProvider menu, BackpackBlockEntity

@Override
public void openMenu(ServerPlayer player, ItemStack backpackStack, Container inventory) {
BlockPos pos = player.blockPosition();
player.openMenu(new ExtendedScreenHandlerFactory() {
@Override
public void writeScreenOpeningData(ServerPlayer player, FriendlyByteBuf buf) {
buf.writeBlockPos(player.blockPosition());
buf.writeBlockPos(pos);
buf.writeBoolean(false);
}

Expand All @@ -59,7 +61,7 @@ public Component getDisplayName() {

@Override
public AbstractContainerMenu createMenu(int syncId, Inventory playerInventory, Player player) {
return new BackpackMenu(syncId, playerInventory, inventory, ContainerLevelAccess.create(player.level(), player.blockPosition()), player.blockPosition(), false);
return new BackpackMenu(syncId, playerInventory, inventory, ContainerLevelAccess.create(player.level(), pos), pos, false);
}
});
}
Expand Down

0 comments on commit 4026982

Please sign in to comment.