Jump to content

[1.12.2] Tile entity surroundings


Merthew

Recommended Posts

How would I get all the entites within a box around my custom block with a ticking tileentity? I am currently trying this:

public class TileEntityBloodCollector extends TileEntity implements ITickable{
	
	private final EobItemHandler itemHandler;
	
	int blood;
	int cap = 10000;
	
	public TileEntityBloodCollector() {
		super();
		itemHandler = new EobItemHandler(1);
	}

	@Override
	public void update() {
		BlockPos pos = this.getPos();
		boolean isChanged = false;
		int ticks = 0;
		
		ItemStack stack0 = itemHandler.getStackInSlot(0);
		
		if(!this.world.isRemote) {
			if(stack0.hasTagCompound()) {
				ItemStack stack = itemHandler.getStackInSlot(2);
				NBTTagCompound nbt;
			    if (stack.hasTagCompound()) {
			        nbt = stack.getTagCompound();
			    }
			    else {
			        nbt = new NBTTagCompound();
			    }
			    if(nbt.hasKey("blood")) {
			    	int currBlood = nbt.getInteger("blood");
				    int possBlood = nbt.getInteger("cap") - currBlood;
				    if(possBlood <= blood) {
				    	nbt.setInteger("blood", possBlood + currBlood);
				    	blood -= possBlood;
				    }
				    else {
				    	nbt.setInteger("blood", blood + currBlood);
				    	blood = 0;
				    }
				    stack.setTagCompound(nbt);
			    }
			    isChanged = true;
			}
			
			List<Entity> entitys = this.getWorld().getEntitiesWithinAABB(EntityLiving.class, new AxisAlignedBB(pos.getX()-5, pos.getY()-2, pos.getZ()-5, pos.getX()+5, pos.getY()+2, pos.getZ()+5));
			
			for(Entity e : entitys) {
				if(!(e instanceof AbstractSkeleton) && !(e instanceof EntitySlime) && !(e instanceof EntityMagmaCube) && (e instanceof EntityCreature) && ticks >= 21) {
					blood += 10;
					e.attackEntityFrom(DamageSource.MAGIC, 1);
					isChanged = true;
					ticks = 0;
				}
			}
			ticks ++;
		}
		
		
		
		if(isChanged) {
			this.updateTile();
		}
		
	}
	
	@Override
	public void readFromNBT(NBTTagCompound nbtTagCompound)
	{
		super.readFromNBT(nbtTagCompound);
		itemHandler.deserializeNBT(nbtTagCompound.getCompoundTag("ItemHandler"));
		this.cap = nbtTagCompound.getInteger("cap");
		this.blood = nbtTagCompound.getInteger("blood");

	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound nbtTagCompound)
	{
		super.writeToNBT(nbtTagCompound);
		nbtTagCompound.setTag("ItemHandler", itemHandler.serializeNBT());
		nbtTagCompound.setDouble("cap", cap);
		nbtTagCompound.setDouble("blood", blood);
		
		return nbtTagCompound;
	}
	
	void updateTile(){
		world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);
		world.scheduleBlockUpdate(pos,this.getBlockType(),0,0);
		markDirty();
	}
	
	public EobItemHandler getItemHandler()
	{
		return itemHandler;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing)
	{
		if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
		{
			return (T) itemHandler;
		}

		return super.getCapability(capability, facing);
	}

	@Override
	public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing)
	{
		return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket()
	{
		return new SPacketUpdateTileEntity(pos, 0, getUpdateTag());
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
	{
		readFromNBT(pkt.getNbtCompound());
	}

	@Override
	public NBTTagCompound getUpdateTag()
	{
		return this.writeToNBT(new NBTTagCompound());
	}

 

Code: https://github.com/Merthew/Empire-Of-Blood/

Currently working on: BlockBloodCollector

The seven became one and the one became two.

Link to comment
Share on other sites

And your problem is..?

 

What you're already doing is correct.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

10 minutes ago, Merthew said:

ticks >= 21

This will never be true because ticks is a local variable assigned to 0:

10 minutes ago, Merthew said:

int ticks = 0;

 

World#getEntitiesWithinAABB is the perfect way to get all entities of a given type in a box around some point. The only thing I could advice is to use EntityCreature as the base class instead of EntityLiving since you are already checking if the entity is an EntityCreature, put the other conditions into the predicate of World#getEntitiesWithinAABB and invoke the method when this condition is satisfied:

10 minutes ago, Merthew said:

ticks >= 21

Otherwise you are just doing extra work that is not necessary.

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.