Jump to content

Recommended Posts

Posted

I decided to make the mod I'm working on a bit fancier, so I started using particle effects.

Everything is well and good, however, one block makes problems.

As it prevents the player's death, I had to use the LivingDeathEvent, which is only fired server-side.

I want to spawn some particles when said block prevents a player's death, which isn't possible on its own when the Method is only fired

by the sever, hence I looked into packets.

With the packets everything works so far, but the strange thing is, that even though the server sends a packet to the client,

the world is still no remote (world.isRemote is false).

 

Here is the neccessary code:

 

PacketHaandler.java:

public class PacketHandler {

public static final SimpleNetworkWrapper
	NETWORK = NetworkRegistry.INSTANCE.newSimpleChannel(LibMisc.MODID);

public static void registerPackets(){
	int id = 0;
	NETWORK.registerMessage(PacketRosurrectSaved.class, PacketRosurrectSaved.class, id++, Side.CLIENT);
}

}

 

PacketRosurrectSaved.java:

public class PacketRosurrectSaved implements IMessage, IMessageHandler<PacketRosurrectSaved, IMessage>{

int 
	worldID;

double
	x,
	y,
	z;

boolean
	headBreak;

public PacketRosurrectSaved() {}

public PacketRosurrectSaved(int id, double x, double y, double z, boolean head) {
	worldID = id;
	this.x = x;
	this.y = y;
	this.z = z;
	headBreak = head;
}

@Override
public void fromBytes(ByteBuf buf) {
	worldID = buf.readInt();
	x = buf.readDouble();
	y = buf.readDouble();
	z = buf.readDouble();
	headBreak = buf.readBoolean();
}

@Override
public void toBytes(ByteBuf buf) {
	buf.writeInt(worldID);
	buf.writeDouble(x);
	buf.writeDouble(y);
	buf.writeDouble(z);
	buf.writeBoolean(headBreak);
}

@Override
public IMessage onMessage(PacketRosurrectSaved message, MessageContext ctx) {
	World world = DimensionManager.getProvider(message.worldID).worldObj; //This is the line I suspect to be the culprit, but I don't know any other way to get the World object from the dimension id
	if(!message.headBreak)
		Methods.spawnParticle("fireworksSpark", world, message.x, message.y, message.z, 10, 4, 1.0, 0.2, 0.5, 0.2);
	else
		world.playAuxSFX(2001, (int)message.x, (int)message.y, (int)message.z, Block.getIdFromBlock(world.getBlock((int)message.x, (int)message.y, (int)message.z)) + (world.getBlockMetadata((int)message.x, (int)message.y, (int)message.z)) << 12);
	return null;
}

}

 

Rosurrect.onPlayerDeath():

	@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onPlayerDeath(LivingDeathEvent event){
	if(event.entity instanceof EntityPlayer){
		EntityPlayer player = (EntityPlayer) event.entity;
		int i = 0;
		for(GameProfile profile : boundPlayers){
			if(profile != null && areEqual(profile, player.getGameProfile())){
				if(mana < getMaxMana()){
					chat(player, StatCollector.translateToLocal("bu.rosurrect.failed"));
					return;
				}
				int x = supertile.xCoord + cardinals[i].offsetX;
				int y = supertile.yCoord;
				int z = supertile.zCoord + cardinals[i].offsetZ;
				event.setCanceled(true);
				player.setHealth(player.getMaxHealth());
				chat(player, StatCollector.translateToLocal("bu.rosurrect.success"));
				supertile.getWorldObj().setBlock(x, y, z, Blocks.air);
				PacketHandler.NETWORK.sendToAll(new PacketRosurrectSaved(supertile.getWorldObj().provider.dimensionId, x, y, z, true));					
				PacketHandler.NETWORK.sendToAll(new PacketRosurrectSaved(supertile.getWorldObj().provider.dimensionId, supertile.xCoord, supertile.yCoord, supertile.zCoord, false));
				mana = 0;
				return;
			}
			i++;
		}
	}
}

 

Methods.spawnParticle():

public static void spawnParticle(String tag, World world, double x, double y, double z, int max, int min, double chance, double motionXModifier, double motionYModifier, double motionZModifier){
	if(!world.isRemote || Math.random() >= chance)
		return;
	int count = world.rand.nextInt(max - min) + min;
	for(int i = 0; i < count; i++){
		world.spawnParticle(tag, x + Math.random(), y + Math.random(), z + Math.random(), world.rand.nextGaussian() * motionXModifier, world.rand.nextGaussian() * motionYModifier, world.rand.nextGaussian() * motionZModifier);
	}
}

 

Posted

1. What MC are you on? In 1.8 you need: http://greyminecraftcoder.blogspot.com.au/2015/01/thread-safety-with-network-messages.html

 

2. You got the idea wrong (with dimension and worlds).

2.1. When sending data to client about some effect, you don't send "world" - client has only ONE world, that is, the one player is currently in.

2.2 You send only x/y/z, the world you get with Minecraft.getMinecraft().theWorld (Note: have look at thread safety and @SideOnly(Side.CLIENT)

2.2.1. To bypass problem of using Minecraft (client only) class in packet handler - use proxies:

http://www.minecraftforge.net/forum/index.php/topic,32585.msg170364.html#msg170364

 

3. What else... Oh! Particles are render-stuff. Send packet about their spawn only to players around location, not all.

1.7.10 is no longer supported by forge, you are on your own.

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.