Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.6.4] addChatMessage on a custom event


Recommended Posts

I'm trying to simply show a message when a custom event fires.

 

@ForgeSubscribe
public void onPlayerPlaceFurnace(PlayerInteractEvent event) {
	EntityPlayer p = event.entityPlayer;
	if(event.action == Action.RIGHT_CLICK_BLOCK) {
		ItemStack s = p.inventory.getCurrentItem();
		if(s.itemID == Block.furnaceIdle.blockID) {
			this.mc.getNetHandler().getPlayer().addChatMessage("Furnace Placed");
		}
	}
}

 

The problem I'm running into is whenever I actually place a furnace, I see "Furnace Placed" twice in chat. I can't figure out why this fires twice. Is there something else I should be doing?

Link to post
Share on other sites

I've added isRemote, however that did not help. I've noticed something different in the console. Whenever the event fires this happens:

 

2014-02-11 21:20:06 [iNFO] [Minecraft-Server] Saving and pausing game...
2014-02-11 21:20:06 [iNFO] [Minecraft-Server] Saving chunks for level 'TestCreative'/Overworld
2014-02-11 21:20:06 [iNFO] [Minecraft-Server] Saving chunks for level 'TestCreative'/Nether
2014-02-11 21:20:06 [iNFO] [Minecraft-Server] Saving chunks for level 'TestCreative'/The End
2014-02-11 21:20:11 [iNFO] [Minecraft-Client] [CHAT] Furnace Placed
2014-02-11 21:20:11 [iNFO] [Minecraft-Client] [CHAT] Furnace Placed
2014-02-11 21:20:12 [iNFO] [Minecraft-Server] Saving and pausing game...

 

So it looks like it's actually the client that fires twice.

Link to post
Share on other sites

@ForgeSubscribe
public void onPlayerPlaceFurnaceOld(PlayerInteractEvent event) {
	EntityPlayer p = event.entityPlayer;
	if(event.action == Action.RIGHT_CLICK_BLOCK) {
		ItemStack s = p.inventory.getCurrentItem();
		if(s.itemID == Block.furnaceIdle.blockID) {
			if(!this.mc.theWorld.isRemote) {
				this.mc.getNetHandler().getPlayer().addChatMessage("Furnace Placed");
			}
		}
	}
}

 

In this case, I don't get any chat messages. if I change it to:

 

if(this.mc.theWorld.isRemote) {
				this.mc.getNetHandler().getPlayer().addChatMessage("Furnace Placed");
			}

 

I get the double message again. I don't know if this is worth noting, but I have another event that checks if player breaks a furnace.

 

@ForgeSubscribe
public void onPlayerDestroyFurnace(BreakEvent event) {
	Block b = event.block;
	if(b == Block.furnaceIdle || b == Block.furnaceBurning) {
		this.mc.getNetHandler().getPlayer().addChatMessage("Furnace Destroyed");
	}
}

 

And this displays correctly, single message.

Link to post
Share on other sites

@ForgeSubscribe

public void onPlayerPlaceFurnace(PlayerInteractEvent event) {

EntityPlayer p = event.entityPlayer;

if(event.action == Action.RIGHT_CLICK_BLOCK) {

ItemStack s = p.inventory.getCurrentItem();

if(s.itemID == Block.furnaceIdle.blockID) {

this.mc.getNetHandler().getPlayer().addChatMessage("Furnace Placed");

}

}

}

TO

@ForgeSubscribe

public void onPlayerPlaceFurnace(PlayerInteractEvent event) {

EntityPlayer p = event.entityPlayer;

if(event.action == Action.RIGHT_CLICK_BLOCK) {

ItemStack s = p.inventory.getCurrentItem();

if(s.itemID == Block.furnaceIdle.blockID && !p.worldObj.isRemote) {

p.addChatMessage("Furnace Placed");

}

}

}

Link to post
Share on other sites

MovingObjectPosition m = new MovingObjectPosition(e.entityPlayer);

if(!e.entityPlayer.worldOb.isRemote && (e.entityPlayer.worldObj.getBlock(m.blockX, m.blockY, m.blockZ) == Block.furnaceIdle ||e.entityPlayer.worldObj.getBlock(m.blockX, m.blockY, m.blockZ) == Block.furnaceActivated)){

//do your stuff

}

 

Edit if you want a smp mod..., send the messages on the furnace destroy event, the same way as i told you to use on the interact event.....

Link to post
Share on other sites

My Bad

change the moving object position to

MovingObjectPosition m = Minecraft.getMinecraft().objectMouseOver;

 

Thank you for all the help. You are awesome!

 

Stop using the Minecraft client on server side, please.

 

I realize this is a simple mistake, but I just started to figure forge out a few days ago. I didn't know that calling addChatMessage would call it on both sides. How do I know when to check for isRemote?

Link to post
Share on other sites
I realize this is a simple mistake, but I just started to figure forge out a few days ago. I didn't know that calling addChatMessage would call it on both sides. How do I know when to check for isRemote?

 

Every time you need to do something that alters the world, or objects within it (and checking not-remote).

Every time you need to do something that is specific to the person-who-is-playing (rendering, input, etc).

 

Also, never ever use Minecraft.getMinecraft() for any reason.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to post
Share on other sites

Also, never ever use Minecraft.getMinecraft() for any reason.

Why not? I use it all the time :P

 

Because you know what it's for.  All these newbs go and do stuff like this:

 

 

function someArbitraryFoo(World world, int x, int y, int z) {
    if(!Minecraft.getMinecraft().getWorld().isRemote) {
        //do stuff
    }
}

 

Because they think that's the only way to get access to a world object.  It's like, "hello, it's passed to you" or "hello, that object contains a reference to its own world object" or "hello, you're coding inside an entity, it has one already."

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to post
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.

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



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.