Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hi guys. I want to make custom gui for my block, but gui doesn't open. Here is code

[spoiler=MyMod.java]

package com.mymod;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;


@Mod(modid = MyMod.MODID, version = MyMod.VERSION)
public class MyMod {
    public static final String MODID = "MyMod";
    public static final String VERSION = "1.0";

    @Mod.Instance(MyMod.MODID) public static MyMod instance;

    public static Logger log = Logger.getLogger("Minecraft");

    public static Block MyModBlock;

    public MyMod() {
        instance = this;
    }

    @Mod.EventHandler
    public void preInit(FMLPreInitializationEvent event) {
        MyModBlock = new MyModBlock();

        GameRegistry.registerBlock(MyModBlock, "MyModBlock");

        GameRegistry.addRecipe(new ItemStack(MyModBlock),
                new Object[]{"###", "## ", "###", Character.valueOf('#'), Items.iron_ingot});

        NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler());

        log.info("MyMod loaded.");
    }
}

 

[spoiler=MyBlock.java]

package com.MyMod;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

public class MyModBlock extends Block {
    private IIcon[] icons = new IIcon[6];

    public MyModBlock() {
        super(Material.iron);

        this.setBlockName("MyModblock");
        this.setCreativeTab(CreativeTabs.tabMisc);
        this.setBlockTextureName("MyMod:MyModblock");
    }

    @Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9){
        if (!world.isRemote) {
            System.out.print("block clicked, open gui.\n");
            System.out.print(MyMod.instance != null);
            player.openGui(MyMod.instance, Gui.GUI_ID, world, x, y, z);
        }
        return true;
    }

    @Override
    public void registerBlockIcons(IIconRegister reg) {
        for (int i = 0; i < 6; i ++) {
            this.icons[i] = reg.registerIcon(this.textureName + "_" + i);
        }
    }

    @Override
    public IIcon getIcon(int side, int meta) {
        return this.icons[side];
    }
}

 

[spoiler=GuiHandler.java]

package com.mymod;

import cpw.mods.fml.common.network.IGuiHandler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;

public class GuiHandler implements IGuiHandler {
    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        return null;
    }

    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        if (ID == Gui.GUI_ID) {
            System.out.print("create gui.\n");
            return new Gui();
        }
        return null;
    }
}

 

[spoiler=Gui.java]

package com.mymod;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;

public class Gui extends GuiScreen {
    public static final int GUI_ID = 20;

    public Gui() {
        System.out.print("gui constructor.\n");

    }

    @Override
    public void initGui() {
        buttonList.clear();
        buttonList.add(new GuiButton(0, 100, 100, 60, 60, "Button"));
    }

    @Override
    public boolean doesGuiPauseGame() {
        return false;
    }

    @Override
    public void drawScreen(int i, int j, float f) {
        drawDefaultBackground();
        super.drawScreen(i, j, f);
        System.out.print("drawing gui.\n");
    }

    @Override
    public void actionPerformed(GuiButton button) {
        System.out.print("button clicked.\n");

    }
}

 

  • Author

Thanks, this is working. And if I want to make craft system, I need create Container? Can you show me a simple example? ::)

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.