mailbox menu showing

This commit is contained in:
DaniTheSkunk 2022-10-31 23:47:51 +00:00
parent 4b2ca83d2b
commit 5330dd2b4e
8 changed files with 158 additions and 2 deletions

View File

@ -33,7 +33,7 @@ public class BlockEntityMailbox extends BaseContainerBlockEntity {
@Override
protected AbstractContainerMenu createMenu(int syncId,
Inventory playerInventory) {
return null;
return new MenuMailbox(syncId, playerInventory, this);
}
@Override

View File

@ -1,12 +1,19 @@
package com.danitheskunk.skunkstuff.Blocks;
import net.minecraft.core.BlockPos;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.HopperBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
@ -71,4 +78,17 @@ public class BlockMailbox extends BaseEntityBlock {
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (world.isClientSide) {
return InteractionResult.SUCCESS;
} else {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof BlockEntityMailbox) {
player.openMenu((BlockEntityMailbox)blockEntity);
}
return InteractionResult.CONSUME;
}
}
}

View File

@ -0,0 +1,77 @@
package com.danitheskunk.skunkstuff.Blocks;
import net.minecraft.client.gui.screens.inventory.HopperScreen;
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.*;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
public class MenuMailbox extends AbstractContainerMenu {
private static final int CONTAINER_SIZE = 9;
private final Container mailbox;
public MenuMailbox(int syncId, Inventory playerInventory) {
this(syncId, playerInventory, new SimpleContainer(CONTAINER_SIZE));
}
public MenuMailbox(int syncId, Inventory playerInventory, Container inventory) {
super(Menus.MAILBOX, syncId);
this.mailbox = inventory;
checkContainerSize(inventory, CONTAINER_SIZE);
inventory.startOpen(playerInventory.player);
int i;
for(i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
this.addSlot(new Slot(inventory, i + j * 3, 62 + i * 18,
18 + j * 18));
}
}
for(i = 0; i < 3; ++i) {
for(int j = 0; j < 9; ++j) {
this.addSlot(new Slot(playerInventory, j + i * 9 + 9,
8 + j * 18, i * 18 + 84));
}
}
for(i = 0; i < 9; ++i) {
this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142));
}
}
@Override
public ItemStack quickMoveStack(Player player, int fromIndex) {
ItemStack itemStack = ItemStack.EMPTY;
Slot slot = (Slot)this.slots.get(fromIndex);
if (slot != null && slot.hasItem()) {
ItemStack itemStack2 = slot.getItem();
itemStack = itemStack2.copy();
if (fromIndex < this.mailbox.getContainerSize()) {
if (!this.moveItemStackTo(itemStack2,
this.mailbox.getContainerSize(), this.slots.size(), true)) {
return ItemStack.EMPTY;
}
} else if (!this.moveItemStackTo(itemStack2, 0,
this.mailbox.getContainerSize(), false)) {
return ItemStack.EMPTY;
}
if (itemStack2.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return itemStack;
}
@Override
public boolean stillValid(Player player) {
return mailbox.stillValid(player);
}
}

View File

@ -0,0 +1,20 @@
package com.danitheskunk.skunkstuff.Blocks;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
public class Menus {
public static final MenuType<MenuMailbox> MAILBOX = register("mailbox",
MenuMailbox::new);
private static <T extends AbstractContainerMenu> MenuType<T> register(String id, MenuType.MenuSupplier<T> factory) {
return Registry.register(Registry.MENU,
new ResourceLocation(Blocks.namespace, id),
new MenuType(factory));
}
public static void init() {
}
}

View File

@ -0,0 +1,21 @@
package com.danitheskunk.skunkstuff.Blocks;
import com.mojang.blaze3d.vertex.PoseStack;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
@Environment(EnvType.CLIENT)
public class ScreenMailbox extends AbstractContainerScreen<MenuMailbox> {
public ScreenMailbox(MenuMailbox handler, Inventory inventory, Component title) {
super(handler, inventory, title);
}
@Override
protected void renderBg(PoseStack matrices, float delta, int mouseX,
int mouseY) {
}
}

View File

@ -0,0 +1,15 @@
package com.danitheskunk.skunkstuff;
import com.danitheskunk.skunkstuff.Blocks.Menus;
import com.danitheskunk.skunkstuff.Blocks.ScreenMailbox;
import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry;
import net.minecraft.client.gui.screens.MenuScreens;
import org.quiltmc.loader.api.ModContainer;
import org.quiltmc.qsl.base.api.entrypoint.client.ClientModInitializer;
public class Client implements ClientModInitializer {
@Override
public void onInitializeClient(ModContainer mod) {
MenuScreens.register(Menus.MAILBOX, ScreenMailbox::new);
}
}

View File

@ -3,6 +3,7 @@ package com.danitheskunk.skunkstuff;
import com.danitheskunk.skunkstuff.Blocks.BlockGoban;
import com.danitheskunk.skunkstuff.Blocks.BlockMailbox;
import com.danitheskunk.skunkstuff.Blocks.Blocks;
import com.danitheskunk.skunkstuff.Blocks.Menus;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.DispenserBlock;
import org.quiltmc.loader.api.ModContainer;
@ -36,6 +37,7 @@ public class SkunkStuff implements ModInitializer {
@Override
public void onInitialize(ModContainer mod) {
Menus.init();
Blocks.registerAll();
}
}

View File

@ -19,7 +19,8 @@
},
"intermediate_mappings": "net.fabricmc:intermediary",
"entrypoints": {
"init": "com.danitheskunk.skunkstuff.SkunkStuff"
"init": "com.danitheskunk.skunkstuff.SkunkStuff",
"client_init": "com.danitheskunk.skunkstuff.Client"
},
"depends": [
{