Jump to content

[solved] custom (additional) dataWatcher


SanAndreaP

Recommended Posts

I'm trying to port the ClaySoldiersMod to 1.3.1 incl. MP compatibility. For stuff which should be shown on the clay soldier (like the stick weapon) will be handled per dataWatcher. Now I ran into a problem: the dataWatcher instance from the entity supports only 32 fields and 18 fields are already occupied for vanilla stuff. But I need more than 13 fields for the little clay soldier.

So I decided to make my own dataWatcher instance, which is called "clayWatcher". The problem is that the clayWatcher fields are only updated when they spawn with this code:

 

@Override
public void writeSpawnData(ByteArrayDataOutput data) {
	ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        try
        {
            this.clayWatcher.writeWatchableObjects(dos);
        }
        catch (IOException e)
        {
            // unpossible
        }

        data.write(bos.toByteArray());
}

@Override
public void readSpawnData(ByteArrayDataInput data) {
	byte[] ByteArr = new byte[33];		
	ByteArrayInputStream bis = new ByteArrayInputStream(ByteArr);
	DataInputStream dis = new DataInputStream(bis);
	List metadata = null;
	try
        {
            metadata = DataWatcher.readWatchableObjects(dis);
        }
        catch (IOException e)
        {
            // Nope
        }

        if (metadata != null)
        {
            this.clayWatcher.updateWatchedObjectsFromList((List)metadata);
        }
}

 

I want to make that the fields are updated while playing, like the dataWatcher, but I don't know how to do that. I probably have to send my own packets, but I don't know how because I never worked with packets before.

So if someone can show me what I have to do or where I can look, I would be very thankful.

 

 

Thanks in advance.

 

 

EDIT: I will use bitflags for my booleans now to decrease the amount of needed dataWatcher objects. Also the superclasses of my entity (EntityCreature->EntityLiving->Entity) occupy only 3 (instead of 18 which I thought) objects: index 0, 1 and 8.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

How exactly are 18 taken up[ by vanilla items?

Also, That seems like a horrible idea to have so many objects synced between client and server.

You may need a re-design to lower that number. What exactly are you watching?

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Link to comment
Share on other sites

How exactly are 18 taken up[ by vanilla items?

Also, That seems like a horrible idea to have so many objects synced between client and server.

You may need a re-design to lower that number. What exactly are you watching?

 

1. ups, I saw a tutorial somewhere for dataWatchers which said that 18 are occupied by the superclasses (EntityCreature->EntityLiving->Entity). I've looked into these now and occupied are, as far as I saw, only 0, 1 and 8.

2. I would never use so many if I need those, but I looked in some classes which send the dataWatcher and it seems the dataWatcher will be send completely with all 32 possible fields, so it souldn't be that horrible. Please correct me if I'm wrong :P

3. Here's a list what I have to watch:

 

  • team color (value for texture picking)
  • if it has a stick and how many uses it has (one value)
  • if it has a blaze rod and how many uses it has (one value)
  • if the soldier has a crown
  • if the soldier has a cape
  • if a feather is deployed (when the soldier falls and has a feather)
  • if the soldier has gunpowder
  • if it has armor and how many "uses" it has(one value)
  • if the stick is sharpened
  • if it's glued to the ground and how long this effect works (one value)
  • if it has gravel
  • if it has snowballs
  • if it has firecharges
  • if it has a shield
  • if it has a bone and how many uses it has
  • if it has a studded shield
  • if it's camouflaged
  • if it has blaze powder

 

These are needed, because they will be rendered from the soldier, for example if it has a blaze rod, it renders a blaze rod in its hand.

I could put all these boolean stuff into one [short] field and access them with bitwise operators, but I just want to test stuff out before I improve the coding.

 

But thank you for opening me the eyes because of these occupied dataWatchers. For now I can use more.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

You should use Bitflags for all the booleans.

 

1 Byte = 8 bits = 00000000

 

1. bit = if it has gravel

2. bit = if it has snowballs

3  bit = if it has firecharges

4. bit = if it has a shield

5. bit = if it has a bone and how many uses it has

6. bit = if it has a studded shield

7. bit = if it's camouflaged

8. bit = if it has blaze powder

 

How to set and get single bits: http://www.vipan.com/htdocs/bitwisehelp.html

(couldn't find a better tutorial and it looks harder than it really is)

 

Just use bitwiseOperaters.

 

Link to comment
Share on other sites

You should use Bitflags for all the booleans.

 

1 Byte = 8 bits = 00000000

 

1. bit = if it has gravel

2. bit = if it has snowballs

3  bit = if it has firecharges

4. bit = if it has a shield

5. bit = if it has a bone and how many uses it has

6. bit = if it has a studded shield

7. bit = if it's camouflaged

8. bit = if it has blaze powder

 

How to set and get single bits: http://www.vipan.com/htdocs/bitwisehelp.html

(couldn't find a better tutorial and it looks harder than it really is)

 

Just use bitwiseOperaters.

 

I know how to use bitwise stuff, but thanks :)

Yes, I think I'll stick with bitflags for all my booleans.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

For example, my TF2 dispenser mod uses a byte "options"

 

1. bit = heal player

2. bit = feed player

3. bit = repair players tool

4. bit = neutralize effects

 

In my TileEntity I'm using custom packets, but in your Entity you can use datawatcher instead :)

 

 

package net.minecraft.src;

import java.util.ArrayList;

public class TileEntityTF2Dispenser extends TileEntity implements IInventory
{

private ItemStack itemStack;
public int energy;
private int counter;
private int delay;
public int maxEnergy;
private boolean spawnBeam;
private Packet230ModLoader packet;
// ModelTF2Dispenser model;

// effects, repair, feed, heal
public byte options;

public TileEntityTF2Dispenser()
{
	// model = new ModelTF2Dispenser();
	energy = 0;
	options = 7;
	counter = 0;
	spawnBeam = false;
	delay = 20;
	packet = new Packet230ModLoader();
	packet.packetType = 0;
	int[] data =
	{ xCoord, yCoord, zCoord, energy, maxEnergy, options };
	packet.dataInt = data;
	packet.modId = ModLoaderMp.getModInstance(mod_TF2Dispenser.class).getId();
}

public void updateEntity()
{

	boolean flag = isActive();
	boolean flag1 = false;
	ArrayList<EntityPlayer> players = anyPlayerInRange();
	ArrayList<EntityPlayer> beams = new ArrayList<EntityPlayer>();
	++counter;

	if (counter > delay)
	{
		if (flag)
		{
			for (EntityPlayer player : players)
			{
				spawnBeam = false;
				if (player != null && canPlayerUseDispenser(player))
				{
					if (player.isBurning())
					{
						player.extinguish();
						spawnBeam = true;
					}

					if ((options & 1) == 1)
					{
						spawnBeam = heal(player) || spawnBeam;
					}
					if ((options & 2) == 2)
					{
						spawnBeam = feed(player) || spawnBeam;
					}
					if ((options & 4) == 4)
					{
						spawnBeam = repair(player) || spawnBeam;
					}
					if ((options &  == 
					{
						spawnBeam = neutralize(player) || spawnBeam;
					}
				}
				if(spawnBeam == true){
					beams.add(player);
				}
			}
		}

		if (!isActive() && itemStack != null)
		{
			fillUp();

		}

		if (flag != isActive())
		{
			flag1 = true;
			BlockTF2Dispenser.updateBlockState(energy > 0, worldObj, xCoord, yCoord, zCoord);
		}

		if (flag1)
		{
			onInventoryChanged();
		}
		counter = 0;
	}

	if (!beams.isEmpty() && flag && counter % 4 == 0 && options > 0)
	{
		mod_TF2Dispenser.sendSpawnBeam(players, this);
	}

	// { xCoord, yCoord, zCoord, energy, maxEnergy, options };

	if (packet.dataInt[3] != energy || packet.dataInt[5] != options)
	{
		sendUpdatePacket();
	}

}

private boolean canPlayerUseDispenser(EntityPlayer player)
{
	if(mod_TF2Dispenser.teamsEnabled){

		try{
			if (!isRed() )
			{
				if(mod_TF2TeamAddon.isPlayerInTeam(player.username, false)){
					return false;
				}
			}else{
				if(mod_TF2TeamAddon.isPlayerInTeam(player.username, true)){
					return false;
				}
			}
		}catch(Exception e){

		}
	}
	return true;
}

public void sendUpdatePacket()
{
//		System.out.println("send te");
	int[] data =
	{ xCoord, yCoord, zCoord, energy, maxEnergy, options };
	packet.dataInt = data;
	mod_TF2Dispenser.sendPacketToClosestPlayers(this, packet);

}

private void fillUp()
{
	int value = 0;
	if (itemStack.itemID == Item.ingotIron.shiftedIndex)
	{
		value = 100;

	} else if (itemStack.itemID == Item.ingotGold.shiftedIndex)
	{
		value = 200;

	} else if (itemStack.itemID == Item.diamond.shiftedIndex)
	{
		value = 800;

	}
	if (value != 0)
	{
		energy = maxEnergy = value;
		--itemStack.stackSize;
		if (itemStack.stackSize <= 0)
		{
			itemStack = null;
		}
		worldObj.playSoundEffect(xCoord, yCoord, zCoord, "TF2.dispenser.fillUp", mod_TF2Dispenser.fillUpVolume, 1F);
	}

}

private boolean heal(EntityPlayer player)
{
	if (player.getHealth() < player.getMaxHealth())
	{
		player.heal(1);
		energy -= 2;
		return true;
	}
	return false;
}

private boolean feed(EntityPlayer player)
{
	if (player.getFoodStats().needFood())
	{
		player.getFoodStats().addStats(1, 0.2f);
		energy -= 2;
		return true;
	}
	return false;

}

private boolean repair(EntityPlayer player)
{
	ItemStack item = player.getCurrentEquippedItem();
	if (item != null && item.isItemDamaged())
	{
		if (item.getItemDamage() - 50 > 0)
		{
			item.damageItem(-50, player);
		} else
		{
			item.setItemDamage(0);
		}
		energy -= energy >= 15 ? 15 : energy;

		return true;
	}
	return false;

}

private boolean neutralize(EntityPlayer player)
{
	if (!player.getActivePotionEffects().isEmpty())
	{

		player.clearActivePotions();
		energy -= 10;
		return true;
	}
	return false;
}

// @Override
// public Packet getDescriptionPacket()
// {
// // System.out.println("send");
//
//
//
// return packet;
// }

public boolean isRed()
{
	return worldObj.getBlockMetadata(xCoord, yCoord, zCoord) > 7 ? true : false;
}

public void readFromNBT(NBTTagCompound par1NBTTagCompound)
{
	super.readFromNBT(par1NBTTagCompound);

	NBTTagCompound nbttagcompound = par1NBTTagCompound.getCompoundTag("Item");
	itemStack = ItemStack.loadItemStackFromNBT(nbttagcompound);

	energy = par1NBTTagCompound.getInteger("Energy");
	maxEnergy = par1NBTTagCompound.getInteger("MaxEnergy");
	options = par1NBTTagCompound.getByte("Options");

}

public void writeToNBT(NBTTagCompound par1NBTTagCompound)
{
	super.writeToNBT(par1NBTTagCompound);
	par1NBTTagCompound.setInteger("Energy", energy);
	par1NBTTagCompound.setInteger("MaxEnergy", maxEnergy);
	par1NBTTagCompound.setByte("Options", options);

	if (itemStack != null)
	{
		par1NBTTagCompound.setCompoundTag("Item", itemStack.writeToNBT(new NBTTagCompound()));

	}
}

public ArrayList<EntityPlayer> anyPlayerInRange()
{
	ArrayList<EntityPlayer> players = new ArrayList<EntityPlayer>();

	for (int i = 0; i < worldObj.playerEntities.size(); i++)
	{
		EntityPlayer entityplayer1 = (EntityPlayer) worldObj.playerEntities.get(i);
		double d1 = entityplayer1.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5);

		if ((5 < 0.0D || d1 < 15))
		{
			players.add(entityplayer1);
		}
	}

	return (ArrayList<EntityPlayer>) players.clone();
}

public int getSizeInventory()
{
	return 1;
}

public ItemStack getStackInSlot(int i)
{
	return itemStack;
}

public ItemStack decrStackSize(int i, int j)
{
	if (itemStack != null)
	{
		if (itemStack.stackSize <= j)
		{
			ItemStack itemstack = itemStack;
			itemStack = null;
			return itemstack;
		}

		ItemStack itemstack1 = itemStack.splitStack(j);

		if (itemStack.stackSize == 0)
		{
			itemStack = null;
		}

		return itemstack1;
	} else
	{
		return null;
	}
}

public void setInventorySlotContents(int i, ItemStack par2ItemStack)
{
	itemStack = par2ItemStack;

	if (par2ItemStack != null && par2ItemStack.stackSize > getInventoryStackLimit())
	{
		par2ItemStack.stackSize = getInventoryStackLimit();
	}

}

public String getInvName()
{
	return "TF2 Dispenser";
}

public int getInventoryStackLimit()
{

	return 64;
}

public int getMaxEnergie()
{
	return maxEnergy;
}

@Override
public ItemStack getStackInSlotOnClosing(int par1)
{
	if (itemStack != null)
	{
		ItemStack itemstack = itemStack;
		itemStack = null;
		return itemstack;
	} else
	{
		return null;
	}
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
	if (worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this)
	{
		return false;
	}
	return entityplayer.getDistanceSq((double) xCoord + 0.5D, (double) yCoord + 0.5D, (double) zCoord + 0.5D) <= 64D;
}

public boolean isActive()
{
	return energy > 0;
}

@Override
public void openChest()
{
	// TODO Auto-generated method stub

}

@Override
public void closeChest()
{
	// TODO Auto-generated method stub

}

}

 

Link to comment
Share on other sites

For example, my TF2 dispenser mod uses a byte "options"

 

1. bit = heal player

2. bit = feed player

3. bit = repair players tool

4. bit = neutralize effects

 

In my TileEntity I'm using custom packets, but in your Entity you can use datawatcher instead :)

 

 

package net.minecraft.src;

import java.util.ArrayList;

public class TileEntityTF2Dispenser extends TileEntity implements IInventory
{

private ItemStack itemStack;
public int energy;
private int counter;
private int delay;
public int maxEnergy;
private boolean spawnBeam;
private Packet230ModLoader packet;
// ModelTF2Dispenser model;

// effects, repair, feed, heal
public byte options;

public TileEntityTF2Dispenser()
{
	// model = new ModelTF2Dispenser();
	energy = 0;
	options = 7;
	counter = 0;
	spawnBeam = false;
	delay = 20;
	packet = new Packet230ModLoader();
	packet.packetType = 0;
	int[] data =
	{ xCoord, yCoord, zCoord, energy, maxEnergy, options };
	packet.dataInt = data;
	packet.modId = ModLoaderMp.getModInstance(mod_TF2Dispenser.class).getId();
}

public void updateEntity()
{

	boolean flag = isActive();
	boolean flag1 = false;
	ArrayList<EntityPlayer> players = anyPlayerInRange();
	ArrayList<EntityPlayer> beams = new ArrayList<EntityPlayer>();
	++counter;

	if (counter > delay)
	{
		if (flag)
		{
			for (EntityPlayer player : players)
			{
				spawnBeam = false;
				if (player != null && canPlayerUseDispenser(player))
				{
					if (player.isBurning())
					{
						player.extinguish();
						spawnBeam = true;
					}

					if ((options & 1) == 1)
					{
						spawnBeam = heal(player) || spawnBeam;
					}
					if ((options & 2) == 2)
					{
						spawnBeam = feed(player) || spawnBeam;
					}
					if ((options & 4) == 4)
					{
						spawnBeam = repair(player) || spawnBeam;
					}
					if ((options &  == 
					{
						spawnBeam = neutralize(player) || spawnBeam;
					}
				}
				if(spawnBeam == true){
					beams.add(player);
				}
			}
		}

		if (!isActive() && itemStack != null)
		{
			fillUp();

		}

		if (flag != isActive())
		{
			flag1 = true;
			BlockTF2Dispenser.updateBlockState(energy > 0, worldObj, xCoord, yCoord, zCoord);
		}

		if (flag1)
		{
			onInventoryChanged();
		}
		counter = 0;
	}

	if (!beams.isEmpty() && flag && counter % 4 == 0 && options > 0)
	{
		mod_TF2Dispenser.sendSpawnBeam(players, this);
	}

	// { xCoord, yCoord, zCoord, energy, maxEnergy, options };

	if (packet.dataInt[3] != energy || packet.dataInt[5] != options)
	{
		sendUpdatePacket();
	}

}

private boolean canPlayerUseDispenser(EntityPlayer player)
{
	if(mod_TF2Dispenser.teamsEnabled){

		try{
			if (!isRed() )
			{
				if(mod_TF2TeamAddon.isPlayerInTeam(player.username, false)){
					return false;
				}
			}else{
				if(mod_TF2TeamAddon.isPlayerInTeam(player.username, true)){
					return false;
				}
			}
		}catch(Exception e){

		}
	}
	return true;
}

public void sendUpdatePacket()
{
//		System.out.println("send te");
	int[] data =
	{ xCoord, yCoord, zCoord, energy, maxEnergy, options };
	packet.dataInt = data;
	mod_TF2Dispenser.sendPacketToClosestPlayers(this, packet);

}

private void fillUp()
{
	int value = 0;
	if (itemStack.itemID == Item.ingotIron.shiftedIndex)
	{
		value = 100;

	} else if (itemStack.itemID == Item.ingotGold.shiftedIndex)
	{
		value = 200;

	} else if (itemStack.itemID == Item.diamond.shiftedIndex)
	{
		value = 800;

	}
	if (value != 0)
	{
		energy = maxEnergy = value;
		--itemStack.stackSize;
		if (itemStack.stackSize <= 0)
		{
			itemStack = null;
		}
		worldObj.playSoundEffect(xCoord, yCoord, zCoord, "TF2.dispenser.fillUp", mod_TF2Dispenser.fillUpVolume, 1F);
	}

}

private boolean heal(EntityPlayer player)
{
	if (player.getHealth() < player.getMaxHealth())
	{
		player.heal(1);
		energy -= 2;
		return true;
	}
	return false;
}

private boolean feed(EntityPlayer player)
{
	if (player.getFoodStats().needFood())
	{
		player.getFoodStats().addStats(1, 0.2f);
		energy -= 2;
		return true;
	}
	return false;

}

private boolean repair(EntityPlayer player)
{
	ItemStack item = player.getCurrentEquippedItem();
	if (item != null && item.isItemDamaged())
	{
		if (item.getItemDamage() - 50 > 0)
		{
			item.damageItem(-50, player);
		} else
		{
			item.setItemDamage(0);
		}
		energy -= energy >= 15 ? 15 : energy;

		return true;
	}
	return false;

}

private boolean neutralize(EntityPlayer player)
{
	if (!player.getActivePotionEffects().isEmpty())
	{

		player.clearActivePotions();
		energy -= 10;
		return true;
	}
	return false;
}

// @Override
// public Packet getDescriptionPacket()
// {
// // System.out.println("send");
//
//
//
// return packet;
// }

public boolean isRed()
{
	return worldObj.getBlockMetadata(xCoord, yCoord, zCoord) > 7 ? true : false;
}

public void readFromNBT(NBTTagCompound par1NBTTagCompound)
{
	super.readFromNBT(par1NBTTagCompound);

	NBTTagCompound nbttagcompound = par1NBTTagCompound.getCompoundTag("Item");
	itemStack = ItemStack.loadItemStackFromNBT(nbttagcompound);

	energy = par1NBTTagCompound.getInteger("Energy");
	maxEnergy = par1NBTTagCompound.getInteger("MaxEnergy");
	options = par1NBTTagCompound.getByte("Options");

}

public void writeToNBT(NBTTagCompound par1NBTTagCompound)
{
	super.writeToNBT(par1NBTTagCompound);
	par1NBTTagCompound.setInteger("Energy", energy);
	par1NBTTagCompound.setInteger("MaxEnergy", maxEnergy);
	par1NBTTagCompound.setByte("Options", options);

	if (itemStack != null)
	{
		par1NBTTagCompound.setCompoundTag("Item", itemStack.writeToNBT(new NBTTagCompound()));

	}
}

public ArrayList<EntityPlayer> anyPlayerInRange()
{
	ArrayList<EntityPlayer> players = new ArrayList<EntityPlayer>();

	for (int i = 0; i < worldObj.playerEntities.size(); i++)
	{
		EntityPlayer entityplayer1 = (EntityPlayer) worldObj.playerEntities.get(i);
		double d1 = entityplayer1.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5);

		if ((5 < 0.0D || d1 < 15))
		{
			players.add(entityplayer1);
		}
	}

	return (ArrayList<EntityPlayer>) players.clone();
}

public int getSizeInventory()
{
	return 1;
}

public ItemStack getStackInSlot(int i)
{
	return itemStack;
}

public ItemStack decrStackSize(int i, int j)
{
	if (itemStack != null)
	{
		if (itemStack.stackSize <= j)
		{
			ItemStack itemstack = itemStack;
			itemStack = null;
			return itemstack;
		}

		ItemStack itemstack1 = itemStack.splitStack(j);

		if (itemStack.stackSize == 0)
		{
			itemStack = null;
		}

		return itemstack1;
	} else
	{
		return null;
	}
}

public void setInventorySlotContents(int i, ItemStack par2ItemStack)
{
	itemStack = par2ItemStack;

	if (par2ItemStack != null && par2ItemStack.stackSize > getInventoryStackLimit())
	{
		par2ItemStack.stackSize = getInventoryStackLimit();
	}

}

public String getInvName()
{
	return "TF2 Dispenser";
}

public int getInventoryStackLimit()
{

	return 64;
}

public int getMaxEnergie()
{
	return maxEnergy;
}

@Override
public ItemStack getStackInSlotOnClosing(int par1)
{
	if (itemStack != null)
	{
		ItemStack itemstack = itemStack;
		itemStack = null;
		return itemstack;
	} else
	{
		return null;
	}
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
	if (worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this)
	{
		return false;
	}
	return entityplayer.getDistanceSq((double) xCoord + 0.5D, (double) yCoord + 0.5D, (double) zCoord + 0.5D) <= 64D;
}

public boolean isActive()
{
	return energy > 0;
}

@Override
public void openChest()
{
	// TODO Auto-generated method stub

}

@Override
public void closeChest()
{
	// TODO Auto-generated method stub

}

}

 

 

Well, I think I'll stick with a short datatype, because I have some more booleans to handle than 8 ^^

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.