Jump to content

Recommended Posts

Posted (edited)

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

Posted
@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 :("));

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

 

 

Posted

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.

Announcements



×
×
  • Create New...

Important Information

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