Jump to content

[Solved] Send Packets when Player loads a chunk/ connects to server


Tegmen

Recommended Posts

Hi,

How can i let my TileEntity send Packets (Server => Player) everytime a Player loads the chunk that contains the TE?

 

I'm looking for a function like onChunkLoaded(), where i can put something like PacketDispatcher.sentToPlayer(myPacket, playerWhoLoadedTheChunk).

 

Does anyone have an idea how to do this?

thx

Link to comment
Share on other sites

Figured it out. Now my TE only needs to send Packets when a Player loads the containing chunk or when the TE changes (for example its orientation).

 

IChunkLoader.java

 

package teg.common;

import cpw.mods.fml.common.network.Player;

public interface IChunkLoader {

void onChunkLoaded(Player player);

}

 

 

ChunkLoad_ChunkWatchEvent.java

 

package teg.common;

import java.util.HashMap;

import cpw.mods.fml.common.network.Player;

import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.player.BonemealEvent;
import net.minecraftforge.event.world.ChunkWatchEvent;
import net.minecraft.src.TileEntity;

public class ChunkLoad_ChunkWatchEvent {

@ForgeSubscribe
public void chunkLoaded(ChunkWatchEvent event)
{
	//System.out.println(event.chunk.chunkXPos+", "+event.chunk.chunkZPos);
	HashMap teMap = (HashMap)event.player.worldObj.getChunkFromChunkCoords(event.chunk.chunkXPos, event.chunk.chunkZPos).chunkTileEntityMap;
	for(Object tileEntity: teMap.values())
	{
		if(tileEntity instanceof IChunkLoader)
		{
			System.out.println(tileEntity.getClass());
			IChunkLoader clTileEntity = (IChunkLoader) tileEntity;
			Player chunkLoadingPlayer = (Player)event.player;
			clTileEntity.onChunkLoaded(chunkLoadingPlayer);
		}
	}

}
}

 

 

and my TileEntity (which implements IChunkLoader) has a Method onChunkLoaded(Player)

 

	@Override
public void onChunkLoaded(Player player) 
{
	if(side == Side.SERVER)
        {	
        	PacketDispatcher.sendPacketToPlayer(createPacket(this), player);
        }
       
}

 

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.