Skip to content

Commit

Permalink
Villagers should now be able to open doors added by other mods
Browse files Browse the repository at this point in the history
  • Loading branch information
ganymedes01 committed Sep 22, 2015
1 parent 1eacd95 commit 89d3121
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ganymedes01.etfuturum.entities.EntityRabbit;
import ganymedes01.etfuturum.entities.EntityTippedArrow;
import ganymedes01.etfuturum.entities.EntityZombieVillager;
import ganymedes01.etfuturum.entities.ai.EntityAIOpenCustomDoor;
import ganymedes01.etfuturum.inventory.ContainerEnchantment;
import ganymedes01.etfuturum.items.TippedArrow;
import ganymedes01.etfuturum.lib.GUIsID;
Expand All @@ -28,7 +29,9 @@
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.ai.EntityAIOpenDoor;
import net.minecraft.entity.ai.EntityAITargetNonTamed;
import net.minecraft.entity.ai.EntityAITasks.EntityAITaskEntry;
import net.minecraft.entity.ai.EntityAITempt;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityCreeper;
Expand Down Expand Up @@ -424,6 +427,16 @@ public void spawnEvent(EntityJoinWorldEvent event) {
EntityWolf wolf = (EntityWolf) event.entity;
if (EtFuturum.enableRabbit)
wolf.targetTasks.addTask(4, new EntityAITargetNonTamed(wolf, EntityRabbit.class, 200, false));
} else if (event.entity instanceof EntityVillager) {
EntityVillager villager = (EntityVillager) event.entity;
for (Object obj : villager.tasks.taskEntries) {
EntityAITaskEntry entry = (EntityAITaskEntry) obj;
if (entry.action instanceof EntityAIOpenDoor) {
villager.tasks.removeTask(entry.action);
villager.tasks.addTask(4, new EntityAIOpenCustomDoor(villager, true));
break;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package ganymedes01.etfuturum.entities.ai;

import net.minecraft.block.Block;
import net.minecraft.block.BlockDoor;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.pathfinding.PathEntity;
import net.minecraft.pathfinding.PathNavigate;
import net.minecraft.pathfinding.PathPoint;
import net.minecraft.util.MathHelper;

/*
* Copy paste from vanilla and adapted to work with other doors
*/
public abstract class EntityAICustomDoorInteract extends EntityAIBase {

protected EntityLiving theEntity;
protected int entityPosX;
protected int entityPosY;
protected int entityPosZ;
protected BlockDoor field_151504_e;
boolean hasStoppedDoorInteraction;
float entityPositionX;
float entityPositionZ;

public EntityAICustomDoorInteract(EntityLiving p_i1621_1_) {
theEntity = p_i1621_1_;
}

@Override
public boolean shouldExecute() {
if (!theEntity.isCollidedHorizontally)
return false;
else {
PathNavigate pathnavigate = theEntity.getNavigator();
PathEntity pathentity = pathnavigate.getPath();

if (pathentity != null && !pathentity.isFinished() && pathnavigate.getCanBreakDoors()) {
for (int i = 0; i < Math.min(pathentity.getCurrentPathIndex() + 2, pathentity.getCurrentPathLength()); ++i) {
PathPoint pathpoint = pathentity.getPathPointFromIndex(i);
entityPosX = pathpoint.xCoord;
entityPosY = pathpoint.yCoord + 1;
entityPosZ = pathpoint.zCoord;

if (theEntity.getDistanceSq(entityPosX, theEntity.posY, entityPosZ) <= 2.25D) {
field_151504_e = func_151503_a(entityPosX, entityPosY, entityPosZ);

if (field_151504_e != null)
return true;
}
}

entityPosX = MathHelper.floor_double(theEntity.posX);
entityPosY = MathHelper.floor_double(theEntity.posY + 1.0D);
entityPosZ = MathHelper.floor_double(theEntity.posZ);
field_151504_e = func_151503_a(entityPosX, entityPosY, entityPosZ);
return field_151504_e != null;
} else
return false;
}
}

@Override
public boolean continueExecuting() {
return !hasStoppedDoorInteraction;
}

@Override
public void startExecuting() {
hasStoppedDoorInteraction = false;
entityPositionX = (float) (entityPosX + 0.5F - theEntity.posX);
entityPositionZ = (float) (entityPosZ + 0.5F - theEntity.posZ);
}

@Override
public void updateTask() {
float f = (float) (entityPosX + 0.5F - theEntity.posX);
float f1 = (float) (entityPosZ + 0.5F - theEntity.posZ);
float f2 = entityPositionX * f + entityPositionZ * f1;

if (f2 < 0.0F)
hasStoppedDoorInteraction = true;
}

private BlockDoor func_151503_a(int x, int y, int z) {
Block block = theEntity.worldObj.getBlock(x, y, z);
return block instanceof BlockDoor ? (BlockDoor) block : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ganymedes01.etfuturum.entities.ai;

import net.minecraft.entity.EntityLiving;

/*
* Copy paste from vanilla and adapted to work with other doors
*/
public class EntityAIOpenCustomDoor extends EntityAICustomDoorInteract {

boolean field_75361_i;
int field_75360_j;

public EntityAIOpenCustomDoor(EntityLiving p_i1644_1_, boolean p_i1644_2_) {
super(p_i1644_1_);
theEntity = p_i1644_1_;
field_75361_i = p_i1644_2_;
}

@Override
public boolean continueExecuting() {
return field_75361_i && field_75360_j > 0 && super.continueExecuting();
}

@Override
public void startExecuting() {
field_75360_j = 20;
field_151504_e.func_150014_a(theEntity.worldObj, entityPosX, entityPosY, entityPosZ, true);
}

@Override
public void resetTask() {
if (field_75361_i)
field_151504_e.func_150014_a(theEntity.worldObj, entityPosX, entityPosY, entityPosZ, false);
}

@Override
public void updateTask() {
--field_75360_j;
super.updateTask();
}
}

0 comments on commit 89d3121

Please sign in to comment.