Jump to content

Tile entity container couldn't open


Jar36

Recommended Posts

I added a tile entity with a container GUI in my mod

But when i clicked the block ,it printed a lot of stacktrace but not make crash

this is the stacktrace

[21:45:45] [Server thread/FATAL] [minecraft/ThreadTaskExecutor]: Error executing task on Server
java.lang.UnsupportedOperationException: Unable to construct this menu by type
	at net.minecraft.inventory.container.Container.getType(Container.java:56) ~[forge:?] {re:classloading}
	at net.minecraftforge.fml.network.NetworkHooks.openGui(NetworkHooks.java:227) ~[forge:?] {re:classloading}
	at net.minecraftforge.fml.network.NetworkHooks.openGui(NetworkHooks.java:192) ~[forge:?] {re:classloading}
	at com.lucaiyu.touhoucraft.blocks.DonateBox.onBlockActivated(DonateBox.java:46) ~[?:?] {re:classloading}
	at net.minecraft.block.AbstractBlock$AbstractBlockState.onBlockActivated(AbstractBlock.java:732) ~[forge:?] {re:classloading}
	at net.minecraft.server.management.PlayerInteractionManager.func_219441_a(PlayerInteractionManager.java:347) ~[forge:?] {re:classloading}
	at net.minecraft.network.play.ServerPlayNetHandler.processTryUseItemOnBlock(ServerPlayNetHandler.java:986) ~[forge:?] {re:classloading}
	at net.minecraft.network.play.client.CPlayerTryUseItemOnBlockPacket.processPacket(CPlayerTryUseItemOnBlockPacket.java:45) ~[forge:?] {re:classloading}
	at net.minecraft.network.play.client.CPlayerTryUseItemOnBlockPacket.processPacket(CPlayerTryUseItemOnBlockPacket.java:12) ~[forge:?] {re:classloading}
	at net.minecraft.network.PacketThreadUtil.lambda$checkThreadAndEnqueue$0(PacketThreadUtil.java:19) ~[forge:?] {re:classloading}
	at net.minecraft.util.concurrent.TickDelayedTask.run(TickDelayedTask.java:20) ~[forge:?] {re:classloading}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.run(ThreadTaskExecutor.java:139) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.RecursiveEventLoop.run(RecursiveEventLoop.java:22) ~[forge:?] {re:classloading}
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:759) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:159) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.driveOne(ThreadTaskExecutor.java:109) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.driveOneInternal(MinecraftServer.java:742) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.driveOne(MinecraftServer.java:736) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.drainTasks(ThreadTaskExecutor.java:97) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.runScheduledTasks(MinecraftServer.java:721) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.func_240802_v_(MinecraftServer.java:668) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.lambda$startServer$0(MinecraftServer.java:233) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_312] {}

This is the block

package com.lucaiyu.touhoucraft.blocks;

import com.lucaiyu.touhoucraft.blocks.tileentities.DonateBoxGuiTileEntity;
import com.lucaiyu.touhoucraft.blocks.tileentities.TileEntityRegistry;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nullable;

public class DonateBox extends Block {
    public DonateBox() {
        super(AbstractBlock.Properties.create(Material.WOOD).sound(SoundType.WOOD).hardnessAndResistance(1.0F, 1.0F).harvestTool(ToolType.AXE));
    }
    @Override
    public boolean hasTileEntity(BlockState state){
        return true;
    }


    @Nullable
    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return TileEntityRegistry.donate_box_gui_tile_entity.get().create();
    }

    @Override
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        if (!worldIn.isRemote){
            TileEntity tileEntity = worldIn.getTileEntity(pos);
            if (tileEntity instanceof DonateBoxGuiTileEntity){
                NetworkHooks.openGui((ServerPlayerEntity) player,(DonateBoxGuiTileEntity)tileEntity, pos);
            }
        }
        return ActionResultType.SUCCESS;
    }
}

This is the tile entity

package com.lucaiyu.touhoucraft.blocks.tileentities;

import com.lucaiyu.touhoucraft.TouHouCraft;
import com.lucaiyu.touhoucraft.containers.DonateBoxContainer;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

public class DonateBoxGuiTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {
    private static final int SLOTS = 1;
    private static int power = 0;

    @Override
    public void read(BlockState state, CompoundNBT nbt) {
        power = nbt.getInt("power");
        super.read(state, nbt);
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        compound.putInt("power", power);
        return super.write(compound);
    }

    public DonateBoxGuiTileEntity(TileEntityType<?> tileEntityTypeIn) {
        super(tileEntityTypeIn);
    }
    public DonateBoxGuiTileEntity(){
        this(TileEntityRegistry.donate_box_gui_tile_entity.get());
    }

    @Override
    public void tick() {
        // todo
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("container." + TouHouCraft.MOD_ID + ".donate_box");
    }

    @Nullable
    @Override
    public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity playerEntity) {
        return new DonateBoxContainer(id, playerInventory, this);
    }
}

This is the container

package com.lucaiyu.touhoucraft.containers;

import com.lucaiyu.touhoucraft.blocks.BlockRegistry;
import com.lucaiyu.touhoucraft.blocks.tileentities.DonateBoxGuiTileEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

import java.util.Objects;

public class DonateBoxContainer extends Container {
    public final DonateBoxGuiTileEntity tileentity;
    private final IWorldPosCallable ableToInteract;
    private final LazyOptional<IItemHandler> UP;
    public DonateBoxContainer(final int id, final PlayerInventory playerInventory, final DonateBoxGuiTileEntity entity){
        super(null, id);
        this.tileentity = entity;
        this.ableToInteract = IWorldPosCallable.of(tileentity.getWorld(), tileentity.getPos());
        this.UP = tileentity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.UP);
        this.UP.ifPresent((s)->{
            this.addSlot(new SlotItemHandler(s, 0, 80, 32));
        });
        for (int i = 0;i < 8; i++) {
            this.addSlot(new Slot(playerInventory,i,i*18+8,152));
            this.addSlot(new Slot(playerInventory,i+9,i*18+8,94));
            this.addSlot(new Slot(playerInventory,i+18,i*18+8,112));
            this.addSlot(new Slot(playerInventory,i+27,i*18+8,130));
        }
    }

    public DonateBoxContainer(final int id, final PlayerInventory playerInventory, final PacketBuffer data) {
        this(id, playerInventory,getTileEntity(playerInventory, data));

    }
    public static DonateBoxGuiTileEntity getTileEntity(PlayerInventory playerInventory, PacketBuffer data){
        Objects.requireNonNull(playerInventory, "player inventory couldn't be null!");
        Objects.requireNonNull(data, "Packet buffer counldn't be null!");
        BlockPos blockPos = data.readBlockPos();
        TileEntity tileEntity = playerInventory.player.world.getTileEntity(blockPos);

        if(tileEntity instanceof DonateBoxGuiTileEntity){
            return ((DonateBoxGuiTileEntity) tileEntity);
        }
        throw new IllegalStateException("Invalid tile entity!");
    }

    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return isWithinUsableDistance(ableToInteract, playerIn, BlockRegistry.donate_box.get());
    }

    @Override
    public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
        return ItemStack.EMPTY;
    }
}

And this is the GUI

package com.lucaiyu.touhoucraft.client.gui;

import com.lucaiyu.touhoucraft.TouHouCraft;
import com.lucaiyu.touhoucraft.containers.DonateBoxContainer;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class DonateBoxGui extends ContainerScreen<DonateBoxContainer> {
    private static final ResourceLocation DONATE_BOX_GUI = new ResourceLocation(TouHouCraft.MOD_ID,"textures/gui/donate_box_gui.png");

    public DonateBoxGui(DonateBoxContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
        super(screenContainer, inv, titleIn);
        this.guiLeft = 0;
        this.guiTop = 0;
        this.xSize = 176;
        this.ySize = 176;
    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        this.drawGuiContainerBackgroundLayer(matrixStack,partialTicks,mouseX,mouseY);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        this.renderHoveredTooltip(matrixStack, mouseX, mouseY);
    }

    @Override
    protected void drawGuiContainerForegroundLayer(MatrixStack matrixStack, int x, int y) {
        this.font.func_243248_b(matrixStack, this.playerInventory.getDisplayName(), (float) this.playerInventoryTitleX, (float) this.playerInventoryTitleY, 4210752);
    }


    @Override
    protected void drawGuiContainerBackgroundLayer(MatrixStack matrixStack, float partialTicks, int mouseX, int mouseY) {
        RenderSystem.color4f(1.0F,1.0F,1.0F,1.0F);
        this.minecraft.textureManager.bindTexture(DONATE_BOX_GUI);
        int x = (this.width-this.xSize)/2;
        int y = (this.height - this.ySize)/2;
        this.blit(matrixStack, x, y, 0, 0, this.xSize, this.ySize);

    }
}

The registry had no problem

Please help me

This is the repo of my project

https://www.github.com/lucaiyu/TouhouCraft

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.