Advertisement
Guest User

Untitled

a guest
Oct 30th, 2015
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1. package com.cout970.magneticraft.api.util;
  2.  
  3. import com.google.common.base.Objects;
  4. import net.minecraft.block.Block;
  5. import net.minecraft.entity.player.EntityPlayerMP;
  6. import net.minecraft.nbt.NBTTagCompound;
  7. import net.minecraft.tileentity.TileEntity;
  8. import net.minecraft.util.MathHelper;
  9. import net.minecraft.world.World;
  10. import net.minecraftforge.common.util.ForgeDirection;
  11.  
  12. import javax.annotation.Nonnull;
  13.  
  14. public class VecInt implements Comparable<VecInt> {
  15.     public static final VecInt NULL_VECTOR = new VecInt(0, 0, 0);
  16.     protected int x;
  17.     protected int y;
  18.     protected int z;
  19.  
  20.     public VecInt(int x, int y, int z) {
  21.         this.x = x;
  22.         this.y = y;
  23.         this.z = z;
  24.     }
  25.  
  26.     public VecInt(double x, double y, double z) {
  27.         this(MathHelper.floor_double(x), MathHelper.floor_double(y), MathHelper.floor_double(z));
  28.     }
  29.  
  30.     public VecInt(int[] ar) {
  31.         this(ar[0], ar[1], ar[2]);
  32.     }
  33.  
  34.  
  35.     public VecInt(TileEntity tile) {
  36.         this(tile.xCoord, tile.yCoord, tile.zCoord);
  37.     }
  38.  
  39.     public VecInt(EntityPlayerMP pl) {
  40.         this(pl.posX, pl.posY, pl.posZ);
  41.     }
  42.  
  43.     public VecInt(NBTTagCompound nbt, String name) {
  44.         this(nbt.getInteger(name + "_x"), nbt.getInteger(name + "_y"), nbt.getInteger(name + "_z"));
  45.     }
  46.  
  47.     public static VecInt fromDirection(MgDirection d) {
  48.         return new VecInt(d.getOffsetX(), d.getOffsetY(), d.getOffsetZ());
  49.     }
  50.  
  51.     public static VecInt getConnection(ForgeDirection d) {
  52.         return new VecInt(d.offsetX, d.offsetY, d.offsetZ);
  53.     }
  54.  
  55.     public static VecInt load(NBTTagCompound nbt) {
  56.         return new VecInt(nbt.getInteger("X"), nbt.getInteger("Y"), nbt.getInteger("Z"));
  57.     }
  58.  
  59.     public VecInt getOpposite() {
  60.         return new VecInt(-x, -y, -z);
  61.     }
  62.  
  63.     public boolean equals(Object obj) {
  64.         if (this == obj) {
  65.             return true;
  66.         } else if (!(obj instanceof VecInt)) {
  67.             return false;
  68.         } else {
  69.             VecInt vecInt = (VecInt) obj;
  70.             return (this.getX() == vecInt.getX()) && ((this.getY() == vecInt.getY()) && (this.getZ() == vecInt.getZ()));
  71.         }
  72.     }
  73.  
  74.     public int hashCode() {
  75.         return (this.getY() + this.getZ() * 31) * 31 + this.getX();
  76.     }
  77.  
  78.     public int compareTo(@Nonnull VecInt vec) {
  79.         return this.getY() == vec.getY() ? (this.getZ() == vec.getZ() ? this
  80.                 .getX() - vec.getX() : this.getZ() - vec.getZ()) : this.getY()
  81.                 - vec.getY();
  82.     }
  83.  
  84.     public int getX() {
  85.         return this.x;
  86.     }
  87.  
  88.     public int getY() {
  89.         return this.y;
  90.     }
  91.  
  92.     public int getZ() {
  93.         return this.z;
  94.     }
  95.  
  96.     public String toString() {
  97.         return Objects.toStringHelper(this).add("x", this.getX())
  98.                 .add("y", this.getY()).add("z", this.getZ()).toString();
  99.     }
  100.  
  101.     public MgDirection toMgDirection() {
  102.         for (MgDirection d : MgDirection.values())
  103.             if (d.getOffsetX() == x && d.getOffsetY() == y && d.getOffsetZ() == z)
  104.                 return d;
  105.         return null;
  106.     }
  107.  
  108.     public VecInt multiply(int i) {
  109.         x *= i;
  110.         y *= i;
  111.         z *= i;
  112.         return this;
  113.     }
  114.  
  115.     public VecInt add(VecInt v) {
  116.         x += v.x;
  117.         y += v.y;
  118.         z += v.z;
  119.         return this;
  120.     }
  121.  
  122.     public VecInt add(int a, int b, int c) {
  123.         x += a;
  124.         y += b;
  125.         z += c;
  126.         return this;
  127.     }
  128.  
  129.     public VecInt copy() {
  130.         return new VecInt(x, y, z);
  131.     }
  132.  
  133.     public void save(NBTTagCompound nbt) {
  134.         nbt.setInteger("X", x);
  135.         nbt.setInteger("Y", y);
  136.         nbt.setInteger("Z", z);
  137.     }
  138.  
  139.     public int[] intArray() {
  140.         return new int[]{x, y, z};
  141.     }
  142.  
  143.     public int squareDistance() {
  144.         return x * x + y * y + z * z;
  145.     }
  146.  
  147.     public void save(NBTTagCompound nbt, String name) {
  148.         nbt.setInteger(name + "_x", x);
  149.         nbt.setInteger(name + "_y", y);
  150.         nbt.setInteger(name + "_z", z);
  151.     }
  152.  
  153.     public TileEntity getTileEntity(World w) {
  154.         return w.getTileEntity(x, y, z);
  155.     }
  156.  
  157.     public VecInt add(MgDirection dir) {
  158.         return this.add(dir.getOffsetX(), dir.getOffsetY(), dir.getOffsetZ());
  159.     }
  160.  
  161.     public Block getBlock(World world) {
  162.         return world.getBlock(x, y, z);
  163.     }
  164.  
  165.     public int getBlockMetadata(World world) {
  166.          return world.getBlockMetadata(x, y, z);
  167.     }
  168.  
  169.     public void setBlockMetadata(World world, int meta, int flags) {
  170.         world.setBlockMetadataWithNotify(x, y, z, meta, flags);
  171.     }
  172.    
  173.     public void setBlock(World world, Block block){
  174.         world.setBlock(x, y, z, block);
  175.     }
  176.  
  177.     public boolean isBlockReplaceable(World world) {
  178.         return getBlock(world).isReplaceable(world, x, y, z);
  179.     }
  180.  
  181.     public void setBlockWithMetadata(World world, Block block, int meta, int flags) {
  182.         setBlock(world, block);
  183.         setBlockMetadata(world, meta, flags);
  184.     }
  185.  
  186.     public boolean blockExists(World world) {
  187.         return world.blockExists(x, y, z);
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement