TechReborn/src/main/java/techreborn/blocks/BlockMachineBase.java

404 lines
16 KiB
Java
Raw Normal View History

2015-04-28 17:18:32 +02:00
package techreborn.blocks;
2015-11-08 13:15:45 +01:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockDynamicLiquid;
import net.minecraft.block.BlockStaticLiquid;
2015-04-28 17:18:32 +02:00
import net.minecraft.block.material.Material;
2015-11-25 22:03:55 +01:00
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockState;
2015-11-23 20:37:39 +01:00
import net.minecraft.block.state.IBlockState;
2015-04-28 17:18:32 +02:00
import net.minecraft.entity.EntityLivingBase;
2015-05-11 22:53:47 +02:00
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.entity.item.EntityItem;
2015-11-12 22:38:11 +01:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
2015-10-07 09:34:24 +02:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
2015-04-28 17:18:32 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-04-28 17:18:32 +02:00
import net.minecraft.tileentity.TileEntity;
2015-11-23 20:37:39 +01:00
import net.minecraft.util.BlockPos;
2015-11-23 20:19:18 +01:00
import net.minecraft.util.EnumFacing;
2015-04-28 17:18:32 +02:00
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-12 22:38:11 +01:00
import net.minecraftforge.fluids.*;
2015-11-25 22:03:55 +01:00
import reborncore.api.IBlockTextureProvider;
import reborncore.common.BaseBlock;
import reborncore.common.BaseTileBlock;
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-11-25 22:03:55 +01:00
import java.util.Iterator;
2015-11-23 20:37:39 +01:00
import java.util.List;
2015-07-02 20:51:24 +02:00
import java.util.Random;
2015-11-25 22:03:55 +01:00
public abstract class BlockMachineBase extends BaseTileBlock implements IBlockTextureProvider {
public static PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
public static PropertyBool ACTIVE = PropertyBool.create("active");
2015-04-28 17:18:32 +02:00
public BlockMachineBase(Material material) {
super(Material.rock);
setCreativeTab(TechRebornCreativeTab.instance);
setHardness(2f);
setStepSound(soundTypeMetal);
2015-11-25 22:03:55 +01:00
this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(ACTIVE, false));
}
protected BlockState createBlockState() {
FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
ACTIVE = PropertyBool.create("active");
return new BlockState(this, FACING, ACTIVE);
}
@Override
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
2015-11-11 18:00:18 +01:00
return new TileMachineBase();
}
2015-11-23 20:37:39 +01:00
@Deprecated
public void onBlockAdded(World world, int x, int y, int z) {
2015-11-23 20:37:39 +01:00
}
2015-11-23 20:37:39 +01:00
@Override
public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
super.onBlockAdded(worldIn, pos, state);
onBlockAdded(worldIn, pos.getX(), pos.getY(), pos.getZ());
2015-11-25 22:03:55 +01:00
this.setDefaultFacing(worldIn, pos, state);
}
2015-11-25 22:03:55 +01:00
private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
{
if (!worldIn.isRemote)
{
Block block = worldIn.getBlockState(pos.north()).getBlock();
Block block1 = worldIn.getBlockState(pos.south()).getBlock();
Block block2 = worldIn.getBlockState(pos.west()).getBlock();
Block block3 = worldIn.getBlockState(pos.east()).getBlock();
EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
if (enumfacing == EnumFacing.NORTH && block.isFullBlock() && !block1.isFullBlock())
{
enumfacing = EnumFacing.SOUTH;
}
2015-11-25 22:03:55 +01:00
else if (enumfacing == EnumFacing.SOUTH && block1.isFullBlock() && !block.isFullBlock())
{
enumfacing = EnumFacing.NORTH;
}
2015-11-25 22:03:55 +01:00
else if (enumfacing == EnumFacing.WEST && block2.isFullBlock() && !block3.isFullBlock())
{
enumfacing = EnumFacing.EAST;
}
2015-11-25 22:03:55 +01:00
else if (enumfacing == EnumFacing.EAST && block3.isFullBlock() && !block2.isFullBlock())
{
enumfacing = EnumFacing.WEST;
}
2015-11-25 22:03:55 +01:00
worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
}
}
2015-11-23 20:37:39 +01:00
@Deprecated
public void onBlockPlacedBy(World world, int x, int y, int z,
EntityLivingBase player, ItemStack itemstack) {
2015-11-23 20:37:39 +01:00
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
onBlockPlacedBy(worldIn, pos.getX(), pos.getY(), pos.getZ(), placer, stack);
2015-11-25 22:03:55 +01:00
setFacing(placer.getHorizontalFacing().getOpposite(), worldIn, pos);
}
public boolean canCreatureSpawn(EnumCreatureType type, World world, int x,
int y, int z) {
return false;
}
2015-11-23 20:37:39 +01:00
@Deprecated
public void breakBlock(World world, int x, int y, int z, Block block, int meta) {
2015-11-23 20:37:39 +01:00
}
2015-11-23 20:37:39 +01:00
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
super.breakBlock(worldIn, pos, state);
breakBlock(worldIn, pos.getX(), pos.getY(), pos.getZ(), state.getBlock(), 0);
dropInventory(worldIn, pos);
}
protected void dropInventory(World world, BlockPos pos) {
TileEntity tileEntity = world.getTileEntity(pos);
if (!(tileEntity instanceof IInventory)) {
return;
}
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++) {
ItemStack itemStack = inventory.getStackInSlot(i);
if (itemStack != null && itemStack.stackSize > 0) {
if (itemStack.getItem() instanceof ItemBlock) {
2015-11-23 20:37:39 +01:00
if (((ItemBlock) itemStack.getItem()).block instanceof BlockFluidBase || ((ItemBlock) itemStack.getItem()).block instanceof BlockStaticLiquid || ((ItemBlock) itemStack.getItem()).block instanceof BlockDynamicLiquid) {
return;
}
}
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;
2015-11-23 20:37:39 +01:00
EntityItem entityItem = new EntityItem(world, pos.getX() + dX, pos.getY() + dY, pos.getZ() + dZ, itemStack.copy());
if (itemStack.hasTagCompound()) {
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-10-07 09:34:24 +02:00
@Override
2015-11-23 20:37:39 +01:00
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
2015-10-07 09:34:24 +02:00
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
2015-11-23 20:37:39 +01:00
// if (Loader.isModLoaded("IC2")) {
// ItemStack stack = IC2Items.getItem(isAdvanced() ? "advancedMachine" : "machine").copy();
// stack.stackSize = 1;
// items.add(stack);
// } //TODO ic2
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 20:37:39 +01:00
public boolean rotateBlock(World world, BlockPos pos, EnumFacing axis) {
2015-11-23 16:15:42 +01:00
if (axis == null) {
2015-10-17 21:21:43 +02:00
return false;
} else {
2015-11-23 20:37:39 +01:00
TileEntity tile = world.getTileEntity(pos);
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-25 22:03:55 +01:00
//TODO fix
// world.setBlockState(pos, world.getBlockState(pos).withProperty(FACING, EnumFacing.getFront(world.getBlockState(pos).getValue(FACING)).getOpposite()));
2015-10-17 21:21:43 +02:00
return true;
}
return false;
}
}
2015-11-12 22:38:11 +01:00
2015-11-23 20:37:39 +01:00
2015-11-12 22:38:11 +01:00
@Override
2015-11-23 20:37:39 +01:00
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
if(fillBlockWithFluid(worldIn, pos, playerIn)){
2015-11-12 22:38:11 +01:00
return true;
}
2015-11-23 20:37:39 +01:00
if(onBlockActivated(worldIn, pos.getX(), pos.getY(), pos.getZ(), playerIn, side.getIndex(), hitX, hitY, hitZ)){
return true;
}
return super.onBlockActivated(worldIn, pos, state, playerIn, side, hitX, hitY, hitZ);
}
@Deprecated
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityplayer, int side, float hitX, float hitY, float hitZ) {
return false;
2015-11-12 22:38:11 +01:00
}
2015-11-23 20:37:39 +01:00
public boolean fillBlockWithFluid(World world, BlockPos pos, EntityPlayer entityplayer) {
2015-11-12 22:38:11 +01:00
ItemStack current = entityplayer.inventory.getCurrentItem();
if (current != null) {
2015-11-23 20:37:39 +01:00
TileEntity tile = world.getTileEntity(pos);
2015-11-12 22:38:11 +01:00
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-11-25 22:03:55 +01:00
@Override
public String getTextureName(IBlockState blockState, EnumFacing facing) {
if(this instanceof IRotationTexture){
IRotationTexture rotationTexture = (IRotationTexture) this;
if(getFacing(blockState) == facing){
return isActive(blockState) ? rotationTexture.getFrontOn() : rotationTexture.getFrontOff();
}
if(facing == EnumFacing.UP){
return rotationTexture.getTop();
}
if(facing == EnumFacing.DOWN){
return rotationTexture.getBottom();
}
return rotationTexture.getSide();
}
2015-11-26 22:48:30 +01:00
if(this instanceof IAdvancedRotationTexture){
IAdvancedRotationTexture advancedRotationTexture = (IAdvancedRotationTexture) this;
if(getFacing(blockState) == facing){
return advancedRotationTexture.getFront(isActive(blockState));
}
if(facing == EnumFacing.UP){
return advancedRotationTexture.getTop(isActive(blockState));
}
if(facing == EnumFacing.DOWN){
return advancedRotationTexture.getBottom(isActive(blockState));
}
return advancedRotationTexture.getSide(isActive(blockState));
}
2015-11-25 22:03:55 +01:00
return "techreborn:blocks/machine/machine_side";
}
@Override
public IBlockState getStateFromMeta(int meta) {
boolean active = false;
if(meta > 6){
active = true;
meta =- 6;
}
int facingInt = meta;
EnumFacing facing = EnumFacing.getFront(facingInt);
if(facing == EnumFacing.DOWN || facing == EnumFacing.UP){
facing = EnumFacing.NORTH;
}
return this.getDefaultState().withProperty(FACING, facing).withProperty(ACTIVE, active);
}
@Override
public int getMetaFromState(IBlockState state) {
int facingInt = state.getValue(FACING).getIndex();
int activeInt = state.getValue(ACTIVE) ? 0 : 6;
return facingInt + activeInt;
}
public boolean isActive(IBlockState state){
return state.getValue(ACTIVE);
}
public EnumFacing getFacing(IBlockState state){
return state.getValue(FACING);
}
public void setFacing(EnumFacing facing, World world, BlockPos pos){
world.setBlockState(pos, world.getBlockState(pos).withProperty(FACING, facing));
}
public void setActive(Boolean active, World world, BlockPos pos){
world.setBlockState(pos, world.getBlockState(pos).withProperty(ACTIVE, active));
}
@Override
public int amoutOfVariants() {
return 12; //six for Facing, and then 2 for eatch of them for if active
}
2015-04-28 17:18:32 +02:00
}