Advertisement
Guest User

Untitled

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