Jump to content

[SOLVED] [1.7.10] Checking world /w packet?


Recommended Posts

Posted

I've setup a packet that contains 3 doubles (coordinates) that are sent to all clients within a 100 block radius of the same coordinates. 

 

 

IMessage msg = new SimplePacket.SimpleMessage(x, y, z);
PacketHandler.net.sendToAllAround(msg, new NetworkRegistry.TargetPoint(0, x, y, z, 100.0D));

ClientProxy.java

    public void SimpleMIn(SimplePacket.SimpleMessage message) {
    	System.out.println("Packet success: ( " + message.getX() + ", " + message.getY() + ", " + message.getZ() + ")");
    }

 

Message/Handler

public class SimplePacket implements IMessageHandler<SimpleMessage, IMessage>
{
	@Override
public IMessage onMessage(SimpleMessage message, MessageContext ctx)
{
  // just to make sure that the side is correct
  if (ctx.side.isClient())
  {
    double x = message.x;
    double y = message.y;
    double z = message.z;
    System.out.println("Is Client");
    Modfile.proxy.SimpleMIn(message);
  }
  return null;
}
  
  public static class SimpleMessage implements IMessage
  {
    private double x;
    private double y;
    private double z;
    
    // this constructor is required otherwise you'll get errors (used somewhere in fml through reflection)
    public SimpleMessage() {}
    
    public SimpleMessage(double x, double y, double z)
    {
      this.x = x;
      this.y = y;
      this.z = z;
    }
    
    @Override
    public void fromBytes(ByteBuf buf)
    {
      // the order is important
      this.x = buf.readDouble();
      this.y = buf.readDouble();
      this.z = buf.readDouble();
    }
    
    @Override
    public void toBytes(ByteBuf buf)
    {
      buf.writeDouble(x);
      buf.writeDouble(y);
      buf.writeDouble(z);
    }
    
    public double getX() {
    	return this.x;
    }
    public double getY() {
    	return this.y;
    }
    public double getZ() {
    	return this.z;
    }
  }

 

 

Console output shows the coordinates are being sent to the client successfully (though I don't know if I should be using getters). What I'm having trouble with is having the world and more importantly the correct world to create the client event in. I've seen something along these lines for armor update events and then using entity.worldObj.(clientevent) but I'm not working with entity's rather coordinates generated by getMovingObjectPositionFromPlayer.

 

    public void SimpleMIn(SimplePacket.SimpleMessage message) {
    	Entity entity = FMLClientHandler.instance().getWorldClient().getEntityById(message.entityID); // message sends the entityID
    }

 

I'm thinking my best option is to send the world ID (-1, 0 or 1) the event occurs in from the server end and check that against the players receiving the packet. i.e.

 

    public void SimpleMIn(SimplePacket.SimpleMessage message) {
    	//if (message.worldID == EntityPlayer.worldObj.)
    		// EntityPlayer.worldObj.(do stuff)
    }

 

How do I refer to "this client's world" (i.e. EntityPlayer) in ClientProxy and use it to specify and verify whether or not to do something for that player?

Posted

The client only ever has one world: The world the player is currently in: Minecraft.getMinecraft().theWorld.

 

Thank you so much! :) Is there some method in there I can use to get the dimension value for comparison? Can't find anything in World or WorldInfo

Posted

I got the idea from another thread I'd looked at that I needed to confirm that only players in the correct dimension were receiving the packet, otherwise depending on what is done (e.g. spawnParticle) it could cause a crash.

 

Posted

Oh of course. It's all working fine now, fixed the sendToAllAround so that it's being given the ID properly and not statically.

 

In case anyone else comes across this thread for a similar issue:

 

On a given right-click event, creates a smoke particle effect at the location for all clients in a 100 block radius.

 

Using sendToAllAround

IMessage msg = new SimplePacket.SimpleMessage(x, y, z);
PacketHandler.net.sendToAllAround(msg, new NetworkRegistry.TargetPoint(world.provider.dimensionId, x, y, z, 100.0D));

ClientProxy

    public void SimpleMIn(SimplePacket.SimpleMessage message) {
    	Minecraft.getMinecraft().theWorld.spawnParticle("largesmoke", message.getX(), message.getY(), message.getZ(), 0, 0 ,0);
}

 

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.