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 have a problem, I'm making some modifications on a code a friend sent me and I need to update/change the way he gives a certain items to the players connected on game.

 

The actual code is like:

 

			try {
			for (Iterator iterator = new ArrayList(world.playerEntities).iterator(); iterator.hasNext()
			{

				((EntityPlayer) iterator.next()).inventory.addItemStackToInventory(new ItemStack(ItemManager.medal));

				}
		}
		catch (Exception ex) {
			Log.logError("Failed to distribute medal!");

		}

 

The problem is that, that method will only work, if the player actually has an empty slot on his inventory.

 

What I want to do is to spawn the item at the player position, but I can't figure out how to get them. I have tried:

 

double positionX = ((EntityPlayer) iterator.next()).posX;

				System.out.println(positionX)

 

And also:

 

EntityPlayer thePlayer = ((EntityPlayer) iterator.next());

 

But it also crashes.

 

What am I doing wrong here?

You don't need to make it so complicated - just use the improved for loop to iterate over the actual list:

for (EntityPlayer player : world.playerEntities) {
  BlockPos pos = new BlockPos(player);
  // or if you want to use coordinates directly:
  double x = player.posX;
  double y = player.posY;
  double z = player.posZ;
  // now spawn in the item entity
}

  • Author

I get that Object can't be converted to EntityPlayer on the for statement.

 

I must be missing something there to make it work, can't clearly see what.

  • Author

Ohhh nvm I guess I forgot to declare the List I was creating.

 

List<EntityPlayer> playersConnected = new ArrayList<EntityPlayer>();

			playersConnected= world.playerEntities;

			for (EntityPlayer player : playersConnected) {

 

Gonna give this a try ;)

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.