Jump to content

GUI with buttons


Yagoki

Recommended Posts

This is my first time trying to make a gui, and i'm not quite sure how to go about it. I want to add a gui to my proximity detector so that the range and functions can be changed. I've already set up the fields i feel will be necessary in the TileEntity, which is currently working as expected.

I tried looking through the tutorial on the wiki which didn't really help with what i want to do (as far as i could tell) and didn't explain it in a way which made it so i could understand everything which was going on.

 

I need buttons so that i can change the entity types it will detect, and also the range on the detector. If somebody could walk me through the basics of this that would be great :D

Link to comment
Share on other sites

If you want to make a GUI and a tile entity without an inventory, you'll be needing the following classes.

 

A block

A GUI handler

A GUI class for the block

A tile entity class

A packet handler

 

Here are those 4 classes from one of my mods (the block is called NumpadBlock):

 

NumpadBlock class:

 

package vivadaylight3.interlock.blocks;

import java.io.IOException;
import java.util.Random;

import vivadaylight3.interlock.Interlock;
import vivadaylight3.interlock.tileentities.numpadblock.GuiNumpadBlock;
import vivadaylight3.interlock.tileentities.numpadblock.TileEntityNumpadBlock;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityCommandBlock;
import net.minecraft.world.World;

public class NumpadBlock extends BlockContainer
{
private String code;

    public NumpadBlock(int par1)
    {
        super(par1, Material.iron);
        setCreativeTab(CreativeTabs.tabRedstone);
    }
    
    public void registerIcons(IconRegister iconRegister){
    	
        blockIcon = iconRegister.registerIcon("Interlock:numpadBlock");
        
    }

    /**
     * Returns a new instance of a block's tile entity class. Called on placing the block.
     */
    public TileEntity createNewTileEntity(World par1World)
    {
        return new TileEntityNumpadBlock();
    }

    /**
     * Called upon block activation (right click on the block.)
     */
    
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    {
    	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

        if (tileEntity == null || par5EntityPlayer.isSneaking()){
        	
        	par5EntityPlayer.addChatMessage("You are sneaking");
        	
        	return false;
        	        	
        }

        par5EntityPlayer.openGui(Interlock.instance, 2, world, x, y, z);
        
        return true;
        
    }

    /**
     * If this returns true, then comparators facing away from this block will use the value from
     * getComparatorInputOverride instead of the actual redstone signal strength.
     */
    public boolean hasComparatorInputOverride()
    {
        return false;
    }

    /**
     * If hasComparatorInputOverride returns true, the return value from this is used instead of the redstone signal
     * strength when this block inputs to a comparator.
     */
    public int getComparatorInputOverride(World par1World, int par2, int par3, int par4, int par5)
    {
        return 0;
    }

    /**
     * Called when the block is placed in the world.
     */
    public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving, ItemStack par6ItemStack)
    {

    }
}

 

 

GuiNumpad

 

 

package vivadaylight3.interlock.tileentities.numpadblock;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.util.StringTranslate;
import net.minecraft.world.World;

import org.lwjgl.input.Keyboard;

import vivadaylight3.interlock.handlers.Config;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class GuiNumpadBlock extends GuiScreen
{

    /** Combination and set/clear buttons. */
    private final TileEntityNumpadBlock numpadBlock;
    private GuiButton button1;
    private GuiButton button2;
    private GuiButton button3;
    private GuiButton button4;
    private GuiButton button5;
    private GuiButton button6;
    private GuiButton button7;
    private GuiButton button8;
    private GuiButton button9;
    private GuiButton button0;
    private GuiButton buttonClear;
    private GuiButton buttonSet;
   // private EntityPlayer player;
    
    private String code = "";
    private int codeSize = 0;
    
    private World world;
    private int x;
    private int y;
    private int z;

    public GuiNumpadBlock(TileEntityNumpadBlock par1, World par2World, int par3x, int par4y, int par5z)
    {
        this.numpadBlock = par1;
        this.world = par2World;
        this.x = par3x;
        this.y = par4y;
        this.z = par5z;
        //this.player = par2Player;
      //  this.player.addChatMessage("GuiNUmpad: constructor");
    }

    /**
     * Called from the main game loop to update the screen.
     */
    public void updateScreen()
    {
        //this.enteredCodeField.updateCursorCounter();
       // this.player.addChatMessage("GuiNUmpad: updateScreen");
    }

    /**
     * Adds the buttons (and other controls) to the screen in question.
     */
    public void initGui()
    {
        StringTranslate stringtranslate = StringTranslate.getInstance();
        Keyboard.enableRepeatEvents(true);
        this.buttonList.clear();
        this.buttonList.add(this.button1 = new GuiButton(1, this.width / 2 - 150, 47, "1"));
        this.buttonList.add(this.button2 = new GuiButton(2, this.width / 2 - 150, 70, "2"));
        this.buttonList.add(this.button3 = new GuiButton(3, this.width / 2 - 150, 93, "3"));
        this.buttonList.add(this.button4 = new GuiButton(4, this.width / 2 - 150, 116, "4"));
        this.buttonList.add(this.button5 = new GuiButton(5, this.width / 2 - 150, 139, "5"));
        this.buttonList.add(this.button6 = new GuiButton(6, this.width / 2 - 150, 162, "6"));
        this.buttonList.add(this.button7 = new GuiButton(7, this.width / 2 - 150, 185, "7"));
        this.buttonList.add(this.buttonSet = new GuiButton(10, this.width / 2 - 150, 198, "Set code"));
        this.buttonList.add(this.buttonClear = new GuiButton(11, this.width / 2, 198, "Cancle"));
       // this.player.addChatMessage("GuiNUmpad: initGui");
    }

    /**
     * Called when the screen is unloaded. Used to disable keyboard repeat events
     */
    public void onGuiClosed()
    {
        Keyboard.enableRepeatEvents(false);
       // this.player.addChatMessage("GuiNUmpad: onGuiClosed");
    }

    /**
     * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
     */
    protected void actionPerformed(GuiButton par1GuiButton)
    {
    	//this.player.addChatMessage("GuiNUmpad: actionPerformed");
        if (par1GuiButton.enabled)
        {
            if (par1GuiButton.id == 1)
            {
            	if(codeSize < 4){
            		
            		code = code+"1";
            		codeSize++;
            		
            	}

            }else if (par1GuiButton.id == 2)
            {
            	if(codeSize < 4){
            		
            		code = code+"2";
            		codeSize++;
            		
            	}

            }else if (par1GuiButton.id == 3)
            {
            	if(codeSize < 4){
            		
            		code = code+"3";
            		codeSize++;
            		
            	}

            }else if (par1GuiButton.id == 4)
            {
            	if(codeSize < 4){
            		
            		code = code+"4";
            		codeSize++;
            		
            	}

            }
            else if (par1GuiButton.id == 5)
            {
            	if(codeSize < 4){
            		
            		code = code+"5";
            		codeSize++;
            		
            	}

            }
            else if (par1GuiButton.id == 6)
            {
            	if(codeSize < 4){
            		
            		code = code+"6";
            		codeSize++;
            		
            	}

            }
            else if (par1GuiButton.id == 7)
            {
            	if(codeSize < 4){
            		
            		code = code+"7";
            		codeSize++;
            		
            	}

            }else if (par1GuiButton.id == 10){
            	
            	if(codeSize == 4){
            		
            		try {
					setCode(world, x, y, z, code, "", "codes.properties");
				} catch (IOException e) {
					e.printStackTrace();
				}
            		
            	}
            	
            }else if(par1GuiButton.id == 11){
            	
            	code = null;
            	codeSize = (Integer) null;
            	this.mc.displayGuiScreen((GuiScreen)null);
            	
            	
            }
            else if (par1GuiButton.id == 0)
            {
                String s = "IntlckNumComb";
                ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
                DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);

                try
                {
                    dataoutputstream.writeInt(this.numpadBlock.xCoord);
                    dataoutputstream.writeInt(this.numpadBlock.yCoord);
                    dataoutputstream.writeInt(this.numpadBlock.zCoord);
                    this.mc.getNetHandler().addToSendQueue(new Packet250CustomPayload(s, bytearrayoutputstream.toByteArray()));
                }
                catch (Exception exception)
                {
                    exception.printStackTrace();
                }

                this.mc.displayGuiScreen((GuiScreen)null);
            }
        }
    }

    /**
     * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
     */
    protected void keyTyped(char par1, int par2)
    {
    	//this.player.addChatMessage("GuiNUmpad: keyTyped");
    	/*
        this.enteredCodeField.textboxKeyTyped(par1, par2);
        this.doneBtn.enabled = this.enteredCodeField.getText().trim().length() > 0;
        */

        if (par2 != 28 && par1 != 13)
        {
            if (par2 == 1)
            {
               // this.actionPerformed(this.buttonClear);
            }
        }
        else
        {
            //this.actionPerformed(this.doneBtn);
        }
    }

    /**
     * Called when the mouse is clicked.
     */
    protected void mouseClicked(int par1, int par2, int par3)
    {
        super.mouseClicked(par1, par2, par3);
       // this.enteredCodeField.mouseClicked(par1, par2, par3);
    }

    /**
     * Draws the screen and all the components in it.
     */
    public void drawScreen(int par1, int par2, float par3)
    {
    	//this.player.addChatMessage("GuiNumpad: drawScreen");
        StringTranslate stringtranslate = StringTranslate.getInstance();
        this.drawDefaultBackground();
        this.drawCenteredString(this.fontRenderer, "Set 4-digit code", this.width / 2, this.height / 4 - 60 + 20, 16777215);
        /*
        this.drawCenteredString(this.fontRenderer, stringtranslate.translateKey("advMode.setCommand"), this.width / 2, this.height / 4 - 60 + 20, 16777215);
        this.drawString(this.fontRenderer, stringtranslate.translateKey("advMode.command"), this.width / 2 - 150, 47, 10526880);
        this.enteredCodeField.drawTextBox();
        */
        super.drawScreen(par1, par2, par3);
    }
    
    public static String getCode(World world, int x, int y, int z, String path, String filename) throws IOException{
    	
    	Config config = new Config();
    	
    	String code = config.getStringKey(world+""+x+""+y+""+z, path, filename);
    	
    	return code;
    	
    }
    
    public void setCode(World world, int x, int y, int z, String value, String path, String filename) throws IOException{
    	
    	Config config = new Config();
    	
    	config.setStringKey(world+""+x+""+y+""+z, value, path, filename);
    	        	
    }
}

 

 

My packet handler

 

package vivadaylight3.interlock.handlers;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;

public class InterlockPacketHandler implements IPacketHandler{

@Override
public void onPacketData(INetworkManager manager,
		Packet250CustomPayload packet, Player player) {

	if(packet.channel.equals("Interlock")){

		handlePacket(packet);

	}

}

public void handlePacket(Packet250CustomPayload packet){

	DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));

	int randomInt1;
	int randomInt2;

	try{

		randomInt1 = inputStream.readInt();
		randomInt2 = inputStream.readInt();

	}catch(IOException e){

		e.printStackTrace();
		return;

	}

	System.out.println(randomInt1+""+randomInt2);

}

}

 

 

My GUI Handler

 

package vivadaylight3.interlock.handlers;

import vivadaylight3.interlock.Interlock;
import vivadaylight3.interlock.tileentities.numpadblock.GuiNumpadBlock;
import vivadaylight3.interlock.tileentities.numpadblock.TileEntityNumpadBlock;
import vivadaylight3.interlock.tileentities.polishermachine.ContainerPolisherMachine;
import vivadaylight3.interlock.tileentities.polishermachine.GuiPolisherMachine;
import vivadaylight3.interlock.tileentities.polishermachine.TileEntityPolisherMachine;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;

public class InterlockGuiHandler implements IGuiHandler{

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z) {

	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

	if(tileEntity instanceof TileEntityPolisherMachine){

		player.addChatMessage("InterlockGuiHandler: Server, Is polisher");

		return new ContainerPolisherMachine(player.inventory, (TileEntityPolisherMachine) tileEntity);

	}else if(tileEntity instanceof TileEntityNumpadBlock){

		player.addChatMessage("InterlockGuiHandler: Server, Is numpad block");

		return new TileEntityNumpadBlock();

	}

	return null;

}

public Object getClientGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z) {

	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

	if(tileEntity instanceof TileEntityPolisherMachine){

		player.addChatMessage("InterlockGuiHandler: Is polisher machine");

		return new GuiPolisherMachine(player.inventory, (TileEntityPolisherMachine) tileEntity);

	}else if(tileEntity instanceof TileEntityNumpadBlock){

		player.addChatMessage("InterlockGuiHandler: Is numpad block");

		return new GuiNumpadBlock((TileEntityNumpadBlock) tileEntity, world, x, y, z);

	}

	return null;
}



}

 

 

The tile entity class (Shouldn't need any content if it doesn't have an inventory)

 

package vivadaylight3.interlock.tileentities.numpadblock;

import net.minecraft.tileentity.TileEntity;

public class TileEntityNumpadBlock extends TileEntity {

}

 

 

My GUI handler

 

package vivadaylight3.interlock.handlers;

import vivadaylight3.interlock.Interlock;
import vivadaylight3.interlock.tileentities.numpadblock.GuiNumpadBlock;
import vivadaylight3.interlock.tileentities.numpadblock.TileEntityNumpadBlock;
import vivadaylight3.interlock.tileentities.polishermachine.ContainerPolisherMachine;
import vivadaylight3.interlock.tileentities.polishermachine.GuiPolisherMachine;
import vivadaylight3.interlock.tileentities.polishermachine.TileEntityPolisherMachine;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;

public class InterlockGuiHandler implements IGuiHandler{

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z) {

	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

	if(tileEntity instanceof TileEntityPolisherMachine){

		player.addChatMessage("InterlockGuiHandler: Server, Is polisher");

		return new ContainerPolisherMachine(player.inventory, (TileEntityPolisherMachine) tileEntity);

	}else if(tileEntity instanceof TileEntityNumpadBlock){

		player.addChatMessage("InterlockGuiHandler: Server, Is numpad block");

		return new TileEntityNumpadBlock();

	}

	return null;

}

public Object getClientGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z) {

	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

	if(tileEntity instanceof TileEntityPolisherMachine){

		player.addChatMessage("InterlockGuiHandler: Is polisher machine");

		return new GuiPolisherMachine(player.inventory, (TileEntityPolisherMachine) tileEntity);

	}else if(tileEntity instanceof TileEntityNumpadBlock){

		player.addChatMessage("InterlockGuiHandler: Is numpad block");

		return new GuiNumpadBlock((TileEntityNumpadBlock) tileEntity, world, x, y, z);

	}

	return null;
}



}

 

 

If you have experience with forge modding, you'll probably understand what's going on in each file, if not, just ask :)

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Link to comment
Share on other sites

just another small question,

 

what do i need to send packets for (and how do i do/handle this properly)? I'm trying to do it so that the range, and detection types in my proximity detector can be changed, so do i need to send a packet each time one of these buttons is pressed, and also how do i send and use the data on the button?

 

also, where is the id for the gui defined? in the following line form your code it is called, but i can't see where it is created or how to know what it will be.

 

par5EntityPlayer.openGui(Interlock.instance, 2, world, x, y, z);

 

[EDIT]

Also, can i see how you registered your package handlers and channels, doing it in the same way as the wiki tutorial gave me a crash saying that the channel name was incorrect or something.

Link to comment
Share on other sites

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.

×
×
  • Create New...

Important Information

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