Jump to content

Have Entity drop 1 Item from List every 10 Seconds?


ScottehBoeh

Recommended Posts

Absolutely. In your Entity class, override any of the #update related methods, e.g. #onUpdate or #onLivingUpdate, and check:

int time = 10 * 20; // 20 ticks per second times 10 seconds
if (this.ticksExisted % time == (time - 1)) { // if you check against 0, it will drop an item immediately every time the world loads
// drop item
}

Note that if you do create a List of ItemStacks, you will want to drop a COPY of the ItemStack, not the ItemStack itself, otherwise once the first one is picked up, all other drops of that stack will have a stack size of zero.

Link to comment
Share on other sites

An item every 10 seconds, by the way, is a lot of items.  If left alone, that would be 30 items littering the ground around one of these entities, constantly, due to the 5 minute entityitem lifespan.

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

Indeed, what Draco mentions would be a problem, plus your entity will be dropping items very regularly. If you want to add some randomness to it, you can store a long value of the next time the entity should drop an item, and check the current world time against that every so often:

private long nextDrop;

// in onUpdate
if (worldObj.getTotalWorldTime() > nextDrop) {
  if (nextDrop > 0) { // make sure nextDrop has been initialized
     // drop the item
  }
  // set the next drop time
  nextDrop = worldObj.getTotalWorldTime() + rand.nextInt(20 * 60 * 3) + (20 * 60 * 2); // from 2-5 minutes between drops
}

Make sure to write and read the nextDrop value to your entity's NBT data.

Link to comment
Share on other sites

You can do this and your entity will spawn items only if a player is within 5 blocks radius, which can be tweaked to your likings:

 

public class YourTileEntity extends TileEntity implements IUpdatePlayerListBox{

@Override
public void update() {
	List<Entity> PlayersInRange = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.fromBounds(this.getPos().getX() -5, this.getPos().getY() - 5, this.getPos().getZ() -5, this.getPos().getX() +6, this.getPos().getY() +6, this.getPos().getZ() +6));

	if(!PlayersInRange.isEmpty()){
		System.out.println("Player Found!");
		//Run Code here
	}

}


}

 

EDIT: This Code is for minecraft 1.8, if you are working with an older version you might have to tweak some little things and you don't need to implement IUpdatePlayerListBox

Link to comment
Share on other sites

@Cerandior - World#getClosestPlayer is FAR more optimized at finding players than #getEntitiesWithinAABB, an example of which can be seen in the post immediately prior to yours.

 

It also is far more cleaner and easier to read. The only reason i posted that code is to give him another option, which he can use in other situations.

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.