-Added crossProduct(IPos), divide(scalar), and to/FromLong functions.

NpcAPI:
-Added getAllInBox(from,to)

IBlock:
-Added setPosition functions

IBlock, IPos, IEntity, IWorld: -Added a readable toString() function
This commit is contained in:
Luis C.M committed 2022-11-06 15:08:58 -06:00
1 parent 084e6e4149
commit c8a8e866cb
10 files changed
+191 -25

No files matched your search

+15 -4
View File
@@ -54,10 +54,7 @@ import noppes.npcs.util.LRUHashMap;
import noppes.npcs.util.NBTJsonUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class NpcAPI extends AbstractNpcAPI {
private static final Map<Integer, ScriptWorld> worldCache = new LRUHashMap<>(10);
@@ -238,6 +235,20 @@ public class NpcAPI extends AbstractNpcAPI {
return this.getIPos((int)x,(int)y,(int)z);
}
public IPos[] getAllInBox(IPos from, IPos to, boolean sortByDistance) {
Iterator<BlockPos> posIterable = BlockPos.getAllInBox(from.getMCPos(),to.getMCPos()).iterator();
ArrayList<IPos> list = new ArrayList<>();
posIterable.forEachRemaining(BlockPos -> list.add(this.getIPos(BlockPos)));
if (sortByDistance) {
list.sort(Comparator.comparingDouble(pos -> pos.distanceTo(from)));
}
return list.toArray(new IPos[0]);
}
public IPos[] getAllInBox(IPos from, IPos to) {
return this.getAllInBox(from,to,true);
}
public INbt getINbt(NBTTagCompound nbtTagCompound) {
return nbtTagCompound == null?new ScriptNbt(new NBTTagCompound()):new ScriptNbt(nbtTagCompound);
}
@@ -20,10 +20,10 @@ import noppes.npcs.util.LRUHashMap;
public class ScriptBlock implements IBlock {
private static final Map<String, IBlock> blockCache = new LRUHashMap(400);
protected final IWorld world;
protected final Block block;
protected final BlockPos pos;
protected final IPos bPos;
protected IWorld world;
protected Block block;
protected BlockPos pos;
protected IPos bPos;
protected ITileEntity tile;
public ScriptBlock(World world, Block block, BlockPos pos) {
@@ -34,10 +34,56 @@ public class ScriptBlock implements IBlock {
this.tile = NpcAPI.Instance().getITileEntity(world.getTileEntity(pos.getX(),pos.getY(), pos.getZ()));
}
public IPos getPosition() {
return this.bPos;
}
public IPos getPos() {
return this.bPos;
}
public boolean setPosition(IPos pos, IWorld world) {
if (pos == null || world == null || !world.setBlock(pos, this))
return false;
this.world.removeBlock(this.getPos());
this.world = world;
this.block = world.getBlock(pos).getMCBlock();
this.pos = pos.getMCPos();
this.bPos = pos;
this.tile = NpcAPI.Instance().getITileEntity(world.getMCWorld().getTileEntity(pos.getX(),pos.getY(),pos.getZ()));
return true;
}
public boolean setPosition(IPos pos) {
return this.setPosition(pos,world);
}
public boolean setPos(IPos pos, IWorld world) {
return this.setPosition(pos,world);
}
public boolean setPos(IPos pos) {
return this.setPosition(pos);
}
public boolean setPosition(int x, int y, int z, IWorld world) {
return this.setPos(NpcAPI.Instance().getIPos(x,y,z),world);
}
public boolean setPosition(int x, int y, int z) {
return this.setPos(NpcAPI.Instance().getIPos(x,y,z));
}
public boolean setPos(int x, int y, int z, IWorld world) {
return this.setPosition(x,y,z, world);
}
public boolean setPos(int x, int y, int z) {
return this.setPosition(x,y,z);
}
public int getX() {
return this.pos.getX();
}
@@ -148,4 +194,8 @@ public class ScriptBlock implements IBlock {
public boolean canCollide() {
return canCollide(0);
}
public String toString() {
return this.getName() + " @" + this.getPos() + (world == null ? "" : " in DIM" + world.getDimensionID());
}
}
@@ -6,6 +6,7 @@
package noppes.npcs.scripted;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.Vec3i;
import net.minecraft.util.math.BlockPos;
import noppes.npcs.scripted.interfaces.IPos;
@@ -100,6 +101,28 @@ public class ScriptBlockPos implements IPos {
return NpcAPI.Instance().getIPos(this.blockPos.offset(EnumFacing.values()[direction], n));
}
public IPos crossProduct(int x, int y, int z) {
return this.crossProduct(NpcAPI.Instance().getIPos(x,y,z));
}
public IPos crossProduct(IPos pos) {
return NpcAPI.Instance().getIPos(this.blockPos.crossProduct(new Vec3i(-pos.getX(), -pos.getY(), -pos.getZ())));
}
public IPos divide(float scalar) {
return NpcAPI.Instance().getIPos(
getX()/scalar , getY()/scalar, getZ()/scalar
);
}
public long toLong() {
return blockPos.toLong();
}
public IPos fromLong(long serialized) {
return NpcAPI.Instance().getIPos(BlockPos.fromLong(serialized));
}
public double[] normalize() {
double d = Math.sqrt((double)(this.blockPos.getX() * this.blockPos.getX() + this.blockPos.getY() * this.blockPos.getY() + this.blockPos.getZ() * this.blockPos.getZ()));
return new double[]{(double)this.getX() / d, (double)this.getY() / d, (double)this.getZ() / d};
@@ -115,4 +138,8 @@ public class ScriptBlockPos implements IPos {
public BlockPos getMCPos() {
return this.blockPos;
}
public String toString() {
return "(" + this.getX() + ", " + this.getY() + ", " + this.getZ() + ")";
}
}
@@ -418,16 +418,16 @@ public class ScriptWorld implements IWorld {
}
public void setBlock(int x, int y, int z, IBlock block){
public boolean setBlock(int x, int y, int z, IBlock block){
if(block == null || block.getMCBlock().isAir(world, x, y, z)){
this.removeBlock(x, y, z);
return;
return false;
}
world.setBlock(x, y, z, block.getMCBlock());
return world.setBlock(x, y, z, block.getMCBlock());
}
public void setBlock(IPos pos, IBlock block){
this.setBlock(pos.getX(),pos.getY(),pos.getZ(),block);
public boolean setBlock(IPos pos, IBlock block){
return this.setBlock(pos.getX(),pos.getY(),pos.getZ(),block);
}
/**
@@ -436,19 +436,20 @@ public class ScriptWorld implements IWorld {
* @param z World position z
* @param item The block to be set
*/
public void setBlock(int x, int y, int z, IItemStack item){
public boolean setBlock(int x, int y, int z, IItemStack item){
if(item == null){
this.removeBlock(x, y, z);
return;
return false;
}
Block block = Block.getBlockFromItem(item.getMCItemStack().getItem());
if(block == null || block == Blocks.air)
return;
world.setBlock(x, y, z, block);
return false;
return world.setBlock(x, y, z, block);
}
public void setBlock(IPos pos, IItemStack itemStack){
this.setBlock(pos.getX(),pos.getY(),pos.getZ(),itemStack);
public boolean setBlock(IPos pos, IItemStack itemStack){
return this.setBlock(pos.getX(),pos.getY(),pos.getZ(),itemStack);
}
/**
@@ -893,4 +894,8 @@ public class ScriptWorld implements IWorld {
public int getDimensionID(){
return world.provider.dimensionId;
}
public String toString() {
return "DIM" + this.getDimensionID();
}
}
@@ -85,21 +85,52 @@ public class ScriptPlayer<T extends EntityPlayerMP> extends ScriptLivingBase<T>
public void setPosition(double x, double y, double z){
NoppesUtilPlayer.teleportPlayer(player, x, y, z, player.dimension);
}
public void setPos(double x, double y, double z) {
this.setPosition(x,y,z);
}
public void setPosition(IPos pos) {
this.setPosition(pos.getX(),pos.getY(),pos.getZ());
}
public void setPos(IPos pos) {
this.setPosition(pos);
}
public void setPosition(double x, double y, double z, int dimensionId){
public void setPosition(double x, double y, double z, int dimensionId) {
if (NpcAPI.Instance().getIWorld(dimensionId) == null)
return;
NoppesUtilPlayer.teleportPlayer(player, x, y, z, dimensionId);
}
public void setPos(double x, double y, double z, int dimensionId) {
this.setPosition(x,y,z,dimensionId);
}
public void setPosition(double x, double y, double z, IWorld world) {
this.setPosition(x,y,z,world.getDimensionID());
}
public void setPos(double x, double y, double z, IWorld world) {
this.setPos(x,y,z,world.getDimensionID());
}
public void setPosition(IPos pos, int dimensionId) {
this.setPosition(pos.getX(),pos.getY(),pos.getZ(), dimensionId);
}
public void setPos(IPos pos, int dimensionId) {
this.setPosition(pos,dimensionId);
}
public void setPosition(IPos pos, IWorld world) {
this.setPosition(pos.getX(),pos.getY(),pos.getZ(),world.getDimensionID());
}
public void setPos(IPos pos, IWorld world) {
this.setPosition(pos,world.getDimensionID());
}
public void setDimension(int dimension) {
this.setPosition(this.getPos(),dimension);
}
public void setDimension(IWorld world) {
this.setDimension(world.getDimensionID());
}
public int getHunger(){
return player.getFoodStats().getFoodLevel();
@@ -116,6 +116,19 @@ public abstract class AbstractNpcAPI {
public abstract IPos getIPos(float x, float y, float z);
/**
* Forms a box with corners as the input IPos parameters, and returns all
* points inside the box as a list of IPos vectors.
*
* @param from The starting IPos vector, first corner of the box.
* @param to The ending IPos vector, opposite corner of the box.
* @param sortByDistance Sorts the list by distance from the "from" IPos parameter.
* @return The list of all IPos vectors inside the box.
*/
public abstract IPos[] getAllInBox(IPos from, IPos to, boolean sortByDistance);
public abstract IPos[] getAllInBox(IPos from, IPos to);
public abstract IContainer getIContainer(IInventory var1);
public abstract IContainer getIContainer(Container var1);
@@ -19,7 +19,22 @@ public interface IBlock {
*
* @return An IPos object with the block's XYZ position.
*/
IPos getPos();
IPos getPosition();
/**
* Moves the block to a new position in the given world. The old position is replaced with air.
*
* @param pos The new position of the block
* @param world The destination world of the block
* @return Whether the block was successfully placed
*/
boolean setPosition(IPos pos, IWorld world);
boolean setPosition(IPos pos);
boolean setPosition(int x, int y, int z, IWorld world);
boolean setPosition(int x, int y, int z);
/**
*
@@ -152,6 +152,16 @@ public interface IPos {
*/
IPos offset(int direction, int n);
IPos crossProduct(int x, int y, int z);
IPos crossProduct(IPos pos);
IPos divide(float scalar);
long toLong();
IPos fromLong(long serialized);
/**
*
* @param pos The IPos object to calculate the distance to
@@ -186,8 +186,8 @@ public interface IWorld {
* @param z World position z
* @param item The block to be set
*/
void setBlock(int x, int y, int z, IItemStack item);
void setBlock(IPos pos, IItemStack item);
boolean setBlock(int x, int y, int z, IItemStack item);
boolean setBlock(IPos pos, IItemStack item);
/**
* @param x World position x
@@ -195,8 +195,8 @@ public interface IWorld {
* @param z World position z
* @param block The block to be set
*/
void setBlock(int x, int y, int z, IBlock block);
void setBlock(IPos pos, IBlock block);
boolean setBlock(int x, int y, int z, IBlock block);
boolean setBlock(IPos pos, IBlock block);
/**
* @param x World position x
@@ -33,6 +33,10 @@ public interface IPlayer<T extends EntityPlayerMP> extends IEntityLivingBase<T>
void setPosition(double x, double y, double z, int dimensionId);
void setPosition(IPos pos, int dimensionId);
void setPosition(double x, double y, double z, IWorld world);
void setPosition(IPos pos, IWorld world);
void setDimension(int dimension);
int getHunger();