Jump to content

[1.7.10]Getting world in packethandler.


FLUFFY2

Recommended Posts

Hello,

I made a working SimpleNetworkWrapper, but the only problem is i can only get the player from MessageContext.

I tryed useing .worldObj but thats not work in client. What is understandable.

I can't use FMLClientHandler to get the world because it will crash the server, but thats working in client.

Can someone help me out?

 

Some info:

The packet is created client side and send to server.

I also have a keyhandler what sends a packet to the server and changes the player item, what is workning both side.

 

Thanks for helping!

 

Link to comment
Share on other sites

Hi

 

Not sure if I've understood you correctly;

 

Perhaps:

Server side

MessageContext ctx ->

ctx.getServerHandler().playerEntity.worldObj;

 

Client side

Minecraft.getMinecraft().theWorld ?

 

If your code is causing problems because the client objects don't exist on the server side, you could consider putting the side-dependent handler code into your proxies, eg along the lines of this

 

http://www.minecraftforge.net/forum/index.php/topic,20383.msg102861.html#msg102861

 

-TGG

Link to comment
Share on other sites

.wordObj somehow dosen't work on client side! It refuses to do what i want.

That wierd because in Netty i can do that and its worked.

Minecraft.getMinecraft().theWorld is work, but it will crash the server because its not SERVER side.

 

I try what GreyGhost said!

Link to comment
Share on other sites

So i tryed what GrayGhost said and its not working.

The problem  with that im sending the packet to the SERVER so i cant detect the CLIENT side.

 

For diesieben, if ctx.getServerHandler().playerEntity.worldObj should work for both side, that it why not?

It a bug or what?

 

Thanks for helping guys i realy appreciate it!

Link to comment
Share on other sites

Hi

 

So i tryed what GrayGhost said and its not working.

What doesn't work?  Show your code?

 

The problem  with that im sending the packet to the SERVER so i cant detect the CLIENT side.

If you are receiving the packet on the server, why do you need to detect anything on the client side?  If your packet only ever arrives on the server side, just forget about the client side totally and use ctx.getServerHandler().playerEntity.worldObj?

 

For diesieben, if ctx.getServerHandler().playerEntity.worldObj should work for both side, that it why not?

I really don't think that will work for both sides because if I understand the code correctly getServerHandler() only works on the server side.

 

From a recent post it looks like there might be a getHandler() now too (not in my version of Forge, but that is quite old now).

http://www.minecraftforge.net/forum/index.php/topic,20689.msg104534.html#msg104534

So I'm guessing ctx.getHandler().playerEntity.worldObj ?

 

-TGG

Link to comment
Share on other sites

Le me explain it!

I have a client tickhandler what creates and send data to the server via pakets(messages).

I used Netty in 1.7.2 and it was worked perfectly, but pakets are leaked what causes memory leakings.

I heard about SimpleNetworkWrapper so i updated to it.

Now its stopped working.

 

The pakets has been sended to the server, but it has no effect on the client.

Thats because i can't get the WORLD for both SERVER side and CLIENT side.

I can get it for client or server only.

.worldObj is working but for server only.

I tryed what GreyGhost said, to check for the sides and get the world for client or server.

The only problem its not detecting the client side because i sent the paket to the server.

I set the reciever side to server.

So i can't get it working to detect the client side.

 

Here is my code:

Registration(preInit):
snw.registerMessage(Light1MessageHandler.class, Light1MessageHandler.class, 3, Side.SERVER);

Packet send:
Amnesia.snw.sendToServer(new Light1MessageHandler(playerPosX, playerPosY, playerPosZ, lastPlayerX, lastPlayerY, lastPlayerZ));

The message handler:

[spoiler]
package com.fluffy.amnesia.pakets;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;

import com.fluffy.amnesia.Amnesia;

import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import cpw.mods.fml.relauncher.Side;

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

int x;
int y;
int z;
int oldX;
int oldY;
int oldZ;

public Light1MessageHandler() 
{

}

public Light1MessageHandler(int x, int y, int z, int oldX, int oldY, int oldZ) 
{
	this.x = x;
	this.y = y;
	this.z = z;
	this.oldX = oldX;
	this.oldY = oldY;
	this.oldZ = oldZ;
}

@Override
public void toBytes(ByteBuf buf) { 
	buf.writeInt(this.x);
	buf.writeInt(this.y);
	buf.writeInt(this.z);
	buf.writeInt(this.oldX);
	buf.writeInt(this.oldY);
	buf.writeInt(this.oldZ);
}

@Override
public void fromBytes(ByteBuf buf) { 
	this.x = buf.readInt();
	this.y = buf.readInt();
	this.z = buf.readInt();
	this.oldX = buf.readInt();
	this.oldY = buf.readInt();
	this.oldZ = buf.readInt();
}

@Override
public IMessage onMessage(Light1MessageHandler message, MessageContext ctx) { 

	EntityPlayer player = ctx.getServerHandler().playerEntity;
	World world;
	if (ctx.side == Side.CLIENT) {
		world = FMLClientHandler.instance().getClient().theWorld;
	} else {
		world = ctx.getServerHandler().playerEntity.worldObj;
	}

	world.setLightValue(EnumSkyBlock.Block, message.x , message.y, message.z, Amnesia.instance.getLanternLightValue());

	updateLight(world, message);

	return null;
}

public void updateLight(World world, Light1MessageHandler message){

	//Middle
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y + 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y - 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y, message.z - 1);
	//Side
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y + 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y - 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y + 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y - 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y + 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y - 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y + 1, message.z - 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y - 1, message.z - 1);
	//Corner
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y + 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y - 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y + 1, message.z - 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y - 1, message.z - 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y + 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y - 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y + 1, message.z - 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y - 1, message.z - 1);
	//Middle-Side
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y, message.z - 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y, message.z - 1);
}
}
[/spoiler]

[code]

Thanks for helping!




Link to comment
Share on other sites

It only sent to the server and my problem is its not working with the server handler.

I tryed it out with that first, but its not worked.

So it has to work but its not?

I spawning the block in server but its not appearing in my client.

Im just getting confused right now.

Link to comment
Share on other sites

You are lost the line, right?

My setLightValue and updateLight code is working i tested it with getting the client world.

But client world is crahing the server so i have to use ctx.getServerHandler().playerEntity.worldObj.

It should place the block on the server world and appear on my client.

The problem is its dosen`t.

No worries im confused about this too.

Link to comment
Share on other sites

The reason the client world crashes is because the server knows nothing about any client side code. Minecraft class? Poof. Nothing. Does not exist on the server.

 

Have you tried what is in that link I posted except with world versions instead?

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

Kwibble:

The client isnt crashes! The server crashes if i use:

World wolrd = FMLClientHandler.instance().getClient.theWorld.

It know why its crashing don`tmexplain it!

But if i test it on my client what not crashes i see its working.

 

Diesieben:

You are close, but no.

 

I want to send a packet from cliemt tickhandler to the sever(it can be a differen machine).

Server recieved the packet and get the server world.

Than place down a block to the world.

Just to make it simple:

 

                                      Client side|Server side

Getting Client world:    Work|Crash(Understandable why)

Getting Server world:    Not work|Server keeps kicking out when the block should be placed.(wierd huh?)

 

I know there is no more single player now in minecraft. It opens an internal server.

So if i get the server world and place a block with that it should appear on client.

And if i open a server up and send the packet it should apperar for everyone.

I DON`T need client world because it crash te server.

 

Hope everyone can understand this. Thanks!

 

 

Link to comment
Share on other sites

-_- Umm... I was just pointing out why it wasn't working. Sometimes people don't realise simple things. And you don't seem to be a person with English as their first language. No offence.

 

Some people aren't english scholars, but I didn't just write a three hundred page report on why it didn't work.

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

Kriki, I think the main thing we're questioning is: what do you mean by "not working"?  You keep talking about the world object "not being there" but if it really wasn't there then there would be a crash.  So I think you're saying that it doesn't crash, but it also doesn't do what you expect.

 

In programming that usually means there is some mistake somewhere, even if you are sure you know what you're doing.  I've often made a simple typographical error that is very hard to catch, or a logical error that is subtle.

 

To debug an error, you need to use line-by-line tracing techniques.  I usually do this by adding console statements, but of course you can also use your IDE's debugging configuration to watch things you're interested in.  It only takes about half an hour to put in enough console statements to confirm every part of your code.

 

My other suggestion is to try something simpler first and then build back up.  Maybe just grab the world object and check other methods to confirm that those respond as expected.  This will eliminate all the possible errors in the packet handling or the message formation or in any other logic in your code.

 

Anyway, it sounds like you already know Java well, but I don't see evidence in your code that you were really tracing every step of code.  Debugging requires that.  For example, if you have code to place a block in response to packet received so you should have a statement right before the block placement that says "placing block".  If that doesn't show on the console then check to make sure all the parameters for the block placement are what you expect (are the x, y, and z values what you expect, etc) and if that doesn't work then go further up the code to what calls the block placement, like confirm that packet was actually received, and if that doesn't work confirm that packet was actually sent. 

 

I'm confident you'll find the error along the way.

 

Anyway, just saying "doesn't work" or "doesn't find world object" doesn't allow us to help you -- it obviously does find some world object if it isn't crashing with null pointer exception.  So I think you mean the block placement isn't happening as expected.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

First of all, thanks for the answer!

I keep doing this line by line checking. I can't even say how many times.

By not working i mean the .worldObj form the serverHandler() in my packet what is sent to the server is not doing what i expect.

I have a dynamiclight system, what works by the folloving():

In my CLIENT tickhandler i get the player x, y, z pos when the right item is held and send that info to the server.

If the player moves(chages x, y, z cord) it updates and sends the packet again with the new pos.

In the packethandler(SimpleMessageWrapper) i get those positions and creates a light block at the player.

By moving it gets updated.

This is what i expect it shoud do.

My problem is its not doeing anything what has any contact with the world.

None of the world.methods are working.

The wierd thing is if i get the player by: ctx.getServerHandler.playerEntity its working for both sides.

I know this because i have a keyhandler what send packet to server and changes the player item.

Tested it on client and server and both of them is working.

I expected if i put .worldObj at the and of it it will work the same.

But its not.

The client don't creates the light and the server kick me out because of some internal error.

 

If you need all my code thats part of this problem just tell me.

 

 

 

 

Link to comment
Share on other sites

Okey here you go:

TickHandler(just the important part)

[spoiler]
	WorldClient world = mc.theWorld;

	if (mc.thePlayer.inventory.getCurrentItem() != null && mc.thePlayer.inventory.getCurrentItem().getItem() == Amnesia.lanternonItem)
	{
		int playerPosX = (int) Math.floor(mc.thePlayer.posX);
		int playerPosY = (int) Math.floor(mc.thePlayer.posY);
		int playerPosZ = (int) Math.floor(mc.thePlayer.posZ);

		if (playerPosX != this.lastPlayerX || playerPosY != this.lastPlayerY || playerPosZ != this.lastPlayerZ)
		{
			Amnesia.snw.sendToServer(new Light1MessageHandler(playerPosX, playerPosY, playerPosZ, lastPlayerX, lastPlayerY, lastPlayerZ));
			this.lastPlayerX = playerPosX;
			this.lastPlayerY = playerPosY;
			this.lastPlayerZ = playerPosZ;
		}
		if(!lanternEquipped)
			this.lanternEquipped = true;

	}else if (mc.thePlayer.inventory.getCurrentItem() != null && mc.thePlayer.inventory.getCurrentItem().getItem() != Amnesia.lanternonItem || mc.thePlayer.inventory.getCurrentItem() == null){
		if(lanternEquipped){
			Amnesia.snw.sendToServer(new DarkMessageHandler(lastPlayerX, lastPlayerY, lastPlayerZ)); //NOT IMPORTANT FOR THIS PROBLEM 
			this.lastPlayerX = 0;
			this.lastPlayerY = 0;
			this.lastPlayerZ = 0;
			this.lanternEquipped = false;
		}
	}
[/spoiler]


Light1MessageHandler:

[spoiler]
package com.fluffy.amnesia.pakets;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;

import com.fluffy.amnesia.Amnesia;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;

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

int x;
int y;
int z;
int oldX;
int oldY;
int oldZ;

public Light1MessageHandler() 
{

}

public Light1MessageHandler(int x, int y, int z, int oldX, int oldY, int oldZ) 
{
	this.x = x;
	this.y = y;
	this.z = z;
	this.oldX = oldX;
	this.oldY = oldY;
	this.oldZ = oldZ;
}

@Override
public void toBytes(ByteBuf buf) { 
	buf.writeInt(this.x);
	buf.writeInt(this.y);
	buf.writeInt(this.z);
	buf.writeInt(this.oldX);
	buf.writeInt(this.oldY);
	buf.writeInt(this.oldZ);
}

@Override
public void fromBytes(ByteBuf buf) { 
	this.x = buf.readInt();
	this.y = buf.readInt();
	this.z = buf.readInt();
	this.oldX = buf.readInt();
	this.oldY = buf.readInt();
	this.oldZ = buf.readInt();
}

@Override
public IMessage onMessage(Light1MessageHandler message, MessageContext ctx) { 

	EntityPlayer player = ctx.getServerHandler().playerEntity;
	World world = ctx.getServerHandler().playerEntity.worldObj;

	world.setLightValue(EnumSkyBlock.Block, message.x , message.y, message.z, Amnesia.instance.getLanternLightValue());

	updateLight(world, message);

	return null;
}

public void updateLight(World world, Light1MessageHandler message){

	//Middle
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y + 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y - 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y, message.z - 1);
	//Side
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y + 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y - 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y + 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y - 1, message.z);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y + 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y - 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y + 1, message.z - 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x, message.y - 1, message.z - 1);
	//Corner
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y + 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y - 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y + 1, message.z - 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y - 1, message.z - 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y + 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y - 1, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y + 1, message.z - 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y - 1, message.z - 1);
	//Middle-Side
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x - 1, message.y, message.z - 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y, message.z + 1);
	world.updateLightByType(EnumSkyBlock.Block, message.x + 1, message.y, message.z - 1);
}
}
[/spoiler]


Registration(preInt):
snw = NetworkRegistry.INSTANCE.newSimpleChannel("Amnesia"); 
snw.registerMessage(Light1MessageHandler.class, Light1MessageHandler.class, 3, Side.SERVER);
[code]

Link to comment
Share on other sites

Hi

 

From following the other replies I reckon your packet handler is actually working fine.

Perhaps you could try something diagnostic in your server packet handler, eg

 

@Override
public IMessage onMessage(Light1MessageHandler message, MessageContext ctx) { 

	EntityPlayer player = ctx.getServerHandler().playerEntity;
	World world = ctx.getServerHandler().playerEntity.worldObj;

                System.out.println("onMessage:" + message.x + ", " + message.y + ", " + message.z);
                System.out.println("World time: " + World.getWorldTime();
                world.setBlock(message.x , message.y, message.z, Blocks.stone, 0, 1 + 2);
//		world.setLightValue(EnumSkyBlock.Block, message.x , message.y, message.z, Amnesia.instance.getLanternLightValue());

//		updateLight(world, message);

	return null;
}

 

-TGG

Link to comment
Share on other sites

Okey thats even wierder.

I tryed some debuging and everything went fine. Stone is placed get the right world time and pos.

What diesieben said true but not in my case.

I disabled my updateLight method so only the .setLightValue stays there.

And its not creating the light. ITS WORKED BEFORE!

I not want to create a ligh block and update that because it can replace blocks.

It has to be EnumSkyBlock.Block.

So its not the .worldObj, but my .setLightValue.

I have to figure a way around that.

Thanks for the help everyone!

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