Jump to content

Detect all nearby entities to player and then shoot lightning at them


Xile

Recommended Posts

Hello everyone,

ย 

I'm new to modding and java in general, and I'm currently working on an RF powered armor set. The issue is I would like to make my armor detect nearby players and mobs within a configurable radius around the player and then constantly zap them with lightning at a configurable interval. If anyone could provide insight into how I would go about doing this I would be immensely grateful.

ย 

Thank you.

Link to comment
Share on other sites

Do you mean like just vanilla lightning raining down from the sky, or zaps coming from the player to mobs? If the latter, you'll have to create your own entity.

ย 

Initially i was going to do my own zaps, but this is my first mod and im a noob so I just want to make vanilla lightning from sky hit them.

ย 

To get the players around you, entites have methods for finding other ones inside an AABB, just go through the list.

ย 

This is what I have tried so far. to no avail.

ย 

//Bounding Box Attempt :/

ย  ย  public double minX;

ย  ย  public double minY;

ย  ย  public double minZ;

ย  ย  public double maxX;

ย  ย  public double maxY;

ย  ย  public double maxZ;

ย  ย  public AxisAlignedBB axisalignedbb = AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ); {

ย  ย  ย  ย  this.minX = 0;

ย  ย  ย  ย  this.minY = 0;

ย  ย  ย  ย  this.minZ = 0;

ย  ย  ย  ย  this.maxX = 10;

ย  ย  ย  ย  this.maxY = 10;

ย  ย  ย  ย  this.maxZ = 10;

}

@Override

ย  ย  public void onArmorTick(World world, EntityPlayer player, ItemStack armor) {

ย 

ย  ย  ย  ย  List entities = world.getEntitiesWithinAABB(getClass(), axisalignedbb);

ย  ย  ย  ย  System.out.println("These Entities Are within Range!" + entities + "");

ย 

ย 

ย  ย  ย  ย  }

ย 

It doesn't detect any entities unfortunately :(

Link to comment
Share on other sites

I'm not sure, but I think the bounding box minX, maxX, etc. are absolute positions.ย  I mean you're looking between 0 and 10 but do you even know where that is in the world?ย  How are you sure there are even any entities there?ย  I think that might be way underground or something.

Check out my tutorials here:ย http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

List players = getWorldObj().getEntitiesWithinAABB(EntityPlayer.class,player.getBoundingBox().expand(3, 3, 3);

ย 

this will get all players around the player (and probably the origin player too) just change what class to find to something else or use the base class, I think EntityLiving? EntitiyLivingBase? then when spawning lightening just make sure you're not spawning one on whoever created the bounding box.

Link to comment
Share on other sites

List players = getWorldObj().getEntitiesWithinAABB(EntityPlayer.class,player.getBoundingBox().expand(3, 3, 3);

ย 

this will get all players around the player (and probably the origin player too) just change what class to find to something else or use the base class, I think EntityLiving? EntitiyLivingBase? then when spawning lightening just make sure you're not spawning one on whoever created the bounding box.

ย 

Thank you very much! However getWorldObj() says that it cannot find that method. I forgot to mention this is on 1.7.10. That may be why?

Link to comment
Share on other sites

Ok this is what I tried.

ย 

List players = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, player.getBoundingBox().expand(3, 3, 3));

ย  ย  ย  ย  System.out.println("Nearby Entities" + players + "!");

ย 

Is this correct? apologies if this is a noob question. I've been able to figure out some stuff myself but this one has been stumping me.

Link to comment
Share on other sites

yeah I didn't want to give copy paste ready code (which i'm bad for) because I was hoping you'd see the error and go "maybe it just takes a world object/instance"

ย 

but the expand(3,3,3) can be any numbers you want it to be. I just used 3's as a placeholder to show you how to use it.

ย 

PreEdit:

you might want to use an Iterator and a while loop, or some kindof loop to go through the list for each entry. you can also refactor the "players" to something like "entities" or "targets" idk whatever you want to call it. I used it for a block that would store mana, and recharge nearby mages.

ย 

but yeah looks a bit better, altho since it's an "onArmorTick" it should have a world param that you can toss in.

like par2World.getEntitiesWithin.....

and if it doesn't it should at least reference the player or entity wearing it (since zombies and skeletons can wear armor) and you can get the world object from there too.

Link to comment
Share on other sites

You guys have all been an immense help thus far!

ย 

This is what I currently have in the armortick

ย 

//Armor Effects

@Override

ย  ย  public void onArmorTick(World world, EntityPlayer player, ItemStack armor) {

ย 

ย 

ย  ย  EnergyStored = armor.stackTagCompound.getInteger("Energy");

ย  ย  if (EnergyStored < 1) NoPower = true;

ย  ย  if (KeyInputHandler.ArmorOn == true)

ย  ย  ย  ย  if (NoPower != true)

ย  ย  ย  ย  ย  ย  System.out.println("Tesla AOE Activated");

ย  ย  ย  ย  else System.out.println("Tesla Ability Deactivated");

ย 

ย 

ย  ย  //Just A Test For Charging Armor Without External Mods!

ย  ย  if (KeyInputHandler.ArmorOn == false)

ย  ย  ย  ย  armor.stackTagCompound.setInteger("Energy", (EnergyStored + 1000));

ย 

ย  ย  //PotionEffects Not sure why they dont work when all armor equipped... is weird..

ย 

ย  ย  if (player.inventory.armorItemInSlot(2) == ItemLoader.armorTeslaLegs) {

ย  ย  ย  ย  if (NoPower != true)

ย  ย  ย  ย  ย  ย  player.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 500, 4));

ย  ย  }

ย 

ย 

ย  ย  if (player.inventory.armorItemInSlot(3) == ItemLoader.armorTeslaBoots) {

ย  ย  ย  ย  if (NoPower != true)

ย  ย  ย  ย  ย  ย  player.addPotionEffect(new PotionEffect(Potion.jump.id, 500, 4));

ย  ย  }

ย  ย  while (KeyInputHandler.ArmorOn == true) {

ย  ย  ย  ย  List players = world.getEntitiesWithinAABB(EntityLivingBase.class, player.getBoundingBox().expand(7, 7, 7));

ย  ย  ย  ย  System.out.println("Nearby Entities" + players + "!");

ย  ย  }

}

ย 

currently I'm just testing random things to find out how they work but the bounding box crashes the game when my world with me wearing my armor loads up with this crash.

ย 

http://pastebin.com/KgYbz6gh

ย 

I got this crash when i was previously messing with bounding boxes as well. I feel like its something obvious I'm missing here.

Link to comment
Share on other sites

Disregard that!

ย 

After some searching I was able to get it working with this code:

ย 

List entities = player.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(player.posX - 5, player.posY - 5, player.posZ - 5, player.posX + 7, player.posY + 7, player.posZ + 7)

ย 

This correctly prints out all nearby entities in the console as well :)

ย 

Thank you everyone!

Link to comment
Share on other sites

No, they are vanilla files and you can look at them yourself (located at Referenced Libraries->forgeSrc.jar)

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

ย 

1.12 -> 1.13 primer by williewillus.

ย 

1.7.10 and older versions of Minecraft are no longer supported due to it's age!ย Update to the latest version for support.

ย 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Edit: Nevermind I fixed it! Should I just use this code after the lightning spawn to make it only fire every half second?

try {

ย  ย  TimeUnit.MILLISECONDS.sleep(500);

ย 

} catch (InterruptedException e) {

ย  ย  //Handle exception

}

ย 

ย 

ย 

Working Code:

ย 

//Attempt at Entity scan and target

ย  ย  if (KeyInputHandler.ArmorOn == true) {

ย  ย  ย  ย  System.out.println("Scanner Activated");

ย  ย  ย  ย  List entities = player.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(player.posX - 5, player.posY - 5, player.posZ - 5, player.posX + 7, player.posY + 7, player.posZ + 7));

ย  ย  ย  ย  if (entities != null && !entities.isEmpty()) {

ย  ย  ย  ย  ย  ย  Iterator iterator = entities.iterator();

ย 

ย  ย  ย  ย  ย  ย  double posX;

ย  ย  ย  ย  ย  ย  double posY;

ย  ย  ย  ย  ย  ย  double posZ;

ย  ย  ย  ย  ย  ย  EntityLivingBase ent;

ย 

ย  ย  ย  ย  ย  ย  ย  ย  while (iterator.hasNext())

ย  ย  ย  ย  ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  ย  ย  ent = (EntityLivingBase) iterator.next();

ย  ย  ย  ย  ย  ย  ย  ย  posX = ent.posX;

ย  ย  ย  ย  ย  ย  ย  ย  posY = ent.posY;

ย  ย  ย  ย  ย  ย  ย  ย  posZ = ent.posZ;

ย 

ย  ย  ย  ย  ย  ย  ย  ย  EntityLightningBolt Lightning = new EntityLightningBolt(world, posX, posY, posZ);

ย 

ย  ย  ย  ย  ย  ย  ย  ย  world.spawnEntityInWorld(Lightning);

ย  ย  ย  ย  ย  ย  }

ย  ย  ย  ย  }

ย  ย  }

ย 

}

ย 

ย 

Pre Edit:

ย 

ย 

Ok, I feel like this should work. However as soon as I activate it my whole game just freezes with no errors :/

ย 

//Attempt at Entity scan and target

ย  ย  if (KeyInputHandler.ArmorOn == true) {

ย  ย  ย  ย  System.out.println("Scanner Activated");

ย  ย  ย  ย  List entities = player.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(player.posX - 5, player.posY - 5, player.posZ - 5, player.posX + 7, player.posY + 7, player.posZ + 7));

ย  ย  ย  ย  Iterator iterator = entities.iterator();

ย  ย  ย  ย  double posX;

ย  ย  ย  ย  double posY;

ย  ย  ย  ย  double posZ;

ย  ย  ย  ย  EntityLivingBase ent;

ย  ย  ย  ย  while (iterator.hasNext());

ย  ย  ย  ย  ent = (EntityLivingBase)iterator.next();

ย  ย  ย  ย  posX = ent.posX;

ย  ย  ย  ย  posY = ent.posY;

ย  ย  ย  ย  posZ = ent.posZ;

ย 

ย  ย  ย  ย  EntityLightningBolt Lightning = new EntityLightningBolt(world, posX, posY, posZ);

ย 

ย  ย  ย  ย  world.spawnEntityInWorld(Lightning);

ย 

Link to comment
Share on other sites

Thank you very much! You have all been a great help. One thing that's been giving me issues is that for some reason the detection of the armor that is equipped doesnt work properly. Currently this is how I am detecting what armor is worn. It doesn't seem to work at all though. This is just a test.

ย 

public void onArmorTick(World world, EntityPlayer player, ItemStack armor) {

ย  ย  //0 is Boots

ย  ย  //1 is Legs

ย  ย  //2 is Chest

ย  ย  //3 is Helm

ย  ย  ItemStack armorslot = player.inventory.armorItemInSlot(0);

ย  ย  System.out.println(armorslot + " Is Equipped");

ย 

ย  ย  if (player.inventory.armorItemInSlot(0) == ItemLoader.ArmorTeslaBoots) {

ย  ย  ย  ย  System.out.println("Boots worn");

ย 

in console it prints:

ย 

1xitem.ArmorTeslaBoots@0 Is Equipped

ย 

however it does not print "Boots Worn" when equipped...

ย 

ย 

Link to comment
Share on other sites

the problem is "armorItemInSlot(0)" returns an ItemStack, you can't compare ItemStacks with Items you will always get a false. You want

armorItemInSlot(0).getItem()

.equals(item) or

== item,

or instanceof item

ย 

also you already have the itemstack from this line:

ItemStack armorslot = player.inventory.armorItemInSlot(0);

ย 

so just use

if (armorslot.getItem()ย  == ItemLoader.ArmorTeslaBoots)

{

//do stuff

}

Link to comment
Share on other sites

You should take a look at the getEntitiesWithinAABB(...) method and the other methods around it in its class. By that I mean to select the method name in your code in Eclipse, then right-click on it, then use the popup menu to view the method's declaration. You'll learn much more that way much faster than coming here to ask others to go do that reading for you.

ย 

Once there, you will see several such methods. Look at their arguments. You can exclude an entity or include only a certain type of entity. I think you'll also see some calls to underlying methods in the chunk management class. Investigating those might give you more ideas how to refine your search to get exactly those entities you want to electrocute (i.e. you probably don't want lightning shooting at paintings and lead-knots).

ย 

Good Luck!

ย 

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • OLO4D adalah pilihan terbaik bagi Anda yang mencari pengalaman bermain slot gacor dengan transaksi mudah menggunakan Bank Bukopin. Berikut adalah beberapa alasan mengapa Anda harus memilih OLO4D: Slot Gacor Terbaik Kami menyajikan koleksi slot gacor terbaik yang menawarkan kesenangan bermain dan peluang kemenangan besar. Dengan fitur-fitur unggulan dan tema-tema menarik, setiap putaran permainan akan memberikan Anda pengalaman yang tak terlupakan. Transaksi Mudah dengan Bank Bukopin Kami menyediakan layanan transaksi mudah melalui Bank Bukopin untuk kenyamanan dan keamanan Anda. Dengan proses yang cepat dan efisien, Anda dapat melakukan deposit dan penarikan dana dengan lancar dan tanpa hambatan. Pasti Jackpot OLO4D memberikan jaminan bahwa setiap pemain pasti mendapatkan jackpot. Dengan peluang kemenangan yang tinggi, setiap putaran permainan bisa menjadi peluang untuk meraih keberuntungan besar. ย 
    • Balon168: Gampang Banjir Scatter Hitam Malam Ini Balon168 telah menjadi sorotan dalam dunia perjudian online, dan ada alasan kuat mengapa pemain semakin tertarik. Salah satu permainan unggulannya, yang dikenal dengan tingkat kemenangan yang tinggi, adalah varian "Gampang Banjir Scatter Hitam Malam Ini". Mari kita selami lebih dalam mengenai fenomena ini. Balon168: Destinasi Utama bagi Pecinta Slot ย  ย  DAFTAR SEKARANG LINK VIP MAXWIN ย 
    • BALON168๐ŸŽˆ: Situs Slot Gacor Terbaik, Termewah, dan Tergacor 2024 dengan RTP 99,99% Mudah Maxwin Jika Anda pencinta judi online, mencari situs yang dapat diandalkan dan memberikan pengalaman bermain yang memuaskan tentu menjadi prioritas. Salah satu opsi terbaik yang patut dipertimbangkan adalah BALON168๐ŸŽˆ. Situs ini tidak hanya menawarkan berbagai permainan slot yang menarik, tetapi juga mempersembahkan keunggulan dan kenyamanan bagi para pemainnya. Keunggulan BALON168๐ŸŽˆ BALON168๐ŸŽˆ tidak hanya sekadar situs slot online biasa. Dengan reputasi yang solid dan terpercaya, BALON168๐ŸŽˆ telah menjadi destinasi favorit bagi para penggemar judi online di tahun 2024. Keunggulan yang ditawarkan mencakup: 1. RTP Tinggi 99,99% Salah satu hal yang membuat BALON168๐ŸŽˆ menonjol adalah tingkat pengembalian (RTP) yang luar biasa tinggi, mencapai 99,99%. Ini berarti pemain memiliki peluang besar untuk memenangkan hadiah besar setiap kali mereka memutar gulungan di slot BALON168๐ŸŽˆ. 2. Permainan Slot Gacor BALON168๐ŸŽˆ dikenal sebagai situs slot gacor terbaik di tahun 2024. "Gacor" adalah istilah yang digunakan untuk mesin slot yang sering memberikan kemenangan kepada pemainnya. Dengan koleksi permainan slot yang beragam dan sering memberikan jackpot besar, BALON168๐ŸŽˆ memastikan pengalaman bermain yang memuaskan bagi para pengunjungnya. 3. Maxwin yang Mudah Di BALON168๐ŸŽˆ, peluang maxwin tidak hanya menjadi impian belaka. Dengan fitur yang mudah dimengerti dan diakses, pemain memiliki kesempatan yang besar untuk meraih kemenangan maksimum dalam setiap permainan yang mereka mainkan. Keamanan dan Kepuasan Pemain BALON168๐ŸŽˆ mengutamakan keamanan dan kepuasan para pemainnya. Dengan sistem keamanan terkini dan perlindungan data yang canggih, para pemain dapat bermain dengan tenang tanpa khawatir tentang privasi dan keamanan mereka. Layanan pelanggan yang responsif dan ramah juga selalu siap membantu para pemain dalam setiap masalah atau pertanyaan yang mereka miliki. ย  ย  ย  โฑโฑโฑโฑโฑ DAFTAR DI SINI โฐโฐโฐโฐโฐ โฑโฑโฑโฑโฑ DAFTAR AKUN PRO โฐโฐโฐโฐโฐ โฑโฑโฑโฑโฑ DAFTAR AKUN VIPย โฐโฐโฐโฐโฐ ย  ย  ย  ย  ย  ย 
    • LadangToto2 adalah pilihan terbaik bagi Anda yang mencari pengalaman bermain slot gacor dengan transaksi mudah menggunakan Bank Mestika. Berikut adalah beberapa alasan mengapa Anda harus memilih LadangToto2: Slot Gacor Terbaik Kami menyajikan koleksi slot gacor terbaik yang menawarkan kesenangan bermain dan peluang kemenangan besar. Dengan fitur-fitur unggulan dan tema-tema menarik, setiap putaran permainan akan memberikan Anda pengalaman yang tak terlupakan. Transaksi Mudah dengan Bank Mestika Kami menyediakan layanan transaksi mudah melalui Bank Mestika untuk kenyamanan dan keamanan Anda. Dengan proses yang cepat dan efisien, Anda dapat melakukan deposit dan penarikan dana dengan lancar dan tanpa hambatan. Hadiah Hingga 100 Juta LadangToto2 memberikan kesempatan untuk meraih hadiah hingga 100 juta dalam kemenangan. Dengan jackpot dan hadiah-hadiah besar yang ditawarkan, setiap putaran permainan bisa menjadi peluang untuk meraih keberuntungan besar. ย 
    • Mengapa Memilih LadangToto? LadangToto adalah pilihan terbaik bagi Anda yang mencari pengalaman bermain slot gacor WD Maxwin dengan transaksi mudah menggunakan Bank BNI. Berikut adalah beberapa alasan mengapa Anda harus memilih LadangToto: Slot Gacor WD Maxwin Terbaik Kami menyajikan koleksi slot gacor WD Maxwin terbaik yang menawarkan kesenangan bermain dan peluang kemenangan besar. Dengan fitur-fitur unggulan dan tema-tema menarik, setiap putaran permainan akan memberikan Anda pengalaman yang tak terlupakan. Transaksi Mudah dengan Bank BNI Kami menyediakan layanan transaksi mudah melalui Bank BNI untuk kenyamanan dan keamanan Anda. Dengan proses yang cepat dan efisien, Anda dapat melakukan deposit dan penarikan dana dengan lancar dan tanpa hambatan. ย 
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.