ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/base/BlockContainerBase.java

281 lines
10 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("BlockContainerBase.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.blocks.base;
2014-12-20 21:34:07 +01:00
2016-01-07 18:20:59 +01:00
import de.ellpeck.actuallyadditions.api.Position;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.creative.CreativeTab;
import de.ellpeck.actuallyadditions.mod.tile.*;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.Util;
import net.minecraft.block.Block;
2014-12-20 21:34:07 +01:00
import net.minecraft.block.BlockContainer;
import net.minecraft.block.BlockRedstoneTorch;
2014-12-20 21:34:07 +01:00
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
2016-01-07 18:20:59 +01:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
2014-12-20 21:34:07 +01:00
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
2015-05-25 17:00:54 +02:00
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.EnumRarity;
2014-12-20 21:34:07 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-05-25 17:00:54 +02:00
import net.minecraft.tileentity.TileEntity;
2016-01-07 18:20:59 +01:00
import net.minecraft.util.BlockPos;
import net.minecraft.world.IBlockAccess;
2014-12-20 21:34:07 +01:00
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidStack;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.common.registry.GameRegistry;
2014-12-20 21:34:07 +01:00
import java.util.ArrayList;
import java.util.Random;
2014-12-20 21:34:07 +01:00
public abstract class BlockContainerBase extends BlockContainer{
private String name;
public BlockContainerBase(Material material, String name){
super(material);
this.name = name;
this.register();
}
private void register(){
2016-01-07 18:20:59 +01:00
this.setUnlocalizedName(ModUtil.MOD_ID_LOWER+"."+this.getBaseName());
GameRegistry.registerBlock(this, this.getItemBlock(), this.getBaseName());
if(this.shouldAddCreative()){
this.setCreativeTab(CreativeTab.instance);
}
else{
this.setCreativeTab(null);
}
}
protected String getBaseName(){
return this.name;
}
protected Class<? extends ItemBlockBase> getItemBlock(){
return ItemBlockBase.class;
}
2015-12-19 10:30:39 +01:00
public boolean shouldAddCreative(){
return true;
}
public EnumRarity getRarity(ItemStack stack){
2016-01-07 18:20:59 +01:00
return EnumRarity.COMMON;
2014-12-20 21:34:07 +01:00
}
2016-01-07 18:20:59 +01:00
public void dropInventory(World world, Position position){
2015-06-28 03:12:32 +02:00
if(!world.isRemote){
2016-01-07 18:20:59 +01:00
TileEntity aTile = position.getTileEntity(world);
if(aTile instanceof TileEntityInventoryBase){
TileEntityInventoryBase tile = (TileEntityInventoryBase)aTile;
if(tile.getSizeInventory() > 0){
for(int i = 0; i < tile.getSizeInventory(); i++){
2016-01-07 18:20:59 +01:00
this.dropSlotFromInventory(i, tile, world, position);
2015-06-18 13:14:57 +02:00
}
}
2014-12-20 21:34:07 +01:00
}
}
}
2015-05-25 17:00:54 +02:00
2016-01-07 18:20:59 +01:00
public void dropSlotFromInventory(int i, TileEntityInventoryBase tile, World world, Position pos){
ItemStack stack = tile.getStackInSlot(i);
if(stack != null && stack.stackSize > 0){
2015-11-22 18:58:23 +01:00
float dX = Util.RANDOM.nextFloat()*0.8F+0.1F;
float dY = Util.RANDOM.nextFloat()*0.8F+0.1F;
float dZ = Util.RANDOM.nextFloat()*0.8F+0.1F;
2016-01-07 18:20:59 +01:00
EntityItem entityItem = new EntityItem(world, pos.getX()+dX, pos.getY()+dY, pos.getZ()+dZ, stack.copy());
2015-10-02 16:48:01 +02:00
if(stack.hasTagCompound()){
entityItem.getEntityItem().setTagCompound((NBTTagCompound)stack.getTagCompound().copy());
}
float factor = 0.05F;
2015-11-22 18:58:23 +01:00
entityItem.motionX = Util.RANDOM.nextGaussian()*factor;
entityItem.motionY = Util.RANDOM.nextGaussian()*factor+0.2F;
entityItem.motionZ = Util.RANDOM.nextGaussian()*factor;
world.spawnEntityInWorld(entityItem);
}
tile.setInventorySlotContents(i, null);
}
@Override
public void onNeighborBlockChange(World world, BlockPos pos, IBlockState state, Block neighborBlock){
this.updateRedstoneState(world, Position.fromBlockPos(pos));
}
public void updateRedstoneState(World world, Position pos){
if(!world.isRemote){
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileEntityBase){
boolean powered = world.isBlockIndirectlyGettingPowered(pos) > 0;
boolean wasPowered = ((TileEntityBase)tile).isRedstonePowered;
if(powered && !wasPowered){
if(tile instanceof IRedstoneToggle && ((IRedstoneToggle)tile).isPulseMode()){
world.scheduleUpdate(pos, this, this.tickRate(world));
}
((TileEntityBase)tile).setRedstonePowered(true);
}
else if(!powered && wasPowered){
((TileEntityBase)tile).setRedstonePowered(false);
}
}
}
}
@Override
public void updateTick(World world, BlockPos pos, IBlockState state, Random random){
if(!world.isRemote){
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof IRedstoneToggle && ((IRedstoneToggle)tile).isPulseMode()){
((IRedstoneToggle)tile).activateOnPulse();
}
}
}
2015-12-19 10:30:39 +01:00
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase entity, ItemStack stack){
2015-12-19 10:30:39 +01:00
if(stack.getTagCompound() != null){
TileEntity tile = world.getTileEntity(pos);
2015-12-19 10:30:39 +01:00
if(tile instanceof IEnergySaver){
((IEnergySaver)tile).setEnergy(stack.getTagCompound().getInteger("Energy"));
}
if(tile instanceof IFluidSaver){
int amount = stack.getTagCompound().getInteger("FluidAmount");
if(amount > 0){
FluidStack[] fluids = new FluidStack[amount];
for(int i = 0; i < amount; i++){
NBTTagCompound compound = stack.getTagCompound().getCompoundTag("Fluid"+i);
if(compound != null){
fluids[i] = FluidStack.loadFluidStackFromNBT(compound);
}
}
((IFluidSaver)tile).setFluids(fluids);
}
}
}
}
@Override
public void onBlockHarvested(World world, BlockPos pos, IBlockState state, EntityPlayer player){
if(!player.capabilities.isCreativeMode){
this.dropBlockAsItem(world, pos, state, 0);
}
}
2015-12-19 10:30:39 +01:00
@Override
public boolean hasComparatorInputOverride(){
return true;
}
2015-12-19 10:30:39 +01:00
@Override
public int getComparatorInputOverride(World world, BlockPos pos){
TileEntity tile = world.getTileEntity(pos);
2015-12-19 10:30:39 +01:00
if(tile instanceof IInventory){
return Container.calcRedstoneFromInventory((IInventory)tile);
}
2015-12-19 10:30:39 +01:00
return 0;
}
@Override
public ArrayList<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune){
ArrayList<ItemStack> drops = new ArrayList<ItemStack>();
TileEntity tile = world.getTileEntity(pos);
if(tile != null){
ItemStack stack = new ItemStack(this.getItemDropped(state, Util.RANDOM, fortune), 1, this.damageDropped(state));
if(tile instanceof IEnergySaver){
int energy = ((IEnergySaver)tile).getEnergy();
if(energy > 0){
if(stack.getTagCompound() == null){
stack.setTagCompound(new NBTTagCompound());
}
stack.getTagCompound().setInteger("Energy", energy);
}
}
if(tile instanceof IFluidSaver){
FluidStack[] fluids = ((IFluidSaver)tile).getFluids();
if(fluids != null && fluids.length > 0){
if(stack.getTagCompound() == null){
stack.setTagCompound(new NBTTagCompound());
}
stack.getTagCompound().setInteger("FluidAmount", fluids.length);
for(int i = 0; i < fluids.length; i++){
if(fluids[i] != null && fluids[i].amount > 0){
NBTTagCompound compound = new NBTTagCompound();
fluids[i].writeToNBT(compound);
stack.getTagCompound().setTag("Fluid"+i, compound);
}
}
}
}
drops.add(stack);
}
2015-12-19 10:30:39 +01:00
return drops;
}
@Override
public void onBlockAdded(World world, BlockPos pos, IBlockState state){
this.updateRedstoneState(world, Position.fromBlockPos(pos));
2015-12-19 10:30:39 +01:00
}
public boolean tryToggleRedstone(World world, Position pos, EntityPlayer player){
2015-12-19 10:30:39 +01:00
ItemStack stack = player.getCurrentEquippedItem();
if(stack != null && Block.getBlockFromItem(stack.getItem()) instanceof BlockRedstoneTorch){
TileEntity tile = pos.getTileEntity(world);
2015-12-19 10:30:39 +01:00
if(tile instanceof IRedstoneToggle){
if(!world.isRemote){
((IRedstoneToggle)tile).toggle(!((IRedstoneToggle)tile).isPulseMode());
tile.markDirty();
if(tile instanceof TileEntityBase){
((TileEntityBase)tile).sendUpdate();
}
}
2015-12-19 10:30:39 +01:00
return true;
}
}
2015-12-19 10:30:39 +01:00
return false;
}
public static final PropertyInteger META = PropertyInteger.create("metadata", 0, 15);
@Override
protected BlockState createBlockState(){
return new BlockState(this, META);
}
@Override
public IBlockState getStateFromMeta(int meta){
return getDefaultState().withProperty(META, meta);
}
@Override
public int getMetaFromState(IBlockState state){
return state.getValue(META);
}
2014-12-20 21:34:07 +01:00
}