| 1 | package net.minecraft.entity.ai;
|
| 2 |
|
| 3 | import net.minecraft.entity.item.EntityItem;
|
| 4 | import net.minecraft.entity.passive.EntityVillager;
|
| 5 | import net.minecraft.init.Items;
|
| 6 | import net.minecraft.inventory.InventoryBasic;
|
| 7 | import net.minecraft.item.Item;
|
| 8 | import net.minecraft.item.ItemStack;
|
| 9 | import net.minecraft.util.MathHelper;
|
| 10 |
|
| 11 | public class EntityAIVillagerInteract extends EntityAIWatchClosest2
|
| 12 | {
|
| 13 | /** The delay before the villager throws an itemstack (in ticks) */
|
| 14 | private int interactionDelay;
|
| 15 | private EntityVillager villager;
|
| 16 |
|
| 17 | public EntityAIVillagerInteract(EntityVillager villagerIn)
|
| 18 | {
|
| 19 | super(villagerIn, EntityVillager.class, 3.0F, 0.02F);
|
| 20 | this.villager = villagerIn;
|
| 21 | }
|
| 22 |
|
| 23 | /**
|
| 24 | * Execute a one shot task or start executing a continuous task
|
| 25 | */
|
| 26 | public void startExecuting()
|
| 27 | {
|
| 28 | super.startExecuting();
|
| 29 |
|
| 30 | if (this.villager.canAbondonItems() && this.closestEntity instanceof EntityVillager && ((EntityVillager)this.closestEntity).func_175557_cr())
|
| 31 | {
|
| 32 | this.interactionDelay = 10;
|
| 33 | }
|
| 34 | else
|
| 35 | {
|
| 36 | this.interactionDelay = 0;
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | /**
|
| 41 | * Updates the task
|
| 42 | */
|
| 43 | public void updateTask()
|
| 44 | {
|
| 45 | super.updateTask();
|
| 46 |
|
| 47 | if (this.interactionDelay > 0)
|
| 48 | {
|
| 49 | --this.interactionDelay;
|
| 50 |
|
| 51 | if (this.interactionDelay == 0)
|
| 52 | {
|
| 53 | InventoryBasic inventorybasic = this.villager.getVillagerInventory();
|
| 54 |
|
| 55 | for (int i = 0; i < inventorybasic.getSizeInventory(); ++i)
|
| 56 | {
|
| 57 | ItemStack itemstack = inventorybasic.getStackInSlot(i);
|
| 58 | ItemStack itemstack1 = null;
|
| 59 |
|
| 60 | if (itemstack != null)
|
| 61 | {
|
| 62 | Item item = itemstack.getItem();
|
| 63 |
|
| 64 | if ((item == Items.bread || item == Items.potato || item == Items.carrot) && itemstack.stackSize > 3)
|
| 65 | {
|
| 66 | int l = itemstack.stackSize / 2;
|
| 67 | itemstack.stackSize -= l;
|
| 68 | itemstack1 = new ItemStack(item, l, itemstack.getMetadata());
|
| 69 | }
|
| 70 | else if (item == Items.wheat && itemstack.stackSize > 5)
|
| 71 | {
|
| 72 | int j = itemstack.stackSize / 2 / 3 * 3;
|
| 73 | int k = j / 3;
|
| 74 | itemstack.stackSize -= j;
|
| 75 | itemstack1 = new ItemStack(Items.bread, k, 0);
|
| 76 | }
|
| 77 |
|
| 78 | if (itemstack.stackSize <= 0)
|
| 79 | {
|
| 80 | inventorybasic.setInventorySlotContents(i, (ItemStack)null);
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | if (itemstack1 != null)
|
| 85 | {
|
| 86 | double d0 = this.villager.posY - 0.30000001192092896D + (double)this.villager.getEyeHeight();
|
| 87 | EntityItem entityitem = new EntityItem(this.villager.worldObj, this.villager.posX, d0, this.villager.posZ, itemstack1);
|
| 88 | float f = 0.3F;
|
| 89 | float f1 = this.villager.rotationYawHead;
|
| 90 | float f2 = this.villager.rotationPitch;
|
| 91 | entityitem.motionX = (double)(-MathHelper.sin(f1 / 180.0F * (float)Math.PI) * MathHelper.cos(f2 / 180.0F * (float)Math.PI) * f);
|
| 92 | entityitem.motionZ = (double)(MathHelper.cos(f1 / 180.0F * (float)Math.PI) * MathHelper.cos(f2 / 180.0F * (float)Math.PI) * f);
|
| 93 | entityitem.motionY = (double)(-MathHelper.sin(f2 / 180.0F * (float)Math.PI) * f + 0.1F);
|
| 94 | entityitem.setDefaultPickupDelay();
|
| 95 | this.villager.worldObj.spawnEntityInWorld(entityitem);
|
| 96 | break;
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 |
|