Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (โ‹ฎ) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I've created a Helicopter, Model and Renderer Works, Entity flies around etc. However I'm trying to have the entity actually drop items onto the ground.

I'm wanting the entity to drop 1 item from a list every 10 seconds.

Is that possible?

ย 

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.

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.

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.

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

@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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions โ†’ Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.