Posted August 22, 201510 yr 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?
August 22, 201510 yr 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 } http://i.imgur.com/NdrFdld.png[/img]
August 23, 201510 yr 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.
August 23, 201510 yr 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.