Mrhand3

BlockUtils

Jan 30th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. package Mrhand3.SOARYN.InventoryReg.utils;
  2.  
  3. import net.minecraft.entity.EntityLivingBase;
  4. import net.minecraft.entity.item.EntityItem;
  5. import net.minecraft.inventory.IInventory;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.tileentity.TileEntity;
  9. import net.minecraft.util.MathHelper;
  10. import net.minecraft.world.World;
  11. import net.minecraftforge.common.ForgeDirection;
  12.  
  13. public class BlockUtils
  14. {
  15. public static final ForgeDirection DEFAULT_BLOCK_DIRECTION = ForgeDirection.WEST;
  16.  
  17. public static ForgeDirection get2dOrientation(EntityLivingBase entity)
  18. {
  19. int l = MathHelper.floor_double(entity.rotationYaw * 4.0F / 360.0F + 0.5D) & 0x3;
  20.  
  21. switch (l)
  22. {
  23. case 0:
  24. return ForgeDirection.SOUTH;
  25.  
  26. case 1:
  27. return ForgeDirection.WEST;
  28.  
  29. case 2:
  30. return ForgeDirection.NORTH;
  31.  
  32. case 3:
  33. return ForgeDirection.EAST;
  34. }
  35.  
  36. return ForgeDirection.SOUTH;
  37. }
  38.  
  39. public static float getRotationFromDirection(ForgeDirection direction)
  40. {
  41. switch (direction)
  42. {
  43. case NORTH:
  44. return 0F;
  45.  
  46. case SOUTH:
  47. return 180F;
  48.  
  49. case WEST:
  50. return 90F;
  51.  
  52. case EAST:
  53. return -90F;
  54.  
  55. case DOWN:
  56. return -90f;
  57.  
  58. case UP:
  59. return 90f;
  60.  
  61. default:
  62. return 0f;
  63. }
  64. }
  65.  
  66. public static ForgeDirection get3dOrientation(EntityLivingBase entity)
  67. {
  68. if (entity.rotationPitch > 45.5F)
  69. {
  70. return ForgeDirection.DOWN;
  71. }
  72. else if (entity.rotationPitch < -45.5F)
  73. {
  74. return ForgeDirection.UP;
  75. }
  76.  
  77. return get2dOrientation(entity);
  78. }
  79.  
  80. public static EntityItem dropItemStackInWorld(World worldObj, int x, int y, int z, ItemStack stack)
  81. {
  82. return dropItemStackInWorld(worldObj, (double)x, (double)y, (double)z, stack);
  83. }
  84.  
  85. public static EntityItem dropItemStackInWorld(World worldObj, double x, double y, double z, ItemStack stack)
  86. {
  87. float f = 0.7F;
  88. float d0 = worldObj.rand.nextFloat() * f + (1.0F - f) * 0.5F;
  89. float d1 = worldObj.rand.nextFloat() * f + (1.0F - f) * 0.5F;
  90. float d2 = worldObj.rand.nextFloat() * f + (1.0F - f) * 0.5F;
  91. EntityItem entityitem = new EntityItem(worldObj, x + d0, y + d1, z + d2, stack);
  92. entityitem.delayBeforeCanPickup = 10;
  93.  
  94. if (stack.hasTagCompound())
  95. {
  96. entityitem.getEntityItem().setTagCompound((NBTTagCompound)stack.getTagCompound().copy());
  97. }
  98.  
  99. worldObj.spawnEntityInWorld(entityitem);
  100. return entityitem;
  101. }
  102.  
  103. public static EntityItem ejectItemInDirection(World world, double x, double y, double z, ForgeDirection direction, ItemStack stack)
  104. {
  105. EntityItem item = BlockUtils.dropItemStackInWorld(world, x, y, z, stack);
  106. item.motionX = direction.offsetX / 5F;
  107. item.motionY = direction.offsetY / 5F;
  108. item.motionZ = direction.offsetZ / 5F;
  109. return item;
  110. }
  111.  
  112. public static void dropTileInventory(TileEntity tileEntity)
  113. {
  114. if (tileEntity != null && tileEntity instanceof IInventory)
  115. {
  116. IInventory inventory = (IInventory)tileEntity;
  117. dropInventory(inventory, tileEntity.worldObj, tileEntity.xCoord, tileEntity.yCoord, tileEntity.zCoord);
  118. }
  119. }
  120.  
  121. public static void dropInventory(IInventory inventory, World world, double x, double y, double z)
  122. {
  123. if (inventory == null)
  124. {
  125. return;
  126. }
  127.  
  128. for (int i = 0; i < inventory.getSizeInventory(); ++i)
  129. {
  130. ItemStack itemStack = inventory.getStackInSlot(i);
  131.  
  132. if (itemStack != null)
  133. {
  134. dropItemStackInWorld(world, x, y, z, itemStack);
  135. }
  136. }
  137. }
  138.  
  139. public static void dropInventory(IInventory inventory, World world, int x, int y, int z)
  140. {
  141. dropInventory(inventory, world, x + 0.5, y + 0.5, z + 0.5);
  142. }
  143.  
  144. public static TileEntity getTileInDirection(TileEntity tile, ForgeDirection direction)
  145. {
  146. int targetX = tile.xCoord + direction.offsetX;
  147. int targetY = tile.yCoord + direction.offsetY;
  148. int targetZ = tile.zCoord + direction.offsetZ;
  149. return tile.worldObj.getBlockTileEntity(targetX, targetY, targetZ);
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment