Jump to content

Recommended Posts

Posted

Hello there!

I tried searching and looking around for some help or the answer to this, but it either doesn't seem to exist, or is too old and no longer applies, so that's why i am asking this now and here:
I am trying to implement a GUI menu popup when you tap on a block. How would i go about doing this?
I understand the general idea behind this (using the onBlockActivated function on a Block type, using the createMenu function on a TileEntity type, using a Container type, ect.) but am unsure how exactly I am suppose to do it, I cannot figure this out.

Here is the current code I have:

Spoiler

Block Code:


package ahndreklicyri.highgentech.common.blocks;

import ahndreklicyri.highgentech.common.tileentities.tingen.FurnGenTin_TE;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
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;

public class FurnGenTin extends RotBlock{
    public FurnGenTin(Properties properties) {
        super(properties);
    }

    // For some reason, hasTileEntity has TWO versions, and only this one works.
    // Don't use the one without the BlockState argument!
    @Override
    public boolean hasTileEntity(BlockState State) {
        return true;
    }

    // Tile Entity to be created when Block is placed
    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return new FurnGenTin_TE();
    }

    // What this Block does on a RIGHT click
    @Override
    public ActionResultType onBlockActivated(BlockState state,World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        if(!worldIn.isRemote){
            final TileEntity TE = worldIn.getTileEntity(pos);
            if(TE instanceof FurnGenTin_TE){
                // I'm guessing I would call
                // TE.createMenu() here?
                LOGGER.info("Tapped a Tin Furnace Generator!");
            }
        }
        return ActionResultType.SUCCESS;
    }

    // What this Block does when replaced (aka Broken)
    @Override
    public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
        if(state.getBlock() != newState.getBlock()){
            final TileEntity TE = worldIn.getTileEntity(pos);
            if(TE instanceof FurnGenTin_TE){
                LOGGER.info("Destroyed a Tin Furnace Generator!");
            }
            // Unsure if this is needed, but just to be safe...
            super.onReplaced(state, worldIn, pos, newState, isMoving);
        }
    }
}

Tile Entity Code:


package ahndreklicyri.highgentech.common.tileentities.tingen;

import ahndreklicyri.highgentech.common.DeferredRegistry;
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.util.text.ITextComponent;

public class FurnGenTin_TE extends TileEntity implements INamedContainerProvider,ITickableTileEntity {

    // Set up the super and such here
    public FurnGenTin_TE() {
        super(DeferredRegistry.FURN_GEN_TIN_TE.get());
    }

    // Required in order to Tick the block
    @Override
    public void tick() {
        if(!world.isRemote){
            // Code to run per tick goes here
        }
    }

    // Write the data when the chunk is unloaded
    // Unused for now, but here for future use
    @Override
    public CompoundNBT write(CompoundNBT compound) {
        super.write(compound);
        return compound;
    }

    // Read the data when the chunk is loaded
    // Unused for now, but here for future use
    @Override
    public void read(CompoundNBT compound) {
        super.read(compound);
    }

    // Mark the TileEntity as Dirty so the game correctly saves the data!
    // CALL THIS WHEN ANY DATA CHANGES IN THE TILE ENTITY.
    // Unused for now, but here for future use
    @Override
    public void markDirty() {
        super.markDirty();
    }
    
    // Pass here is what I am struggling with what I am suppose to do.
    @Override
    public ITextComponent getDisplayName() {
        // I'm guessing this is the name (internal?) of the Display?
        return null;
    }
    
    @Override
    public Container createMenu(int p_createMenu_1_, PlayerInventory playerInv, PlayerEntity PlayerEnt) {
        // I know I should return a container here but how exactly...?
        return null;
    }
}

Container Code:


package ahndreklicyri.highgentech.common.tileentities.tingen;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;

// Not quite sure what I am suppose to do in this container class.

public class FurnGenTin_CON extends Container {

    protected FurnGenTin_CON(ContainerType<?> type, int id) {
        super(type, id);
    }

    // Allows the player to interact with this Container
    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return true;
    }
}

 

Any help would be appreciated, and sorry if this is asked a ton and i just missed it or something!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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