Jump to content

[1.7.10]How can i make a Gui extending GuiScreen [SOLVED]


RR55

Recommended Posts

Hello,

I want to make a simple GUI and I want to open this GUI with a command. The GUI should only display a "Help" with icons (Blocks and Items) and text (Description) but that's not my problem. I cant open the GUI. Whenever I run the command I get an error.

 

[server thread/ERROR]: Couldn't process command: 'mycommand'

java.lang.ClassCastException: tk.MyName.MyMod.gui.GuiHelp cannot be cast to net.minecraft.inventory.Container

 

 

Command Class:

public class CommandMain extends CommandBase
{
protected Minecraft mc;

    public String getCommandName(){
        return "mycommand";
    }

    /**
     * Return the required permission level for this command.
     */
    public int getRequiredPermissionLevel(){
        return 0;
    }
    
    /**
     * Return the required permission level for this command.
     */
    public String getCommandUsage(ICommandSender commandSender){
        return "Usage";
    }

    public void processCommand(ICommandSender commandSender, String[] strArray){
        
    	int i;
        WorldServer worldserver = MinecraftServer.getServer().worldServers[1];
        
        commandSender.addChatMessage(new ChatComponentTranslation("commands.main.heading",  new Object[0]));
        EntityPlayer     p = (EntityPlayer)commandSender;
        World            w = p.getEntityWorld();
        ChunkCoordinates c = commandSender.getPlayerCoordinates();
        int x = c.posX;
        int y = c.posY;
        int z = c.posZ;

        System.out.println("CommandMain.java: call GUI");
        p.openGui(Main.instance, 100, w, x, y, z);
        System.out.println("CommandMain.java: done");   
    }

   
    
}

 

GuiHandler Class:

public class GuiHandler implements IGuiHandler{

public GuiHandler (){

}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	if(ID == 0){
		---
	}else if(ID == 1){
		---
	}else if(ID == 100){
		System.out.println("GuiHandler: getClientGuiElement ID == 100 called");
		return new GuiHelp(player);
	}
	return null;
    }

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	if(ID == 0){
		---
	}else if(ID == 1){
		---
	}else if(ID == 100){
		System.out.println("GuiHandler: getServerGuiElement ID == 100 called");

		return new GuiHelp(player);
	}
	return null; 
}
}

 

Gui Class: (its a modified copy of the DemoGui)

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

private static final Logger logger = LogManager.getLogger();
    private static final ResourceLocation field_146348_f = new ResourceLocation("textures/gui/demo_background.png");

    
    public GuiHelp(EntityPlayer player){
    	
    }
    
    @Override
    public boolean doesGuiPauseGame()
    {
        return false;
    }
    
    /**
     * Adds the buttons (and other controls) to the screen in question.
     */
    public void initGui()
    {
        this.buttonList.clear();
        byte b0 = -16;
        this.buttonList.add(new GuiButton(1, this.width / 2 - 116, this.height / 2 + 62 + b0, 114, 20, I18n.format("gui.help.ok", new Object[0])));
    }

    protected void actionPerformed(GuiButton button)
    {
        switch (button.id)
        {
            case 1:
                button.enabled = false;
                this.mc.displayGuiScreen((GuiScreen)null);
                this.mc.setIngameFocus();
        }
    }

    /**
     * Called from the main game loop to update the screen.
     */
    public void updateScreen()
    {
        super.updateScreen();
    }

    /**
     * Draws either a gradient over the background screen (when it exists) or a flat gradient over background.png
     */
    public void drawDefaultBackground()
    {
        super.drawDefaultBackground();
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(field_146348_f);
        int i = (this.width - 248) / 2;
        int j = (this.height - 166) / 2;
        this.drawTexturedModalRect(i, j, 0, 0, 248, 166);
    }

    /**
     * Draws the screen and all the components in it.
     */
    public void drawScreen(int p_73863_1_, int p_73863_2_, float p_73863_3_)
    {
        this.drawDefaultBackground();
        int k = (this.width - 248) / 2 + 10;
        int l = (this.height - 166) / 2 + 8;
        this.fontRendererObj.drawString(I18n.format("demo.help.title", new Object[0]), k, l, 2039583);
        l += 12;
        GameSettings gamesettings = this.mc.gameSettings;
        this.fontRendererObj.drawString(I18n.format("demo.help.movementShort", new Object[] {GameSettings.getKeyDisplayString(gamesettings.keyBindForward.getKeyCode()), GameSettings.getKeyDisplayString(gamesettings.keyBindLeft.getKeyCode()), GameSettings.getKeyDisplayString(gamesettings.keyBindBack.getKeyCode()), GameSettings.getKeyDisplayString(gamesettings.keyBindRight.getKeyCode())}), k, l, 5197647);
        this.fontRendererObj.drawString(I18n.format("demo.help.movementMouse", new Object[0]), k, l + 12, 5197647);
        this.fontRendererObj.drawString(I18n.format("demo.help.jump", new Object[] {GameSettings.getKeyDisplayString(gamesettings.keyBindJump.getKeyCode())}), k, l + 24, 5197647);
        this.fontRendererObj.drawString(I18n.format("demo.help.inventory", new Object[] {GameSettings.getKeyDisplayString(gamesettings.keyBindInventory.getKeyCode())}), k, l + 36, 5197647);
        this.fontRendererObj.drawSplitString(I18n.format("demo.help.fullWrapped", new Object[0]), k, l + 68, 218, 2039583);
        super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_);
    }

}

 

 

What am I doing wrong?

I think my GuiHandler is not right.

Yes. I know. I have a bad english. Sorry, but hey, I'm still alive! ;)

Link to comment
Share on other sites

You're right, your GuiHandler is not right. In the

getServerGuiElement(params)

method you return a Gui instead of a Container. If you don't want a container, you need to call the

EntityPlayer#openGui(params)

method client side (

world.isRemote

is true for client, false for server) and return null for

getServerGuiElement(params)

.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Changes:

 

GuiHandler

}else if(ID == 100){
System.out.println("GuiHandler: getServerGuiElement ID == 100 called");
return null;
}

 

Command

@SideOnly(Side.CLIENT)
    public void processCommand(ICommandSender commandSender, String[] strArray){
        
    	int i;
        WorldServer worldserver = MinecraftServer.getServer().worldServers[1];
        
        commandSender.addChatMessage(new ChatComponentTranslation("commands.main.heading",  new Object[0]));
        EntityPlayer     p = (EntityPlayer)commandSender;
        World            w = p.getEntityWorld();
        ChunkCoordinates c = commandSender.getPlayerCoordinates();
        int x = c.posX;
        int y = c.posY;
        int z = c.posZ;

        System.out.println("CommandMain.java: call GUI");
        if(w.isRemote){
        	System.out.println("CommandMain.java:CLIENT - World is remote");
        	p.openGui(Main.instance, 100, w, x, y, z);
        }else{
        	System.out.println("CommandMain.java:SERVER - World is not remote");
        }
       
        System.out.println("CommandMain.java: done");   
    }

 

But it doesnt work.

 

Console output:

 

CommandMain.java: call GUI

CommandMain.java:SERVER - World is not remote

CommandMain.java: done

Yes. I know. I have a bad english. Sorry, but hey, I'm still alive! ;)

Link to comment
Share on other sites

Commands are processed on the server side. You need to send a packet to the client that notifys the client to open the GUI. There's a nice tutorial on the SimpleNetworkWrapper in the tutorials section.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Thank you, but that's too deep for me :'(. I found another solution. I created a Dummy Container which displays the GUI. Now I cant use GuiScreen but it works.

Yes. I know. I have a bad english. Sorry, but hey, I'm still alive! ;)

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.

Announcements



×
×
  • Create New...

Important Information

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