Jump to content

Recommended Posts

Posted

I have a IPlayerTracker that is supposed to send a packet to the server when a player logs in.  It sends the packets, but they are never recieved!

 

Here is my code:

 

Player Tracker:

 

 

package Technomage3.both.Common;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import cpw.mods.fml.common.IPlayerTracker;
import cpw.mods.fml.common.network.PacketDispatcher;
import Technomage3.both.ModInfo;
import Technomage3.both.Blocks.tileentities.TileEntityMagicPathway;
import Technomage3.both.Network.TMPacketHandeler;
import Technomage3.both.Network.TMPackets;
import am2.PlayerTracker;

public class TMPlayerTracker implements IPlayerTracker {

@Override
public void onPlayerLogin(EntityPlayer player) {

	System.out.println("TechMagic Player login");
	NBTTagCompound toSend = new NBTTagCompound();
	PacketDispatcher.sendPacketToServer(TMPacketHandeler.getNBTPacket(toSend, ModInfo.channel, TMPackets.magicPathwayRead));



}

@Override
public void onPlayerLogout(EntityPlayer player) {

}

@Override
public void onPlayerChangedDimension(EntityPlayer player) {

}

@Override
public void onPlayerRespawn(EntityPlayer player) {

}

}

 

 

 

Packet Handeler:

 

 

package Technomage3.both.Network;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;

import Technomage3.Core.Utils.TN3DataReader;
import Technomage3.Core.Utils.TN3DataWriter;
import Technomage3.both.ModInfo;
import Technomage3.both.Blocks.tileentities.TileEntityMageForge;
import Technomage3.both.Blocks.tileentities.TileEntityMagicPathway;
import Technomage3.both.Interfaces.containers.ContainerMageForge;
import Technomage3.both.Items.ItemKeystone;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;

public class TMPacketHandeler implements IPacketHandler {



@Override
public void onPacketData(INetworkManager manager,
		Packet250CustomPayload packet, Player player) {

	NBTTagCompound tag = TN3DataReader.readNBTPacket(packet);

	System.out.println("Caught packet with id " + tag.getInteger("PacketID"));
	World world = Minecraft.getMinecraft().theWorld;
	switch(tag.getInteger("PacketID")){

	case TMPackets.magicPathwayRead:


		int[] cords = tag.getIntArray("Cords");
		System.out.println("Caught Magic pathway packet with cords (" + cords[0] + ", " + cords[1] + ", " + cords[2] + ")");
		TileEntityMagicPathway t = (TileEntityMagicPathway) world.getBlockTileEntity(cords[0], cords[1], cords[2]);
		t.recieveUpdatePacket(tag);

	case TMPackets.playerLogin:
		System.out.println("TechMagic Player login packet recieved");
		for(Object o : world.loadedTileEntityList)
			if(o instanceof TileEntityMagicPathway)
				((TileEntityMagicPathway) o).sendUpdatePacket();

	}



}

public static Packet250CustomPayload getNBTPacket(NBTTagCompound nbt, String channel, int id){
	return TN3DataWriter.getNBTPacket(id, channel, nbt);
}


}

 

 

 

Main mod class:

 

 

package Technomage3.both;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import Technomage3.both.AncientSpells.AncientSpellStaff;
import Technomage3.both.AncientSpells.AncientSpells;
import Technomage3.both.Blocks.TMBlocks;
import Technomage3.both.Blocks.tileentities.TileEntityMageForge;
import Technomage3.both.Blocks.tileentities.TileEntityMagicPathway;
import Technomage3.both.Blocks.tileentities.recipies.MageForgeRecipie;
import Technomage3.both.Common.ChatHandeler;
import Technomage3.both.Common.TMEventHandeler;
import Technomage3.both.Common.TMPlayerTracker;
import Technomage3.both.Config.TMConfigHandeler;
import Technomage3.both.Entities.TMEntities;
import Technomage3.both.Interfaces.TMGUIHandeler;
import Technomage3.both.Items.TMItems;
import Technomage3.both.Network.TMPacketHandeler;
import Technomage3.both.Proxies.TMCommonProxy;
import Technomage3.both.Spell.SpellComponents;
import Technomage3.both.Spell.SpellIconRegisterer;
import Technomage3.both.World.TMGenerationHandeler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;


@Mod(modid = ModInfo.id, name = ModInfo.name, version = ModInfo.version)
@NetworkMod(channels = {ModInfo.channel}, clientSideRequired = true, serverSideRequired = true, packetHandler = TMPacketHandeler.class)
public class TechnologicalMagic {

@SidedProxy(clientSide = "Technomage3.both.Proxies.TMClientProxy", serverSide = "Technomage3.both.Proxies.TMCommonProxy")
public static TMCommonProxy proxy;

public boolean hasPerformedSucesfulLoad = false;

@Instance(ModInfo.id)
public static TechnologicalMagic instance;


@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
	TMConfigHandeler.init(event.getSuggestedConfigurationFile());

	if(event.getSide() == Side.CLIENT)
		MinecraftForge.EVENT_BUS.register(new SpellIconRegisterer());

	proxy.initRenderers();
	proxy.initSounds();

	NetworkRegistry.instance().registerChannel(new TMPacketHandeler(), ModInfo.channel);

	TMItems.init();
	TMBlocks.blockInit();
	TMEntities.init();
	AncientSpells.init();
	//SpellComponents.register();


}

@EventHandler
public void load(FMLInitializationEvent event)
{

	addCraftingRecipies();

	GameRegistry.registerPlayerTracker(new TMPlayerTracker());
	GameRegistry.registerWorldGenerator(new TMGenerationHandeler());

	MinecraftForge.EVENT_BUS.register(new ChatHandeler());
	MinecraftForge.EVENT_BUS.register(new TMEventHandeler());

	new TMGUIHandeler();

}

@EventHandler
public void postInit(FMLPostInitializationEvent event)
{

}


private static void addCraftingRecipies() {

	addMageForgeRecipies();

	GameRegistry.addRecipe(AncientSpellStaff.getStaff(AncientSpells.teleport), 

			" A ",
			" B ",
			" B ",
					'A', Item.diamond, 'B', Item.blazeRod



			);






}

private static void addMageForgeRecipies() {
	TileEntityMageForge.addRecipie(
			new MageForgeRecipie(
					new ItemStack(TMItems.metoroticIron), 

					new ItemStack(Item.diamond), 
					new ItemStack(TMItems.clestialCrystal), 
					new ItemStack(Item.blazeRod), 
					new ItemStack(Item.enderPearl), 

					100, 0, 
					new ItemStack(TMItems.brightSteel))
			);

}


}

 

 

 

Tile Entity:

 

 

package Technomage3.both.Blocks.tileentities;

import java.util.ArrayList;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.world.World;
import net.minecraftforge.event.Event;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.world.ChunkEvent;
import net.minecraftforge.event.world.WorldEvent;
import Technomage3.Core.Utils.TN3DataWriter;
import Technomage3.both.ModInfo;
import Technomage3.both.Items.ItemKeystone;
import Technomage3.both.Items.ItemKeystone.KeyCode;
import Technomage3.both.Items.TMItems;
import Technomage3.both.Network.TMPacketHandeler;
import Technomage3.both.Network.TMPackets;
import Technomage3.both.Power.TileEntityMagicMachine;

public class TileEntityMagicPathway extends TileEntityMagicMachine implements IInventory {

private String name = "none";

public ItemStack[] keystone = new ItemStack[1];

public void onUpdate(){

}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	super.writeToNBT(nbt);

	System.out.println(worldObj.isRemote ? "Client writeToNBT" : "Server writeToNBT");
    
    nbt.setString("Name", name);

	NBTTagList items = new NBTTagList();

	for (int i = 0; i < getSizeInventory(); i++) {
		ItemStack stack = getStackInSlot(i);

		if (stack != null) {
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("Slot", (byte)i);
			stack.writeToNBT(item);
			items.appendTag(item);
		}
	}

	nbt.setTag("Items", items);
}

@Override
public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);
	name = compound.getString("Name");
	NBTTagList items = compound.getTagList("Items");

	for (int i = 0; i < items.tagCount(); i++) {
		NBTTagCompound item = (NBTTagCompound)items.tagAt(i);
		int slot = item.getByte("Slot");

		if (slot >= 0 && slot < getSizeInventory()) {
			setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
		}
	}
	onInventoryChanged();


}

public ArrayList<TileEntityMagicPathway> getOtherPoints(KeyCode code){

	ArrayList<TileEntityMagicPathway> list = new ArrayList<TileEntityMagicPathway>();

	for(Object o : worldObj.loadedTileEntityList){
		if(o instanceof TileEntityMagicPathway){
			TileEntityMagicPathway t = (TileEntityMagicPathway) o;
			if(t.getCode().isMatch(code) && !list.contains(t) && !t.equals(this))
				list.add(t);
		}
	}


	return list;

}



public KeyCode getCode() {
	return keystone[0] == null ? KeyCode.blank : KeyCode.getCode(keystone[0]);
}

@Override
public int getSizeInventory() {
	return 1;
}

@Override
public ItemStack getStackInSlot(int i) {
	return keystone[i];
}

@Override
public ItemStack decrStackSize(int i, int j) {

	ItemStack itemstack = getStackInSlot(i);

	if (itemstack != null) {
		if (itemstack.stackSize <= j) {
			setInventorySlotContents(i, null);
			onInventoryChanged();
		}else{
			itemstack = itemstack.splitStack(j);
			onInventoryChanged();
		}
	}

	return itemstack;

}

@Override
public ItemStack getStackInSlotOnClosing(int i) {

	ItemStack s = getStackInSlot(i);

	setInventorySlotContents(i, null);
	onInventoryChanged();

	return s;
}

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {

	if(itemstack != null && itemstack.stackSize > getInventoryStackLimit())
		itemstack.stackSize = getInventoryStackLimit();

	if(this.isItemValidForSlot(0, itemstack))
		keystone[i] = itemstack;

	onInventoryChanged();

}

@Override
public String getInvName() {
	return name;
}

@Override
public boolean isInvNameLocalized() {
	return false;
}

@Override
public int getInventoryStackLimit() {
	return 1;
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
	return entityplayer.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) <= 64;
}

@Override
public void openChest() {

}

@Override
public void closeChest() {

}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	return itemstack == null || itemstack.itemID == TMItems.keyStone.itemID;
}

public String getName() {
	return name;
}

@Override
public boolean equals(Object o){

	if(!(o instanceof TileEntityMagicPathway))
		return false;

	TileEntityMagicPathway t = (TileEntityMagicPathway) o;

	return t.xCoord == xCoord && t.yCoord == yCoord && t.zCoord == zCoord;

}

public static boolean shouldPlayerSeeKeystone(EntityPlayer player, TileEntityMagicPathway tile){
	return KeyCode.getHeldCode(player).equals(tile.getCode());
}

public void setCode(KeyCode keyCode) {

	if(!keyCode.equals(KeyCode.blank)){
		if(keyCode.equals(getCode()))
			return;

		ItemStack i = new ItemStack(TMItems.keyStone);

		KeyCode.setCode(i, keyCode);

		this.setInventorySlotContents(0, i);
	} else
		this.setInventorySlotContents(0, null);
	onInventoryChanged();

}

public void setCodeN(KeyCode keyCode) {

	if(!keyCode.equals(KeyCode.blank)){
		if(keyCode.equals(getCode()))
			return;

		ItemStack i = new ItemStack(TMItems.keyStone);

		KeyCode.setCode(i, keyCode);

		keystone[0] = i;
	} else
		keystone[0] = null;

}


public void recieveUpdatePacket(NBTTagCompound tag) {
	System.out.println("Recived Update packet!");
	if(FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT){
		name = tag.getString("Name");
		keystone[0] = ItemStack.loadItemStackFromNBT(tag.getCompoundTag("KeyStack"));
	}

}


public void sendUpdatePacket() {
	if(FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER){
		System.out.println("Sent Update packet!");
		NBTTagCompound toSend = new NBTTagCompound();
		NBTTagCompound stack = new NBTTagCompound();
		if(keystone[0] != null)
			keystone[0].writeToNBT(stack);
		toSend.setCompoundTag("KeyStack", stack);
		toSend.setString("Name", name);
		toSend.setIntArray("Cords", new int[] {xCoord, yCoord, zCoord});
		PacketDispatcher.sendPacketToAllPlayers(TMPacketHandeler.getNBTPacket(toSend, ModInfo.channel, TMPackets.magicPathwayRead));
	}
}

public void onInventoryChanged()
{
    super.onInventoryChanged();
    
    if(FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
    	sendUpdatePacket();
    
    /*
    if(!worldObj.isRemote)
    	for(int i = 0 ; i < 3 ; i++)
    		worldObj.addBlockEvent(xCoord, yCoord, zCoord, worldObj.getBlockId(xCoord, yCoord, zCoord), 10 + i, getCode().getCode(i));
	*/
}
/*
@SideOnly(Side.CLIENT)
public boolean receiveClientEvent(int id, int val)
{
	if(worldObj.isRemote){
		if(id - 10 < 3 && id - 10 >= 0){
			setCodeN(getCode().setCode(val, id - 10));
			return true;
		} else
			return false;
	}
	return false;
}
*/
    /*
private static ArrayList<TileEntityMagicPathway> tileList = new ArrayList<TileEntityMagicPathway>();
private static boolean hasList = false;

public static void writeToTileList(TileEntityMagicPathway tile, KeyCode path){

	tileList = tile.getOtherPoints(path);
	hasList = true;

}

public static void clearTileList(){
	tileList.clear();
	hasList = false;
}

public static ArrayList<TileEntityMagicPathway> getTileList(){
	if(hasList){
		return tileList;
	} else
		throw new IllegalStateException("You must write a TileList first!");
}
*/
}

 

 

Posted

Thanks, it works, but now Minecraft.getMinecraft().theWorld is null!  I nee access to the world, so I can update my tile entities' data.  Is there a way to get the world other than Minecraft.getMinecraft().theWorld?

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.