2015-04-28 17:18:32 +02:00
|
|
|
package techreborn.blocks;
|
|
|
|
|
2015-11-23 15:40:30 +01:00
|
|
|
import net.minecraftforge.fml.common.Loader;
|
2015-10-07 09:34:24 +02:00
|
|
|
import ic2.api.item.IC2Items;
|
2015-11-08 13:15:45 +01:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.BlockContainer;
|
|
|
|
import net.minecraft.block.BlockDynamicLiquid;
|
|
|
|
import net.minecraft.block.BlockStaticLiquid;
|
2015-04-28 17:18:32 +02:00
|
|
|
import net.minecraft.block.material.Material;
|
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
2015-05-11 22:53:47 +02:00
|
|
|
import net.minecraft.entity.EnumCreatureType;
|
2015-06-07 01:00:15 +02:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
2015-11-12 22:38:11 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2015-06-07 01:00:15 +02:00
|
|
|
import net.minecraft.inventory.IInventory;
|
2015-10-07 09:34:24 +02:00
|
|
|
import net.minecraft.item.Item;
|
2015-06-14 12:58:56 +02:00
|
|
|
import net.minecraft.item.ItemBlock;
|
2015-04-28 17:18:32 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2015-06-07 01:00:15 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-04-28 17:18:32 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.MathHelper;
|
2015-09-05 10:17:58 +02:00
|
|
|
import net.minecraft.world.IBlockAccess;
|
2015-04-28 17:18:32 +02:00
|
|
|
import net.minecraft.world.World;
|
2015-11-23 15:45:16 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2015-11-12 22:38:11 +01:00
|
|
|
import net.minecraftforge.fluids.*;
|
2015-05-20 00:00:36 +02:00
|
|
|
import techreborn.client.TechRebornCreativeTab;
|
2015-10-07 09:34:24 +02:00
|
|
|
import techreborn.init.ModBlocks;
|
2015-09-05 10:17:58 +02:00
|
|
|
import techreborn.tiles.TileMachineBase;
|
2015-04-28 17:18:32 +02:00
|
|
|
|
2015-10-07 09:34:24 +02:00
|
|
|
import java.util.ArrayList;
|
2015-07-02 20:51:24 +02:00
|
|
|
import java.util.Random;
|
|
|
|
|
2015-06-06 22:41:41 +02:00
|
|
|
public class BlockMachineBase extends BlockContainer {
|
2015-04-28 17:18:32 +02:00
|
|
|
|
2015-08-09 12:05:32 +02:00
|
|
|
public BlockMachineBase(Material material) {
|
|
|
|
super(Material.rock);
|
|
|
|
setCreativeTab(TechRebornCreativeTab.instance);
|
|
|
|
setHardness(2f);
|
|
|
|
setStepSound(soundTypeMetal);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
|
2015-11-11 18:00:18 +01:00
|
|
|
return new TileMachineBase();
|
2015-08-09 12:05:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onBlockAdded(World world, int x, int y, int z) {
|
|
|
|
|
|
|
|
super.onBlockAdded(world, x, y, z);
|
|
|
|
this.setDefaultDirection(world, x, y, z);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setDefaultDirection(World world, int x, int y, int z) {
|
|
|
|
|
|
|
|
if (!world.isRemote) {
|
|
|
|
Block block1 = world.getBlock(x, y, z - 1);
|
|
|
|
Block block2 = world.getBlock(x, y, z + 1);
|
|
|
|
Block block3 = world.getBlock(x - 1, y, z);
|
|
|
|
Block block4 = world.getBlock(x + 1, y, z);
|
|
|
|
|
|
|
|
byte b = 3;
|
|
|
|
|
|
|
|
if (block1.func_149730_j() && !block2.func_149730_j()) {
|
|
|
|
b = 3;
|
|
|
|
}
|
|
|
|
if (block2.func_149730_j() && !block1.func_149730_j()) {
|
|
|
|
b = 2;
|
|
|
|
}
|
|
|
|
if (block3.func_149730_j() && !block4.func_149730_j()) {
|
|
|
|
b = 5;
|
|
|
|
}
|
|
|
|
if (block4.func_149730_j() && !block3.func_149730_j()) {
|
|
|
|
b = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
world.setBlockMetadataWithNotify(x, y, z, b, 2);
|
2015-09-19 10:31:24 +02:00
|
|
|
setTileRotation(world, x, y, z, b);
|
2015-08-09 12:05:32 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onBlockPlacedBy(World world, int x, int y, int z,
|
|
|
|
EntityLivingBase player, ItemStack itemstack) {
|
|
|
|
|
|
|
|
int l = MathHelper
|
|
|
|
.floor_double((double) (player.rotationYaw * 4.0F / 360F) + 0.5D) & 3;
|
|
|
|
|
|
|
|
if (l == 0) {
|
2015-09-19 10:31:24 +02:00
|
|
|
setTileRotation(world, x, y, z, 2);
|
2015-08-09 12:05:32 +02:00
|
|
|
}
|
|
|
|
if (l == 1) {
|
2015-09-19 10:31:24 +02:00
|
|
|
setTileRotation(world, x, y, z, 5);
|
2015-08-09 12:05:32 +02:00
|
|
|
}
|
|
|
|
if (l == 2) {
|
2015-09-19 10:31:24 +02:00
|
|
|
setTileRotation(world, x, y, z, 3);
|
2015-08-09 12:05:32 +02:00
|
|
|
}
|
|
|
|
if (l == 3) {
|
2015-09-19 10:31:24 +02:00
|
|
|
setTileRotation(world, x, y, z, 4);
|
2015-08-09 12:05:32 +02:00
|
|
|
}
|
2015-05-22 22:15:19 +02:00
|
|
|
super.onBlockPlacedBy(world, x, y, z, player, itemstack);
|
2015-08-09 12:05:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canCreatureSpawn(EnumCreatureType type, World world, int x,
|
|
|
|
int y, int z) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-07 01:00:15 +02:00
|
|
|
@Override
|
2015-08-09 12:05:32 +02:00
|
|
|
public void breakBlock(World world, int x, int y, int z, Block block, int meta) {
|
2015-06-07 01:00:15 +02:00
|
|
|
dropInventory(world, x, y, z);
|
|
|
|
super.breakBlock(world, x, y, z, block, meta);
|
|
|
|
}
|
2015-08-09 12:05:32 +02:00
|
|
|
|
|
|
|
protected void dropInventory(World world, int x, int y, int z) {
|
2015-06-07 01:00:15 +02:00
|
|
|
TileEntity tileEntity = world.getTileEntity(x, y, z);
|
|
|
|
|
2015-08-09 12:05:32 +02:00
|
|
|
if (!(tileEntity instanceof IInventory)) {
|
2015-06-07 01:00:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IInventory inventory = (IInventory) tileEntity;
|
|
|
|
|
2015-08-09 12:05:32 +02:00
|
|
|
for (int i = 0; i < inventory.getSizeInventory(); i++) {
|
2015-06-07 01:00:15 +02:00
|
|
|
ItemStack itemStack = inventory.getStackInSlot(i);
|
|
|
|
|
2015-08-09 12:05:32 +02:00
|
|
|
if (itemStack != null && itemStack.stackSize > 0) {
|
|
|
|
if (itemStack.getItem() instanceof ItemBlock) {
|
2015-11-01 17:04:41 +01:00
|
|
|
if (((ItemBlock) itemStack.getItem()).field_150939_a instanceof BlockFluidBase || ((ItemBlock) itemStack.getItem()).field_150939_a instanceof BlockStaticLiquid || ((ItemBlock) itemStack.getItem()).field_150939_a instanceof BlockDynamicLiquid) {
|
2015-06-14 12:58:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-06-07 01:00:15 +02:00
|
|
|
Random rand = new Random();
|
|
|
|
|
|
|
|
float dX = rand.nextFloat() * 0.8F + 0.1F;
|
|
|
|
float dY = rand.nextFloat() * 0.8F + 0.1F;
|
|
|
|
float dZ = rand.nextFloat() * 0.8F + 0.1F;
|
|
|
|
|
|
|
|
EntityItem entityItem = new EntityItem(world, x + dX, y + dY, z + dZ, itemStack.copy());
|
|
|
|
|
2015-08-09 12:05:32 +02:00
|
|
|
if (itemStack.hasTagCompound()) {
|
2015-06-07 01:00:15 +02:00
|
|
|
entityItem.getEntityItem().setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy());
|
|
|
|
}
|
|
|
|
|
|
|
|
float factor = 0.05F;
|
|
|
|
entityItem.motionX = rand.nextGaussian() * factor;
|
|
|
|
entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
|
|
|
|
entityItem.motionZ = rand.nextGaussian() * factor;
|
|
|
|
world.spawnEntityInWorld(entityItem);
|
|
|
|
itemStack.stackSize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-05 10:17:58 +02:00
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
public void setTileRotation(World world, int x, int y, int z, int meta) {
|
|
|
|
if (world.getTileEntity(x, y, z) != null && world.getTileEntity(x, y, z) instanceof TileMachineBase) {
|
2015-09-19 10:31:24 +02:00
|
|
|
((TileMachineBase) world.getTileEntity(x, y, z)).setRotation(meta);
|
2015-09-05 10:17:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
public int getTileRotation(World world, int x, int y, int z) {
|
|
|
|
if (world.getTileEntity(x, y, z) != null && world.getTileEntity(x, y, z) instanceof TileMachineBase) {
|
2015-09-19 10:31:24 +02:00
|
|
|
return ((TileMachineBase) world.getTileEntity(x, y, z)).getRotation();
|
2015-09-05 10:17:58 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
public int getTileRotation(IBlockAccess blockAccess, int x, int y, int z) {
|
2015-11-23 16:15:42 +01:00
|
|
|
return blockAccess.getTileEntity(x, y, z) != null ? getTileRotation(blockAccess.getTileEntity(x, y, z).getWorld(), x, y, z) : 0;
|
2015-09-05 10:17:58 +02:00
|
|
|
}
|
2015-10-07 09:34:24 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune) {
|
|
|
|
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
|
2015-11-08 13:15:45 +01:00
|
|
|
if (Loader.isModLoaded("IC2")) {
|
2015-11-01 16:58:34 +01:00
|
|
|
ItemStack stack = IC2Items.getItem(isAdvanced() ? "advancedMachine" : "machine").copy();
|
|
|
|
stack.stackSize = 1;
|
|
|
|
items.add(stack);
|
2015-10-07 09:34:24 +02:00
|
|
|
} else {
|
2015-11-08 13:15:45 +01:00
|
|
|
items.add(isAdvanced() ? new ItemStack(Item.getItemFromBlock(ModBlocks.MachineCasing), 1, 2) : new ItemStack(Item.getItemFromBlock(ModBlocks.MachineCasing), 1, 0));
|
2015-10-07 09:34:24 +02:00
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
public boolean isAdvanced() {
|
2015-10-07 09:34:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-17 21:21:43 +02:00
|
|
|
|
|
|
|
@Override
|
2015-11-23 15:45:16 +01:00
|
|
|
public boolean rotateBlock(World worldObj, int x, int y, int z, EnumFacing axis) {
|
2015-11-23 16:15:42 +01:00
|
|
|
if (axis == null) {
|
2015-10-17 21:21:43 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
TileEntity tile = worldObj.getTileEntity(x, y, z);
|
2015-11-08 13:15:45 +01:00
|
|
|
if (tile != null && tile instanceof TileMachineBase) {
|
2015-10-17 21:21:43 +02:00
|
|
|
TileMachineBase machineBase = (TileMachineBase) tile;
|
2015-11-23 15:45:16 +01:00
|
|
|
machineBase.setRotation(EnumFacing.getOrientation(machineBase.getRotation()).getRotation(axis).ordinal());
|
2015-10-17 21:21:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-11-12 22:38:11 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityplayer, int side, float hitX, float hitY, float hitZ) {
|
2015-11-16 12:17:36 +01:00
|
|
|
if(fillBlockWithFluid(world, x, y, z, entityplayer, side, hitX, hitY, hitZ)){
|
2015-11-12 22:38:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-16 12:17:36 +01:00
|
|
|
return super.onBlockActivated(world, x, y, z, entityplayer, side, hitX, hitY, hitZ);
|
2015-11-12 22:38:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean fillBlockWithFluid(World world, int x, int y, int z, EntityPlayer entityplayer, int side, float hitX, float hitY, float hitZ) {
|
|
|
|
ItemStack current = entityplayer.inventory.getCurrentItem();
|
|
|
|
|
|
|
|
if (current != null) {
|
|
|
|
TileEntity tile = world.getTileEntity(x, y, z);
|
|
|
|
|
|
|
|
if (tile instanceof IFluidHandler) {
|
|
|
|
IFluidHandler tank = (IFluidHandler) tile;
|
|
|
|
// Handle FluidContainerRegistry
|
|
|
|
if (FluidContainerRegistry.isContainer(current)) {
|
|
|
|
FluidStack liquid = FluidContainerRegistry.getFluidForFilledItem(current);
|
|
|
|
// Handle filled containers
|
|
|
|
if (liquid != null) {
|
2015-11-23 16:15:42 +01:00
|
|
|
int qty = tank.fill(null, liquid, true);
|
2015-11-12 22:38:11 +01:00
|
|
|
|
|
|
|
if (qty != 0 && !entityplayer.capabilities.isCreativeMode) {
|
|
|
|
if (current.stackSize > 1) {
|
|
|
|
if (!entityplayer.inventory.addItemStackToInventory(FluidContainerRegistry.drainFluidContainer(current))) {
|
|
|
|
entityplayer.dropPlayerItemWithRandomChoice(FluidContainerRegistry.drainFluidContainer(current), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, consumeItem(current));
|
|
|
|
} else {
|
|
|
|
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, FluidContainerRegistry.drainFluidContainer(current));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Handle empty containers
|
|
|
|
} else {
|
2015-11-23 16:15:42 +01:00
|
|
|
FluidStack available = tank.getTankInfo(null)[0].fluid;
|
2015-11-12 22:38:11 +01:00
|
|
|
|
|
|
|
if (available != null) {
|
|
|
|
ItemStack filled = FluidContainerRegistry.fillFluidContainer(available, current);
|
|
|
|
|
|
|
|
liquid = FluidContainerRegistry.getFluidForFilledItem(filled);
|
|
|
|
|
|
|
|
if (liquid != null) {
|
|
|
|
if (!entityplayer.capabilities.isCreativeMode) {
|
|
|
|
if (current.stackSize > 1) {
|
|
|
|
if (!entityplayer.inventory.addItemStackToInventory(filled)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, consumeItem(current));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, consumeItem(current));
|
|
|
|
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, filled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 16:15:42 +01:00
|
|
|
tank.drain(null, liquid.amount, true);
|
2015-11-12 22:38:11 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (current.getItem() instanceof IFluidContainerItem) {
|
|
|
|
if (current.stackSize != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!world.isRemote) {
|
|
|
|
IFluidContainerItem container = (IFluidContainerItem) current.getItem();
|
|
|
|
FluidStack liquid = container.getFluid(current);
|
2015-11-23 16:15:42 +01:00
|
|
|
FluidStack tankLiquid = tank.getTankInfo(null)[0].fluid;
|
2015-11-12 22:38:11 +01:00
|
|
|
boolean mustDrain = liquid == null || liquid.amount == 0;
|
|
|
|
boolean mustFill = tankLiquid == null || tankLiquid.amount == 0;
|
|
|
|
if (mustDrain && mustFill) {
|
|
|
|
// Both are empty, do nothing
|
|
|
|
} else if (mustDrain || !entityplayer.isSneaking()) {
|
2015-11-23 16:15:42 +01:00
|
|
|
liquid = tank.drain(null, 1000, false);
|
2015-11-12 22:38:11 +01:00
|
|
|
int qtyToFill = container.fill(current, liquid, true);
|
2015-11-23 16:15:42 +01:00
|
|
|
tank.drain(null, qtyToFill, true);
|
2015-11-12 22:38:11 +01:00
|
|
|
} else if (mustFill || entityplayer.isSneaking()) {
|
|
|
|
if (liquid.amount > 0) {
|
2015-11-23 16:15:42 +01:00
|
|
|
int qty = tank.fill(null, liquid, false);
|
|
|
|
tank.fill(null, container.drain(current, qty, true), true);
|
2015-11-12 22:38:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack consumeItem(ItemStack stack) {
|
|
|
|
if (stack.stackSize == 1) {
|
|
|
|
if (stack.getItem().hasContainerItem(stack)) {
|
|
|
|
return stack.getItem().getContainerItem(stack);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stack.splitStack(1);
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 17:18:32 +02:00
|
|
|
}
|