Jump to content

[1.13.2] Help with AI task for pig


Yamahari

Recommended Posts

Hi, I have this AI task

package yamahari.weeds.entities.ai;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.event.ForgeEventFactory;
import yamahari.weeds.blocks.BlockDryFarmland;
import yamahari.weeds.lists.BlockList;

public class EntityAIMakeSoil extends EntityAIBase {
    private final EntityLiving soilMakerEntity;

    private final World entityWorld;

    private int makingSoilTimer;

    public EntityAIMakeSoil(EntityLiving soilMakerEntity) {
        this.soilMakerEntity = soilMakerEntity;
        this.entityWorld = soilMakerEntity.world;
        this.setMutexBits(7);
    }

    @Override
    public boolean shouldExecute() {
        if(this.soilMakerEntity.getRNG().nextInt(this.soilMakerEntity.isChild() ? 100 : 200) == 0) {
            IBlockState blockState = this.entityWorld.getBlockState(this.soilMakerEntity.getPosition().down());
            Block block = blockState.getBlock();
            return block == Blocks.GRASS_BLOCK
                    || block == Blocks.DIRT
                    || block == BlockList.dry_farmland
                    || block == Blocks.COARSE_DIRT;
        }
        return false;
    }

    @Override
    public void startExecuting() {
        this.makingSoilTimer = 40;
        this.soilMakerEntity.getNavigator().clearPath();
    }

    @Override
    public void resetTask() {
        this.makingSoilTimer = 0;
    }

    @Override
    public boolean shouldContinueExecuting() {
        return this.makingSoilTimer > 0;
    }

    public int getMakingSoilTimer() {
        return this.makingSoilTimer;
    }

    @Override
    public void tick() {
        this.makingSoilTimer = Math.max(0, this.makingSoilTimer - 1);
        if(this.makingSoilTimer == 4) {
            BlockPos pos = new BlockPos(this.soilMakerEntity).down();
            IBlockState blockState = this.entityWorld.getBlockState(pos);

            if(ForgeEventFactory.getMobGriefingEvent(this.entityWorld, this.soilMakerEntity)) {
                if(blockState.getBlock() == Blocks.GRASS_BLOCK || blockState.getBlock() == Blocks.DIRT) {
                    this.entityWorld.playEvent(Constants.WorldEvents.BREAK_BLOCK_EFFECTS, pos, Block.getStateId(blockState.getBlock().getDefaultState()));
                    this.entityWorld.setBlockState(pos, BlockList.dry_farmland.getDefaultState(), Constants.BlockFlags.NOTIFY_LISTENERS);
                }
                else if(blockState.getBlock() == Blocks.COARSE_DIRT) {
                    this.entityWorld.playEvent(Constants.WorldEvents.BREAK_BLOCK_EFFECTS, pos, Block.getStateId(blockState.getBlock().getDefaultState()));
                    this.entityWorld.setBlockState(pos, Blocks.DIRT.getDefaultState(), Constants.BlockFlags.NOTIFY_LISTENERS);
                }
                else if(blockState.getBlock() == BlockList.dry_farmland && !blockState.get(((BlockDryFarmland) blockState.getBlock()).getNutrition())) {
                    this.entityWorld.playEvent(Constants.WorldEvents.BREAK_BLOCK_EFFECTS, pos, Block.getStateId(blockState.getBlock().getDefaultState()));
                    this.entityWorld.setBlockState(pos, blockState.with(((BlockDryFarmland) blockState.getBlock()).getNutrition(), Boolean.valueOf(true)), Constants.BlockFlags.NOTIFY_LISTENERS);
                }
            }
        }
    }
}

that is attached to pigs when they spawn. It works like the EatGrass AI task for sheeps, but it turns dirt/grass block into my farmblock with no nutrition, and my farmblock with no nutrition into one with nutrition. Everything seems to work like I want it to but for some reason the first case

if(blockState.getBlock() == Blocks.GRASS_BLOCK || blockState.getBlock() == Blocks.DIRT) {
                    this.entityWorld.playEvent(Constants.WorldEvents.BREAK_BLOCK_EFFECTS, pos, Block.getStateId(blockState.getBlock().getDefaultState()));
                    this.entityWorld.setBlockState(pos, BlockList.dry_farmland.getDefaultState(), Constants.BlockFlags.NOTIFY_LISTENERS);
                }

is called often even though the pig is not standing on dirt, you can just hear the sound playing. It's not caused by trampling the underlying farmland, I tried that already. 

Link to comment
Share on other sites

I'd put an IDE breakpoint on the line of code that's being unexpectedly hit (this.entityWorld.playEvent....) and find out what the blockstate actually is there (and verify that the blockpos is what you expect it to be, i.e. the block the entity is standing on).

  • Like 1
Link to comment
Share on other sites

After you get the block below the entity, first check if the block above that is farmland, and if so, use that block instead.

 

Alternatively, get the blockpos one block below the entity's position with a small positive Y offset.

  • Like 1
Link to comment
Share on other sites

this seems to have done the trick

IBlockState blockState = this.entityWorld.getBlockState(new BlockPos(this.soilMakerEntity.posX, Math.ceil(this.soilMakerEntity.posY), this.soilMakerEntity.posZ).down());
 BlockPos pos = new BlockPos(this.soilMakerEntity.posX, Math.ceil(this.soilMakerEntity.posY), this.soilMakerEntity.posZ).down();

thanks! 

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

    • WINNING303 DAFTAR SITUS JUDI SLOT RESMI TERPERCAYA 2024 Temukan situs judi slot resmi dan terpercaya untuk tahun 2024 di Winning303! Daftar sekarang untuk mengakses pengalaman berjudi slot yang aman dan terjamin. Dengan layanan terbaik dan reputasi yang kokoh, Winning303 adalah pilihan terbaik bagi para penggemar judi slot. Jelajahi berbagai pilihan permainan dan raih kesempatan besar untuk meraih kemenangan di tahun ini dengan Winning303 [[jumpuri:❱❱❱❱❱ DAFTAR DI SINI ❰❰❰❰❰ > https://w303.pink/orochimaru]] [[jumpuri:❱❱❱❱❱ DAFTAR DI SINI ❰❰❰❰❰ > https://w303.pink/orochimaru]]
    • SLOT PULSA TANPA POTONGAN : SLOT PULSA 5000 VIA INDOSAT IM3 TANPA POTONGAN - SLOT PULSA XL TELKOMSEL TANPA POTONGAN  KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << SITUS SLOT GACOR 88 MAXWIN X500 HARI INI TERBAIK DAN TERPERCAYA GAMPANG MENANG Dunia Game gacor terus bertambah besar seiring berjalannya waktu, dan sudah tentu dunia itu terus berkembang serta merta bersamaan dengan berkembangnya SLOT GACOR sebagai website number #1 yang pernah ada dan tidak pernah mengecewakan sekalipun. Dengan banyaknya member yang sudah mempercayakan untuk terus menghasilkan uang bersama dengan SLOT GACOR pastinya mereka sudah percaya untuk bermain Game online bersama dengan kami dengan banyaknya testimoni yang sudah membuktikan betapa seringnya member mendapatkan jackpot besar yang bisa mencapai ratusan juta rupiah. Best online Game website that give you more money everyday, itu lah slogan yang tepat untuk bermain bersama SLOT GACOR yang sudah pasti menang setiap harinya dan bisa menjadikan bandar ini sebagai patokan untuk mendapatkan penghasilan tambahan yang efisien dan juga sesuatu hal yang fix setiap hari nya. Kami juga mendapatkan julukan sebagai Number #1 website bocor yang berarti terus memberikan member uang asli dan jackpot setiap hari nya, tidak lupa bocor itu juga bisa diartikan dalam bentuk berbagi promosi untuk para official member yang terus setia bermain bersama dengan kami. Berbagai provider Game terus bertambah banyak setiap harinya dan terus melakukan support untuk membuat para official member terus bisa menang dan terus maxwin dalam bentuk apapun maka itu langsung untuk feel free to try yourself, play with SLOT GACOR now or never !
    • BRI4D adalah pilihan tepat bagi Anda yang menginginkan pengalaman bermain slot dengan RTP tinggi dan transaksi yang akurat melalui Bank BRI. Berikut adalah beberapa alasan mengapa Anda harus memilih BRI4D: Tingkat Pengembalian (RTP) Tertinggi Kami bangga menjadi salah satu agen situs slot dengan RTP tertinggi, mencapai 99%! Ini berarti Anda memiliki peluang lebih besar untuk meraih kemenangan dalam setiap putaran permainan. Transaksi Melalui Bank BRI yang Akurat Proses deposit dan penarikan dana di BRI4D cepat, mudah, dan akurat. Kami menyediakan layanan transaksi melalui Bank BRI untuk kenyamanan Anda. Dengan begitu, Anda dapat melakukan transaksi dengan lancar dan tanpa khawatir. Beragam Pilihan Permainan BRI4D menyajikan koleksi permainan slot yang beragam dan menarik dari berbagai provider terkemuka. Mulai dari tema klasik hingga yang paling modern, Anda akan menemukan banyak pilihan permainan yang sesuai dengan selera dan preferensi Anda.  
    • SPARTA88 adalah pilihan tepat bagi Anda yang menginginkan agen situs slot terbaik dengan RTP tinggi dan transaksi yang mudah melalui Bank BNI. Berikut adalah beberapa alasan mengapa Anda harus memilih SPARTA88: Tingkat Pengembalian (RTP) Tinggi Kami bangga menjadi salah satu agen situs slot dengan RTP tertinggi, mencapai 98%! Ini berarti Anda memiliki peluang lebih besar untuk meraih kemenangan dalam setiap putaran permainan. Beragam Pilihan Permainan SPARTA88 menyajikan koleksi permainan slot yang beragam dan menarik dari berbagai provider terkemuka. Mulai dari tema klasik hingga yang paling modern, Anda akan menemukan banyak pilihan permainan yang sesuai dengan selera dan preferensi Anda. Kemudahan Bertransaksi Melalui Bank BNI Proses deposit dan penarikan dana di SPARTA88 cepat, mudah, dan aman. Kami menyediakan layanan transaksi melalui Bank BNI untuk kenyamanan Anda. Dengan begitu, Anda dapat melakukan transaksi dengan lancar tanpa perlu khawatir.
    • Slot Bank ALADIN atau Daftar slot Bank ALADIN bisa anda lakukan pada situs WINNING303 kapanpun dan dimanapun, Bermodalkan Hp saja anda bisa mengakses chat ke agen kami selama 24 jam full. keuntungan bergabung bersama kami di WINNING303 adalah anda akan mendapatkan bonus 100% khusus member baru yang bergabung dan deposit. Tidak perlu banyak, 5 ribu rupiah saja anda sudah bisa bermain bersama kami di WINNING303 . Tunggu apa lagi ? Segera Klik DAFTAR dan anda akan jadi Jutawan dalam semalam.
  • Topics

×
×
  • Create New...

Important Information

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