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

I want to get mc.currentScreen, but when I start mod currentScreen == null. Why?

ExampleMod.java

 

Spoiler

package com.example.examplemod;

import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.settings.KeyBinding;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import net.minecraft.util.ChatComponentText;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.common.MinecraftForge;
import org.lwjgl.input.Keyboard;

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

    /* Key Bindings */

    public static KeyBinding keyBinding = new KeyBinding("Test param", Keyboard.KEY_RIGHT, "Test container");

    @EventHandler
    public void init(FMLInitializationEvent event)
    {

        /* Key-Registery */
        ClientRegistry.registerKeyBinding(keyBinding);
        FMLCommonHandler.instance().bus().register(new KeyHandler());
        MinecraftForge.EVENT_BUS.register(new KeyHandler());
    }




}

 

 

 

 

 

KeyHandler.java

Spoiler

package com.example.examplemod;

import ...;


public class KeyHandler {

    private static final double WALK_PER_STEP = 4.3 / 20;


    private  Minecraft mc = Minecraft.getMinecraft();

 @SubscribeEvent
    public  void pickup(PlayerInteractEvent event)
    {
        GuiScreen screen = Minecraft.getMinecraft().currentScreen;
        if (screen == null){
            mc.thePlayer.addChatComponentMessage(new ChatComponentText("Screen is null :("));
            return;
        }

        mc.thePlayer.addChatComponentMessage(new ChatComponentText("Hello World!"));
    }
}

 

 

 

 

I recive message: Screen is null :(

Why? How to fix it?

Edited by Hello World
Update code

  • Author

I want to get mc.currentScreen, but when I start mod currentScreen == null. Why?

ExampleMod.java

 

Spoiler

package com.example.examplemod;

import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.settings.KeyBinding;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import net.minecraft.util.ChatComponentText;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.common.MinecraftForge;
import org.lwjgl.input.Keyboard;

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

    /* Key Bindings */

    public static KeyBinding keyBinding = new KeyBinding("Test param", Keyboard.KEY_RIGHT, "Test container");

    @EventHandler
    public void init(FMLInitializationEvent event)
    {

        /* Key-Registery */
        ClientRegistry.registerKeyBinding(keyBinding);
        FMLCommonHandler.instance().bus().register(new KeyHandler());
        MinecraftForge.EVENT_BUS.register(new KeyHandler());
    }




}

 

 

 

 

 

KeyHandler.java

Spoiler

package com.example.examplemod;

import ...;


public class KeyHandler {

    private static final double WALK_PER_STEP = 4.3 / 20;


    private  Minecraft mc = Minecraft.getMinecraft();

 @SubscribeEvent
    public  void pickup(PlayerInteractEvent event)
    {
        GuiScreen screen = Minecraft.getMinecraft().currentScreen;
        if (screen == null){
            mc.thePlayer.addChatComponentMessage(new ChatComponentText("Screen is null :("));
            return;
        }

        mc.thePlayer.addChatComponentMessage(new ChatComponentText("Hello World!"));
    }
}

 

 

 

 

I recive message: Screen is null :(

Why? How to fix it?

42 minutes ago, Hello World said:

Moderators, delete pls 

Why are you using the default java code then adding your own code I would recommend making your own package

@SubscribeEvent
    public  void pickup(PlayerInteractEvent event)
    {
        GuiScreen screen = Minecraft.getMinecraft().currentScreen;
        if (screen == null){
            mc.thePlayer.addChatComponentMessage(new ChatComponentText("Screen is null :("));

            return;
        }

Do you know how to code? in the keyhandler file it says Screen Is Null  

mc.thePlayer.addChatComponentMessage(new ChatComponentText("Screen is null :("));

5 minutes ago, Avan12IsDead said:

Do you know how to code? in the keyhandler file it says Screen Is Null 

That is his debug message. He knows what he is doing. He is asking why Minecraft.getMinecraft().currentScreen is null.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

  • Author

@Avan12IsDead  I know it. But why? When I open chest some error. ( I want to get GuiContainer from chest)

Edited by Hello World

1 minute ago, Hello World said:

@Avan12IsDead  I know it. But why? When I open chest some error. ( I want to get GuiContainer from chest)

Explain what your code is doing from your mod.

1. PlayerInteractEvent can be triggered from the server side, in which case the currentScreen is null, as the server side does not have a currently opened GUI. The currentScreen is only client side.

2. Please elucidate what you are trying to do, and we can perhaps figure out an alternative solution. We need some context of your problem.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

  • Guest locked this topic
Guest
This topic is now closed to further replies.

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.