mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-17 16:55:12 +00:00
IEntityLivingBase:
-Added stopOnBlock, stopOnLiquid, and stopOnCollision options for getLookingAtEntities() IWorld: -Added stopOnBlock, stopOnLiquid, and stopOnCollision options for rayCastBlock & rayCastPos
This commit is contained in:
1 parent
4e5ed2d758
commit
d54035687c
6 files changed
+87
-64
No files matched your search
@@ -131,4 +131,11 @@ public class ScriptBlock implements IBlock {
|
||||
this.tile.writeToNBT(compound);
|
||||
return NpcAPI.Instance().getINbt(compound);
|
||||
}
|
||||
|
||||
public boolean isCollidable() {
|
||||
boolean minBoundsZero = block.getBlockBoundsMinX() == 0 && block.getBlockBoundsMinY() == 0 && block.getBlockBoundsMinZ() == 0;
|
||||
boolean maxBoundsZero = block.getBlockBoundsMaxX() == 0 && block.getBlockBoundsMaxY() == 0 && block.getBlockBoundsMaxZ() == 0;
|
||||
|
||||
return !(minBoundsZero && maxBoundsZero);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import java.util.*;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockLiquid;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.effect.EntityLightningBolt;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
@@ -315,47 +316,8 @@ public class ScriptWorld implements IWorld {
|
||||
public void removeBlock(int x, int y, int z){
|
||||
world.setBlock(x, y, z, Blocks.air);
|
||||
}
|
||||
|
||||
/**
|
||||
* starting at the start position, draw a line in the lookVector direction until a block is detected
|
||||
* @param startPos
|
||||
* @param lookVector should be a normalized direction vector
|
||||
* @param maxDistance
|
||||
* @return the first detected block but null if maxDistance is reached
|
||||
*/
|
||||
public IBlock rayCastBlock(double[] startPos, double[] lookVector, int maxDistance) {
|
||||
if (startPos.length != 3 || lookVector.length != 3) return null;
|
||||
Vec3 currentPos = Vec3.createVectorHelper(startPos[0], startPos[1], startPos[2]); int rep = 0;
|
||||
while (rep++ < maxDistance + 10) {
|
||||
currentPos = currentPos.addVector(lookVector[0], lookVector[1], lookVector[2]);
|
||||
IBlock block = getBlock((int)currentPos.xCoord, (int)currentPos.yCoord, (int)currentPos.zCoord);
|
||||
//System.out.println("Checking block at ["+(int)currentPos.xCoord+","+(int)currentPos.yCoord+","+(int)currentPos.zCoord+"]");
|
||||
if (block == null) continue;
|
||||
double distance = Math.pow(
|
||||
Math.pow(currentPos.xCoord-startPos[0],2)
|
||||
+Math.pow(currentPos.yCoord-startPos[1],2)
|
||||
+Math.pow(currentPos.zCoord-startPos[2],2)
|
||||
, 0.5);
|
||||
//System.out.println("current distance check: "+distance+" on rep "+rep);
|
||||
if (distance > maxDistance) return null;
|
||||
return block;
|
||||
}
|
||||
//System.out.println("ScriptWorld:WARNING: Repeated a ray cast to many times");
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* starting at the start position, draw a line in the lookVector direction until a block is detected
|
||||
* @param startPos
|
||||
* @param lookVector will normalize x, y, z to get a direction vector
|
||||
* @param maxDistance
|
||||
* @return the first detected block but null if maxDistance is reached
|
||||
*/
|
||||
public IBlock rayCastBlock(IPos startPos, IPos lookVector, int maxDistance) {
|
||||
return rayCastBlock(new double[] {startPos.getX(), startPos.getY(), startPos.getZ()}, lookVector.normalize(), maxDistance);
|
||||
}
|
||||
|
||||
public IPos rayCastPos(double[] startPos, double[] lookVector, int maxDistance) {
|
||||
public IPos rayCastPos(double[] startPos, double[] lookVector, int maxDistance, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision) {
|
||||
if (startPos.length != 3 || lookVector.length != 3) {
|
||||
return null;
|
||||
}
|
||||
@@ -368,7 +330,9 @@ public class ScriptWorld implements IWorld {
|
||||
pos = new ScriptBlockPos(new BlockPos(currentPos.xCoord, currentPos.yCoord, currentPos.zCoord));
|
||||
|
||||
IBlock block = getBlock((int)currentPos.xCoord, (int)currentPos.yCoord, (int)currentPos.zCoord);
|
||||
if (block != null) {
|
||||
if (block != null && stopOnBlock) {
|
||||
if ((!stopOnLiquid || block.getMCBlock() instanceof BlockLiquid)
|
||||
&& (!stopOnCollision || block.isCollidable()))
|
||||
return pos;
|
||||
}
|
||||
|
||||
@@ -385,8 +349,32 @@ public class ScriptWorld implements IWorld {
|
||||
return null;
|
||||
}
|
||||
|
||||
public IPos rayCastPos(double[] startPos, double[] lookVector, int maxDistance) {
|
||||
return rayCastPos(startPos,lookVector,maxDistance,true, false, false);
|
||||
}
|
||||
|
||||
public IPos rayCastPos(IPos startPos, IPos lookVector, int maxDistance, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision) {
|
||||
return rayCastPos(new double[] {startPos.getX(), startPos.getY(), startPos.getZ()}, lookVector.normalize(), maxDistance, stopOnBlock, stopOnLiquid, stopOnCollision);
|
||||
}
|
||||
|
||||
public IPos rayCastPos(IPos startPos, IPos lookVector, int maxDistance) {
|
||||
return rayCastPos(new double[] {startPos.getX(), startPos.getY(), startPos.getZ()}, lookVector.normalize(), maxDistance);
|
||||
return rayCastPos(new double[] {startPos.getX(), startPos.getY(), startPos.getZ()}, lookVector.normalize(), maxDistance, true, false, false);
|
||||
}
|
||||
|
||||
public IBlock rayCastBlock(double[] startPos, double[] lookVector, int maxDistance, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision) {
|
||||
return getBlock(rayCastPos(startPos,lookVector,maxDistance,stopOnBlock,stopOnLiquid,stopOnCollision));
|
||||
}
|
||||
|
||||
public IBlock rayCastBlock(double[] startPos, double[] lookVector, int maxDistance) {
|
||||
return rayCastBlock(startPos, lookVector, maxDistance, true, false, false);
|
||||
}
|
||||
|
||||
public IBlock rayCastBlock(IPos startPos, IPos lookVector, int maxDistance, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision) {
|
||||
return rayCastBlock(new double[] {startPos.getX(), startPos.getY(), startPos.getZ()}, lookVector.normalize(), maxDistance, stopOnBlock, stopOnLiquid, stopOnCollision);
|
||||
}
|
||||
|
||||
public IBlock rayCastBlock(IPos startPos, IPos lookVector, int maxDistance) {
|
||||
return rayCastBlock(new double[] {startPos.getX(), startPos.getY(), startPos.getZ()}, lookVector.normalize(), maxDistance);
|
||||
}
|
||||
|
||||
public IPos getNearestAir(IPos pos, int maxHeight) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package noppes.npcs.scripted.entity;
|
||||
|
||||
import net.minecraft.block.BlockLiquid;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.SharedMonsterAttributes;
|
||||
import net.minecraft.potion.Potion;
|
||||
@@ -111,23 +112,31 @@ public class ScriptLivingBase<T extends EntityLivingBase> extends ScriptEntity<T
|
||||
return new ScriptBlockPos(new BlockPos(lookVec.xCoord,lookVec.yCoord,lookVec.zCoord));
|
||||
}
|
||||
|
||||
public IBlock getLookingAtBlock(int maxDistance) {
|
||||
public IBlock getLookingAtBlock(int maxDistance, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision) {
|
||||
Vec3 lookVec = entity.getLookVec();
|
||||
return getWorld().rayCastBlock(
|
||||
new double[] {entity.posX, entity.posY+entity.getEyeHeight(), entity.posZ},
|
||||
new double[] {lookVec.xCoord, lookVec.yCoord, lookVec.zCoord},
|
||||
maxDistance);
|
||||
maxDistance, stopOnBlock, stopOnLiquid, stopOnCollision);
|
||||
}
|
||||
|
||||
public IPos getLookingAtPos(int maxDistance) {
|
||||
public IBlock getLookingAtBlock(int maxDistance) {
|
||||
return getLookingAtBlock(maxDistance, true, false, false);
|
||||
}
|
||||
|
||||
public IPos getLookingAtPos(int maxDistance, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision) {
|
||||
Vec3 lookVec = entity.getLookVec();
|
||||
return getWorld().rayCastPos(
|
||||
new double[] {entity.posX, entity.posY+entity.getEyeHeight(), entity.posZ},
|
||||
new double[] {lookVec.xCoord, lookVec.yCoord, lookVec.zCoord},
|
||||
maxDistance);
|
||||
maxDistance, stopOnBlock, stopOnLiquid, stopOnCollision);
|
||||
}
|
||||
|
||||
public IEntity[] getLookingAtEntities(int maxDistance, int range) {
|
||||
public IPos getLookingAtPos(int maxDistance) {
|
||||
return getLookingAtPos(maxDistance, true, false, false);
|
||||
}
|
||||
|
||||
public IEntity[] getLookingAtEntities(int maxDistance, int range, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision) {
|
||||
Vec3 lookVec = entity.getLookVec();
|
||||
double[] startPos = new double[] {entity.posX, entity.posY+entity.getEyeHeight(), entity.posZ};
|
||||
double[] lookVector = new double[] {lookVec.xCoord, lookVec.yCoord, lookVec.zCoord};
|
||||
@@ -139,6 +148,13 @@ public class ScriptLivingBase<T extends EntityLivingBase> extends ScriptEntity<T
|
||||
while (rep++ < maxDistance + 10) {
|
||||
currentPos = currentPos.addVector(lookVector[0], lookVector[1], lookVector[2]);
|
||||
IPos pos = new ScriptBlockPos(new BlockPos(currentPos.xCoord, currentPos.yCoord, currentPos.zCoord));
|
||||
IBlock block = getWorld().getBlock(pos);
|
||||
|
||||
if (block != null && stopOnBlock) {
|
||||
if ((!stopOnLiquid || block.getMCBlock() instanceof BlockLiquid)
|
||||
&& (!stopOnCollision || block.isCollidable()))
|
||||
return entities.toArray(new IEntity[0]);
|
||||
}
|
||||
|
||||
IEntity[] entitiesNear = getWorld().getEntitiesNear(pos,range);
|
||||
for (IEntity entity : entitiesNear) {
|
||||
@@ -159,6 +175,10 @@ public class ScriptLivingBase<T extends EntityLivingBase> extends ScriptEntity<T
|
||||
|
||||
return entities.toArray(new IEntity[0]);
|
||||
}
|
||||
|
||||
public IEntity[] getLookingAtEntities(int maxDistance, int range) {
|
||||
return getLookingAtEntities(maxDistance,range,true,false,true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Expert use only
|
||||
|
||||
@@ -103,4 +103,6 @@ public interface IBlock {
|
||||
* @return An INbt object which can modify the block's tile entity's NBT data.
|
||||
*/
|
||||
INbt getTileEntityNBT();
|
||||
|
||||
boolean isCollidable();
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
package noppes.npcs.scripted.interfaces;
|
||||
|
||||
import net.minecraft.block.BlockLiquid;
|
||||
import net.minecraft.util.Vec3;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import noppes.npcs.scripted.ScriptBlockPos;
|
||||
import noppes.npcs.scripted.ScriptScoreboard;
|
||||
import noppes.npcs.scripted.interfaces.entity.IEntity;
|
||||
import noppes.npcs.scripted.interfaces.entity.IPlayer;
|
||||
@@ -160,27 +163,28 @@ public interface IWorld {
|
||||
*/
|
||||
public void removeBlock(int x, int y, int z);
|
||||
|
||||
/**
|
||||
* starting at the start position, draw a line in the lookVector direction until a block is detected
|
||||
* @param startPos
|
||||
* @param lookVector should be a normalized direction vector
|
||||
* @param maxDistance
|
||||
* @return the first detected block but null if maxDistance is reached
|
||||
*/
|
||||
public IBlock rayCastBlock(double[] startPos, double[] lookVector, int maxDistance);
|
||||
IPos rayCastPos(double[] startPos, double[] lookVector, int maxDistance, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision);
|
||||
|
||||
IPos rayCastPos(double[] startPos, double[] lookVector, int maxDistance);
|
||||
|
||||
IPos rayCastPos(IPos startPos, IPos lookVector, int maxDistance, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision);
|
||||
|
||||
IPos rayCastPos(IPos startPos, IPos lookVector, int maxDistance);
|
||||
|
||||
/**
|
||||
* starting at the start position, draw a line in the lookVector direction until a block is detected
|
||||
* @param startPos
|
||||
* @param lookVector will normalize x, y, z to get a direction vector
|
||||
* @param maxDistance
|
||||
* @return the first detected block but null if maxDistance is reached
|
||||
*/
|
||||
public IBlock rayCastBlock(IPos startPos, IPos lookVector, int maxDistance);
|
||||
* starting at the start position, draw a line in the lookVector direction until a block is detected
|
||||
* @param startPos
|
||||
* @param lookVector should be a normalized direction vector
|
||||
* @param maxDistance
|
||||
* @return the first detected block but null if maxDistance is reached
|
||||
*/
|
||||
IBlock rayCastBlock(double[] startPos, double[] lookVector, int maxDistance, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision);
|
||||
|
||||
public IPos rayCastPos(double[] startPos, double[] lookVector, int maxDistance);
|
||||
IBlock rayCastBlock(double[] startPos, double[] lookVector, int maxDistance);
|
||||
|
||||
public IPos rayCastPos(IPos startPos, IPos lookVector, int maxDistance);
|
||||
IBlock rayCastBlock(IPos startPos, IPos lookVector, int maxDistance, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision);
|
||||
|
||||
IBlock rayCastBlock(IPos startPos, IPos lookVector, int maxDistance);
|
||||
|
||||
/**
|
||||
* @param startPos
|
||||
|
||||
@@ -82,6 +82,8 @@ public interface IEntityLivingBase<T extends EntityLivingBase> extends IEntity<T
|
||||
* @return A list of entities, sorted first by closest to the entity along the look vector, then by which
|
||||
* entities are closest to the vector.
|
||||
*/
|
||||
IEntity[] getLookingAtEntities(int maxDistance, int range, boolean stopOnBlock, boolean stopOnLiquid, boolean stopOnCollision);
|
||||
|
||||
IEntity[] getLookingAtEntities(int maxDistance, int range);
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user