Jump to content

Block detecting arrows that hit him


Xydru

Recommended Posts

Hello, I want to create a block like the target block in Minecraft 1.15 (and also 1.7.10. I want the mod for both versions to be available for, but it is okay if I only get an answer that helps me with the 1.15 version even if it would be much better if someone could help me to get a working 1.7.10 version of the mod). I am a beginner in Forge Modding, so I am struggling a lot with it.

The block should detect arrows that hit him and then tell me where they hit him, so I can calculate a number that should present by a redstone signal how close the block to the center is.

But my problem is that I do not know how the block can tell me where the arrow is.

 

I tried to copy some code of the target of the OpenBlocks Mod, but they have created classes like TileEntityTarget, that extends SyncedTileEntity that extends OpenTileEntity that extends TileEntity so I ended up with trying to edit the code and failing a lot...

Here is my code:

 

@Override
    public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity) {

        if (!world.isRemote && entity != null && entity instanceof EntityArrow) {
            if (lastEntityHit != entity.getEntityId()) {
                lastEntityHit = entity.getEntityId();
                return;
            }
            lastEntityHit = entity.getEntityId();
            onTargetHit(world, x, y, z, Vec3.createVectorHelper(entity.posX, entity.posY, entity.posZ));
        }
    }
    
    public void onTargetHit(World world, int x, int y, int z, Vec3 entityPosition) {

        if (world.isRemote) return;

        final TileEntity target = getTileEntity(world, x, y, z, TileEntity.class);
        if (target == null) return;

        Vec3 bullseye = Vec3.createVectorHelper(x, y, z);

        double distance = entityPosition.distanceTo(bullseye);

        BlockTargetBlock.setStrength(15 - Math.min(15, Math.max(0, (int)(distance * 32))));

    }
    
    @Override
    public int isProvidingWeakPower(IBlockAccess world, int x, int y, int z, int m) {
        final TileEntity tile = getTileEntity(world, x, y, z, TileEntity.class);
        return tile != null? BlockTargetBlock.getStrength() : 0;
    }

    @Override
    public int isProvidingStrongPower(IBlockAccess world, int x, int y, int z, int m) {
        return isProvidingWeakPower(world, x, y, z, m);
    }

    @Override
    public AxisAlignedBB getSelectedBoundingBoxFromPool(World world, int x, int y, int z) {
        setBlockBoundsBasedOnState(world, x, y, z);
        return super.getSelectedBoundingBoxFromPool(world, x, y, z);
    }

    @Override
    public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int x, int y, int z) {
        setBlockBoundsBasedOnState(world, x, y, z);
        return super.getCollisionBoundingBoxFromPool(world, x, y, z);
    }
    
    private TileEntity getTileEntity(IBlockAccess world, int x, int y, int z, Class<TileEntity> class1) {
        return (worldObj != null && worldObj.blockExists(x, y, z))? worldObj.getTileEntity(x, y, z) : null;
    }
    
    public static void setStrength(int strength) {
        BlockTargetBlock.strength = strength;
        tickCounter = 10;
        worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, null);
    }
    
    public static int getStrength() {
        return strength;
    }

 

I would be glad if someone could help me out. They don't have to give me the code, it would be enough if I could get a tip or a link to an article or video or something like this that could explain me how I can do it or at least give me also useful tips.

 

Greetings,

Xydru

Edited by Xydru
Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

That is 1.7.10 code.

1.7.10 is not supported on this forum.

 

If you are actually using 1.15.2, post your actual code.

Can you please help me? There are no other forums like this that deal with Minecraft Forge Modding. If I do not get help here, I do not get help anywhere.

Link to comment
Share on other sites

48 minutes ago, Xydru said:

There are no other forums like this that deal with Minecraft Forge Modding

https://www.minecraftforum.net/forums/mapping-and-modding-java-edition/minecraft-mods/modification-development

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

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