Posted June 23, 201411 yr I have a monster and i want to detect if its in a specified range to player. There is any way to do it? I tryed that, but its only see the closest entity and all what in the list: List<Entity> entities = mc.theWorld.getEntitiesWithinAABBExcludingEntity(mc.thePlayer, mc.thePlayer.boundingBox.expand(19.0D, 19.0D, 19.0D)); Entity entity; if(entities.iterator().hasNext()){ entity = (Entity)entities.iterator().next(); if(entity instanceof EntityGrunt){ if(numTicks % 20L == 0L && sanity != 0) sanity -= 1; if(terrorcounter != 0){ terrorcounter--; } else if(terrorcounter == 0){ mc.theWorld.playSound(X, Y, Z, "fluffy:Terror", 999.0F, 1.0F, false); terrorcounter = 80; } if(chase == false){ try{ if(fear == false){ Minecraft.getMinecraft().entityRenderer.theShaderGroup = new ShaderGroup(Minecraft.getMinecraft().getResourceManager(), Minecraft.getMinecraft().getFramebuffer(), new ResourceLocation("shaders/post/deconverge.json")); Minecraft.getMinecraft().entityRenderer.theShaderGroup.createBindFramebuffers(Minecraft.getMinecraft().displayWidth, Minecraft.getMinecraft().displayHeight); } mc.theWorld.playSound(X, Y, Z, "fluffy:music.AttackGrunt", 999.0F, 1.0F, false); this.chase = true; } catch(IOException ioexception) {} } }else{ if(chase){ mc.entityRenderer.theShaderGroup = null; this.chase = false; terrorcounter = 0; mc.gameSettings.setSoundLevel(SoundCategory.RECORDS, 0.0F); mc.gameSettings.setSoundLevel(SoundCategory.RECORDS, sound); } } }else{ if(chase){ mc.entityRenderer.theShaderGroup = null; this.chase = false; terrorcounter = 0; mc.gameSettings.setSoundLevel(SoundCategory.RECORDS, 0.0F); mc.gameSettings.setSoundLevel(SoundCategory.RECORDS, sound); } } I can't get an entity out of the list coz entities.contain() not work. Thanks for helping!
June 23, 201411 yr That's not how iterator works. Please go learn how and then see if you can figure it out. We all stuff up sometimes... But I seem to be at the bottom of that pot.
June 23, 201411 yr Author Okey i did what you said and come up with this: while(entities.iterator().hasNext()){ if(entities.contains(EntityGrunt.class){ //NOT WORK mc.thePlayer.addChatMessage(new ChatComponentText("ON")); }else{ mc.thePlayer.addChatMessage(new ChatComponentText("OFF")); } break; } But i can't get the entity from the list. Only OFF text showing that means i screw up the "contains" code.
June 23, 201411 yr I missed it before, but no wonder your contains call isn't working. Your checking to see if a list of entities (not classes) contains a class. What you want to do is get the iterators current value with entities.iterator().next(). Then you would check with if (entityInstance instanceof EntityGrunt) { // your code here } We all stuff up sometimes... But I seem to be at the bottom of that pot.
June 23, 201411 yr Author Sorry but i have another problem with this. If the entity instanceof EntityGrunt then: Turn ON If the list is empty then: Turn OFF How can i make NOT instanceof? If i use else then its sometimes turns off coz its checking for the closest entity and its can be an item or xp or anything. I need notinstanceof somehow but i cant do that.
June 23, 201411 yr Sorry but i have another problem with this. If the entity instanceof EntityGrunt then: Turn ON If the list is empty then: Turn OFF How can i make NOT instanceof? If i use else then its sometimes turns off coz its checking for the closest entity and its can be an item or xp or anything. I need notinstanceof somehow but i cant do that. ! ... just put this in the if statement: if (!(entityInstance instanceof EntityGrunt))
June 23, 201411 yr Author Thank you guys, but what i was looking is too simple and figured out by my own. I use now: (its the same but with a mobselector at the end.) List<Entity> entities = mc.theWorld.getEntitiesWithinAABBExcludingEntity(mc.thePlayer, mc.thePlayer.boundingBox.expand(19.0D, 19.0D, 19.0D), EntityGrunt.mobSelector); if(entities.isEmpty){ } else{ } Its that simple! Thanks anyway for helping me out!
June 25, 201411 yr Author Okey i give up! Nothing has worked so far. I made a new code: List<Entity> entities = mc.theWorld.getEntitiesWithinAABBExcludingEntity(mc.thePlayer, mc.thePlayer.boundingBox.expand(19.0D, 19.0D, 19.0D), EntityGrunt.mobSelector); Entity entity; while(entities.iterator().hasNext()){ entity = (Entity)entities.iterator().next(); if(entity instanceof EntityGrunt){ mc.thePlayer.addChatComponentMessage(new ChatComponentText("ON")); } else if(!(entity instanceof EntityGrunt)){ mc.thePlayer.addChatComponentMessage(new ChatComponentText("OFF")); } break; } if(entities.isEmpty()) { mc.thePlayer.addChatComponentMessage(new ChatComponentText("OFF")); } The problem is when another mob is closer than the mob i want to detect its simply turns off! So iterator not gona for me this i why i need list.contains. Thanks for helping!
June 25, 201411 yr Try the following List<Entity> entities = mc.theWorld.getEntitiesWithinAABBExcludingEntity(mc.thePlayer, mc.thePlayer.boundingBox.expand(19.0D, 19.0D, 19.0D), EntityGrunt.mobSelector); Entity entity; boolean foundTarget = false; Iterator<EntityLiving> i = entities.iterator(); while(i.hasNext()){ entity = (Entity)i.next(); if(entity instanceof EntityGrunt){ foundTarget = true; } //break; <-- Why did you have this? } if(foundTarget) { mc.thePlayer.addChatComponentMessage(new ChatComponentText("ON")); }else { mc.thePlayer.addChatComponentMessage(new ChatComponentText("OFF")); } I am the author of Draconic Evolution
June 25, 201411 yr sorry ether my Internet or the forum stopped working half way through my post. I am the author of Draconic Evolution
June 25, 201411 yr Author Thank you guys for helping! brandon:Its the same as mine just in a different way. But apprechiate you help! diesieben:Now its clear to me, thanks to you. BUT, how am i supposed to make it working than? My logic is failed.
June 25, 201411 yr Like diesieben07 and brandon3055 mentioned you need to put your iterator into a field before you start the while loop. So then you're using the same iterator each time the loop tests for .hasNext(). Also I think the break statement in your loop is in the wrong place. Since I think you want to stop searching as soon as you find an entity, you want to break the while loop inside the if statement where you find the entity you are searching for. Basically there are two ways the while loop will end -- if it gets to end of list so there is no hasNext() or if it finds an entity that matches what you're looking for. Maybe like this: Entity entity; boolean foundTarget = false; Iterator<EntityLiving> i = entities.iterator(); while(i.hasNext()){ entity = (Entity)i.next(); if(entity instanceof EntityGrunt){ foundTarget = true; break; } } if(foundTarget) { mc.thePlayer.addChatComponentMessage(new ChatComponentText("ON")); }else { mc.thePlayer.addChatComponentMessage(new ChatComponentText("OFF")); } Check out my tutorials here: http://jabelarminecraft.blogspot.com/
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.