Jump to content

Recommended Posts

Posted

Hello Modders,

 

I am quite new to Client Modding and now have a problem with handeling Events with Forge.

 

Here is my code:

 

@EventHandler
public void init(FMLInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(new Events(this));
}

 

and in the Events Class:

@SubscribeEvent
public void chatEvent(ServerChatEvent event)
{
	System.out.println("Chat registered");
}

@SubscribeEvent
public void blockBreak(BreakEvent event)
{
	System.out.println("Block has been broken.");
}

 

I do not get any Errors which can help me.

If i start the client (via Eclipse) Forge tells me the mod is loaded but nothing happens when i write (with my second account) in chat or if i break a block.

 

I used the breakBlock event to check wether the ChatEvent is just lost or wheter i have a problem with the Events.

 

I am Stuck with this for 36 hours now and i searched all over Google and Forums for a solution.

If I am just too stupid to find anything please let me know.

 

When my english is corrupted somewhere please forgive me i am german and my writing skills are not the best...

 

Thanks for your help.

Posted

Sure here my Classes:

 

First The Main:

 

package de.recklessGreed.chatMod;

import ibxm.Player;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

@Mod(modid = Reference.Mod_ID, name = Reference.Mod_Name, version = Reference.Version)
public class ChatControlMain
{
@Instance(value = Reference.Mod_ID)
public static ChatControlMain instance;
EntityPlayerSP user = Minecraft.getMinecraft().thePlayer;

@EventHandler
public void init(FMLInitializationEvent event)
{
	MinecraftForge.EVENT_BUS.register(new Events(this));
}
}

 

and the Events:

 

package de.recklessGreed.chatMod;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class Events
{
ChatControlMain classMain;

public Events(ChatControlMain main)
{
	classMain = main;
}

@SubscribeEvent
public void chatEvent(ServerChatEvent event)
{
	/*String message 	      = event.message;
	String username       = event.username;
	String[] words = message.split(" ");
	EntityPlayerMP sender = event.player;
	if(checkForWord(words, "hallo")) 
	{
		classMain.user.sendChatMessage("Hallo. This is an automatic Message.");
	}*/
	System.out.println("Chat registered");
}

@SubscribeEvent
public void blockBreak(BreakEvent event)
{
	System.out.println("Block has been broken.");
}

public boolean checkForWord(String[] words, String cmd)
{
	for(String word : words)
	{
		if(word.equalsIgnoreCase(cmd)) return true;
	}
	return false;
}
}

 

Refercences just holds the MOD ID, The Version and the Name

Posted

First things first. There are some serious problems in your code.

 

1.

 EntityPlayerSP user = Minecraft.getMinecraft().thePlayer; 

Why are you doing that? There is no need to do that and also this will crash if you load the mod on a server, because the Minecraft class is Client sided.

2.

public Events(ChatControlMain main)
{
	classMain = main;
}

There is really no need to save a reference to ur main class in ur Events class if ur Main class has a static instance.

 

Right now I am not sure why ur events arent getting called.

 

Posted

He wasn't really giving an opinion so much as facts.  You should follow the best practices on those instance.  The PlayerSP will crash your mod.

 

 

You said in your first post that "when i load this in client".  Those events I believe are server side.  Not really sure if you will ever see them with just the client running.  Then again, I haven't played much with mods in just the single player environment.

 

 

 

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

You said in your first post that "when i load this in client".  Those events I believe are server side.  Not really sure if you will ever see them with just the client running.  Then again, I haven't played much with mods in just the single player environment.

 

In single player, the integrated server will fire all server-side events (including

BreakEvent

and

ServerChatEvent

).

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.