ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/api/Position.java

119 lines
3.5 KiB
Java
Raw Normal View History

/*
2015-12-30 22:02:15 +01:00
* This file ("Position.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.api;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
2016-01-07 18:20:59 +01:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.Item;
import net.minecraft.tileentity.TileEntity;
2016-01-07 18:20:59 +01:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.Vec3;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
2016-01-05 04:47:35 +01:00
/**
* This utility class describes a position in the world
*/
public class Position extends BlockPos{
public Position(int x, int y, int z){
super(x, y, z);
}
public TileEntity getTileEntity(IBlockAccess world){
return world != null ? world.getTileEntity(this) : null;
}
public Material getMaterial(IBlockAccess world){
2016-01-07 18:20:59 +01:00
if(world != null){
Block block = this.getBlock(world);
if(block != null){
return block.getMaterial();
}
}
return null;
}
public Item getItemBlock(IBlockAccess world){
return world != null ? Item.getItemFromBlock(this.getBlock(world)) : null;
}
public Block getBlock(IBlockAccess world){
2016-01-07 18:20:59 +01:00
if(world != null){
IBlockState state = this.getBlockState(world);
if(state != null){
return state.getBlock();
}
}
return null;
}
public int getMetadata(IBlockAccess world){
2016-01-07 18:20:59 +01:00
//TODO Fix meta
return /*world != null ? world.getBlockMetadata(this.x, this.y, this.z) : */0;
}
public void setMetadata(IBlockAccess world, int meta, int flag){
2016-01-07 18:20:59 +01:00
//TODO Fix meta
/*if(world != null){
world.setBlockMetadataWithNotify(this.x, this.y, this.z, meta, flag);
2016-01-07 18:20:59 +01:00
}*/
}
public boolean isEqual(Position pos){
return pos != null && this.getX() == pos.getX() && this.getY() == pos.getY() && this.getZ() == pos.getZ();
}
2016-01-07 18:20:59 +01:00
public boolean setBlock(World world, Block block, int meta, int flag){
//TODO Fix meta
return world != null && this.setBlockState(world, block.getDefaultState(), meta, flag);
}
public boolean setBlockState(World world, IBlockState state, int meta, int flag){
//TODO Fix meta
return world.setBlockState(this, state, flag);
}
public Position copy(){
return new Position(this.getX(), this.getY(), this.getZ());
}
public String toString(){
return "["+this.getX()+", "+this.getY()+", "+this.getZ()+"]";
}
public Vec3 toVec(){
return new Vec3(this.getX(), this.getY(), this.getZ());
2016-01-07 18:20:59 +01:00
}
public IBlockState getBlockState(IBlockAccess world){
return world != null ? world.getBlockState(this) : null;
2016-01-07 18:20:59 +01:00
}
public Position getOffsetPosition(EnumFacing side){
return new Position(this.getX()+side.getFrontOffsetX(), this.getY()+side.getFrontOffsetY(), this.getZ()+side.getFrontOffsetZ());
2016-01-07 18:20:59 +01:00
}
public Position getOffsetPosition(int x, int y, int z){
return new Position(this.getX()+x, this.getY()+y, this.getZ()+z);
}
2016-01-07 18:20:59 +01:00
public static Position fromTileEntity(TileEntity tile){
return fromBlockPos(tile.getPos());
}
public static Position fromBlockPos(BlockPos pos){
return (Position)pos;
}
}