American2050 Posted August 22, 2015 Share Posted August 22, 2015 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? Quote Link to comment Share on other sites More sharing options...
coolAlias Posted August 22, 2015 Share Posted August 22, 2015 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 } Quote http://i.imgur.com/NdrFdld.png[/img] Link to comment Share on other sites More sharing options...
American2050 Posted August 23, 2015 Author Share Posted August 23, 2015 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. Quote Link to comment Share on other sites More sharing options...
American2050 Posted August 23, 2015 Author Share Posted August 23, 2015 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.