Jump to content

[1.7.2][Solved] Sending text from onBlockActivated sends message twice


Recommended Posts

Posted

So i want it to be so that when you right click the block with a item, it prints the fluid inside the tank to the chat.

I have got it so that it prints it to the chat, but it prints it twice.

}else if(heldtItem.isItemEqual(new ItemStack(CompactTanks.meter))){ //checks if it's the right item
Debug.consoleln("Fluid Meter found"); //Everything prefixed with Debug just prints to the console
if(tileSmallTank.isTankEmpty()){ 
	ChatHandler.sendToPlayer("The tank is empty", par5EntityPlayer);//the text that gets printed twice
}else{
	ChatHandler.sendToPlayer("Current fluid in tank: " + tileSmallTank.getFluidName(), par5EntityPlayer);//the text that gets printed twice
	ChatHandler.sendToPlayer("Amount of fluid in tank: " + tileSmallTank.getFluidAmount(), par5EntityPlayer);//the text that gets printed twice
	return true;
}
}

 

The code in the ChatHandler looks like this:

public class ChatHandler {

public static void sendToPlayer(String par1Message, EntityPlayer par2Player){
	if(par1Message.length() > 0){
		par2Player.addChatMessage(new ChatComponentTranslation(par1Message));
	}
}

}

 

and the isTankEmpty code looks lige this:

public boolean isTankEmpty(){
	if(tank.getFluid() == null){
		return true;
	}else{
		return false;
	}
}

 

 

All the code can be found here: https://github.com/simwir/Compact-Tanks/tree/Tank

and the files in question are:

ChatHandler - https://github.com/simwir/Compact-Tanks/blob/Tank/main/java/simwir/ct/handler/ChatHandler.java

BlockSmallTank - https://github.com/simwir/Compact-Tanks/blob/Tank/main/java/simwir/ct/blocks/BlockSmallTank.java

TileSmallTank - https://github.com/simwir/Compact-Tanks/blob/Tank/main/java/simwir/ct/tile/TileSmallTank.java

 

Posted

The method gets called twice, once for client and once for server. You should only send the message on the server (

world.isRemote == false

).

 

Thanks that worked :D

Posted

diesieben, I know this is solved, but was looking around the forum, and notice you used == false. You can just do

if(!world.isRemote)

. Just though I would point it out, I generally use that method because its faster.

 

Happy coding!

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.