Jump to content

Recommended Posts

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?

Posted

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
}

Posted

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

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