Jump to content

Detecting specific items near a block


Endfrost

Recommended Posts

I'm a little new to modding so don't bash me for some mistakes.

 

You see with a mod I'm writing I've made block that when right click will check for tripwire, redstone, or rails around it, and depending on the blocks around it look for certain items near it.

Only, I know how check for entities but not specific ones or even, in this case, specific items.

I know its possible because Ars Magica does exactly what I want to do, but I don't wish to poke around its files, and you should know why. The section of code I use for checking for the ring around is below.

 

 

public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer entity, int l, float m, float n, float o){

 

if(world.getBlockId(i, j, k+1) == Block.tripWire.blockID){

if(world.getBlockId(i, j, k-1) == Block.tripWire.blockID){

if(world.getBlockId(i+1, j, k) == Block.tripWire.blockID){

if(world.getBlockId(i-1, j, k) == Block.tripWire.blockID){

if(world.getBlockId(i+1, j, k+1) == Block.tripWire.blockID){

if(world.getBlockId(i-1, j, k+1) == Block.tripWire.blockID){

if(world.getBlockId(i+1, j, k-1) == Block.tripWire.blockID){

if(world.getBlockId(i-1, j, k-1) == Block.tripWire.blockID){

//Here is where I wish to put item detection

}

}

}

}

}

}

}

}

 

if(world.getBlockId(i, j, k+1) == Block.rail.blockID){

if(world.getBlockId(i, j, k-1) == Block.rail.blockID){

if(world.getBlockId(i+1, j, k) == Block.rail.blockID){

if(world.getBlockId(i-1, j, k) == Block.rail.blockID){

if(world.getBlockId(i+1, j, k+1) == Block.rail.blockID){

if(world.getBlockId(i-1, j, k+1) == Block.rail.blockID){

if(world.getBlockId(i+1, j, k-1) == Block.rail.blockID){

if(world.getBlockId(i-1, j, k-1) == Block.rail.blockID){

//Here is where I wish to put item detection

}

}

}

}

}

}

}

}

 

if(world.getBlockId(i, j, k+1) == Block.redstoneWire.blockID){

if(world.getBlockId(i, j, k-1) == Block.redstoneWire.blockID){

if(world.getBlockId(i+1, j, k) == Block.redstoneWire.blockID){

if(world.getBlockId(i-1, j, k) == Block.redstoneWire.blockID){

if(world.getBlockId(i+1, j, k+1) == Block.redstoneWire.blockID){

if(world.getBlockId(i-1, j, k+1) == Block.redstoneWire.blockID){

if(world.getBlockId(i+1, j, k-1) == Block.redstoneWire.blockID){

if(world.getBlockId(i-1, j, k-1) == Block.redstoneWire.blockID){

//Here is where I wish to put item detection

}

}

}

}

}

}

}

}

 

return true;}

 

Also, for minecraft 1.6.4 any ideas are greatly wanted.

 

Link to comment
Share on other sites

Assuming you want to take the same action if it is any of those, you might try something a bit more efficient

 

 

 

 

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entity, int l, float m, float n, float o){

 

List<Integer> target_ids = Arrays.asList(5, 20, 52);  // You need to put YOUR ID's of interest here, I just made stuff up.

 

for (int i = -1; i <= 1; i++) {

for (k = -1; k <= 1; k++) {

if (i != 0 || k != 0){

if (target_ids.contains(world.getBlockId(x + i, y z + k) {

 

// Code for entities

 

}

}

}

}

}

}

 

 

 

 

As far as looking for entities, you need to use a bounding box.  You can find some of that code in various mobs.

 

I use soemthing like this

 

 

 

 

  // Setup Search Area

  MyBB area = new MyBB(this.posX - range, this.posY - range, this.posZ - range, this.posX + range, this.posY + range, this.posZ + range);

 

  // Get list of entities in that area

  List<Entity> targets = new ArrayList<Entity>();;

  List<Entity> targets_temp = this.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, area);

 

 

 

 

With a custom BB like this.

 

 

 

 

package clandoolittle.custom_npc.Common.Utilities.BB;

 

import net.minecraft.util.AxisAlignedBB;

 

public class MyBB extends AxisAlignedBB {

 

    public MyBB(double par1, double par3, double par5, double par7, double par9, double par11) {

        super(par1, par3, par5, par7, par9, par11);

       

        this.minX = par1;

        this.minY = par3;

        this.minZ = par5;

        this.maxX = par7;

        this.maxY = par9;

        this.maxZ = par11;

       

    }

   

}

 

 

 

 

 

Good Luck.

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

Thanks on the efficiency tip.  :) The bounding box I have set that up but as far as detecting exactly what item it is, For example:

On of them needs to detect an iron helmet, how could I have  it setup to tell whether it is an iron helmet or iron sword? But you did help me still so much thanks! ;D

Link to comment
Share on other sites

You would look for EntityItem.class

 

Then you would need to cycle through each of the targets found that way and see what the item is of the EntityItem.  It has a method called GetEntityItem() which will give you the ItemStack.

 

If you haven't researched how to look through the base Minecraft code, I highly recommend it.  Both from examples on how to do things as well as being able to find stuff as I just listed above.

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

Lets assume you returned a list of EntityItem's into your Entity list.

 

 

 

 

 

// Setup Search Area

MyBB area = new MyBB(this.posX - range, this.posY - range, this.posZ - range, this.posX + range, this.posY + range, this.posZ + range);

     

// Get list of entities in that area

List<Entity> targets= this.worldObj.getEntitiesWithinAABB(EntityItem.class, area);

 

// Search through the entities

for (Entity item : targets) {

 

// Cast

EntityItem itemofinterest = (EntityItem) item;

 

// check

if (itemofinterest != null) {

 

Item thethingyouarelookingfor = itemofinterst.getEntityItem().getItem. 

 

 

}

 

}

 

 

 

 

or something close to that.  Play around with it.

Long time Bukkit & Forge Programmer

Happy to try and help

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



×
×
  • Create New...

Important Information

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