2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("BlockContainerBase.java") is part of the Actually Additions mod for Minecraft.
|
2015-08-29 14:33:25 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
2016-05-16 22:52:27 +02:00
|
|
|
* http://ellpeck.de/actaddlicense
|
2015-08-29 14:33:25 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 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-08 20:51:03 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2017-06-29 18:30:02 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.render.IHasModel;
|
2017-03-04 22:25:40 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.ConfigValues;
|
2016-07-02 15:01:46 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityInventoryBase;
|
2016-04-20 21:39:03 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
|
2017-01-30 21:13:44 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2015-12-03 18:40:43 +01:00
|
|
|
import net.minecraft.block.Block;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.block.BlockContainer;
|
|
|
|
import net.minecraft.block.material.Material;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2015-12-13 00:54:25 +01:00
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
2015-12-13 00:54:25 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2015-12-03 20:15:07 +01:00
|
|
|
import net.minecraft.item.EnumRarity;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2016-07-02 15:01:46 +02:00
|
|
|
import net.minecraft.nbt.NBTBase;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-07-02 15:01:46 +02:00
|
|
|
import net.minecraft.nbt.NBTTagInt;
|
2015-05-25 17:00:54 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.EnumBlockRenderType;
|
2016-11-23 17:16:43 +01:00
|
|
|
import net.minecraft.util.EnumHand;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2017-01-30 21:13:44 +01:00
|
|
|
import net.minecraft.util.text.Style;
|
|
|
|
import net.minecraft.util.text.TextComponentTranslation;
|
|
|
|
import net.minecraft.util.text.TextFormatting;
|
2016-01-07 19:47:53 +01:00
|
|
|
import net.minecraft.world.IBlockAccess;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.world.World;
|
2016-06-05 12:52:59 +02:00
|
|
|
import net.minecraftforge.fluids.FluidTank;
|
|
|
|
import net.minecraftforge.fluids.FluidUtil;
|
2014-12-20 21:34:07 +01:00
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
import java.util.ArrayList;
|
2016-07-02 15:01:46 +02:00
|
|
|
import java.util.List;
|
2015-12-24 00:34:47 +01:00
|
|
|
import java.util.Random;
|
2015-12-13 00:54:25 +01:00
|
|
|
|
2017-06-29 18:30:02 +02:00
|
|
|
public abstract class BlockContainerBase extends BlockContainer implements ItemBlockBase.ICustomRarity, IHasModel{
|
2014-12-20 21:34:07 +01:00
|
|
|
|
2016-05-19 20:05:12 +02:00
|
|
|
private final String name;
|
2015-12-03 20:15:07 +01:00
|
|
|
|
|
|
|
public BlockContainerBase(Material material, String name){
|
|
|
|
super(material);
|
|
|
|
this.name = name;
|
|
|
|
|
|
|
|
this.register();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void register(){
|
2016-04-20 21:39:03 +02:00
|
|
|
ItemUtil.registerBlock(this, this.getItemBlock(), this.getBaseName(), this.shouldAddCreative());
|
2016-01-08 20:51:03 +01:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:15:07 +01:00
|
|
|
protected String getBaseName(){
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
|
2016-04-20 21:39:03 +02:00
|
|
|
protected ItemBlockBase getItemBlock(){
|
|
|
|
return new ItemBlockBase(this);
|
2015-12-03 20:15:07 +01:00
|
|
|
}
|
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
public boolean shouldAddCreative(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-29 18:30:02 +02:00
|
|
|
public void registerRendering(){
|
2016-06-01 00:37:28 +02:00
|
|
|
ActuallyAdditions.proxy.addRenderRegister(new ItemStack(this), this.getRegistryName(), "inventory");
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
2016-06-27 20:24:45 +02:00
|
|
|
@Override
|
2015-12-03 20:15:07 +01:00
|
|
|
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-11-27 11:37:55 +01:00
|
|
|
private void dropInventory(World world, BlockPos position){
|
2015-06-28 03:12:32 +02:00
|
|
|
if(!world.isRemote){
|
2016-01-08 13:31:58 +01:00
|
|
|
TileEntity aTile = world.getTileEntity(position);
|
2015-09-13 19:39:02 +02:00
|
|
|
if(aTile instanceof TileEntityInventoryBase){
|
|
|
|
TileEntityInventoryBase tile = (TileEntityInventoryBase)aTile;
|
2016-12-04 00:10:52 +01:00
|
|
|
if(tile.slots.getSlots() > 0){
|
|
|
|
for(int i = 0; i < tile.slots.getSlots(); 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-11-27 11:37:55 +01:00
|
|
|
private void dropSlotFromInventory(int i, TileEntityInventoryBase tile, World world, BlockPos pos){
|
2016-12-04 00:10:52 +01:00
|
|
|
ItemStack stack = tile.slots.getStackInSlot(i);
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(stack)){
|
2016-11-02 19:36:32 +01:00
|
|
|
float dX = world.rand.nextFloat()*0.8F+0.1F;
|
|
|
|
float dY = world.rand.nextFloat()*0.8F+0.1F;
|
|
|
|
float dZ = world.rand.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-09-13 19:39:02 +02:00
|
|
|
float factor = 0.05F;
|
2016-11-02 19:36:32 +01:00
|
|
|
entityItem.motionX = world.rand.nextGaussian()*factor;
|
|
|
|
entityItem.motionY = world.rand.nextGaussian()*factor+0.2F;
|
|
|
|
entityItem.motionZ = world.rand.nextGaussian()*factor;
|
2016-11-26 21:32:27 +01:00
|
|
|
world.spawnEntity(entityItem);
|
2015-09-13 19:39:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
public boolean tryToggleRedstone(World world, BlockPos pos, EntityPlayer player){
|
2016-05-02 07:50:13 +02:00
|
|
|
ItemStack stack = player.getHeldItemMainhand();
|
2017-03-04 22:25:40 +01:00
|
|
|
if(StackUtil.isValid(stack) && stack.getItem() == ConfigValues.itemRedstoneTorchConfigurator){
|
2016-02-01 17:49:55 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2016-07-02 15:01:46 +02:00
|
|
|
if(tile instanceof TileEntityBase){
|
|
|
|
TileEntityBase base = (TileEntityBase)tile;
|
|
|
|
if(!world.isRemote && base.isRedstoneToggle()){
|
|
|
|
base.isPulseMode = !base.isPulseMode;
|
2017-02-06 14:55:54 +01:00
|
|
|
base.markDirty();
|
|
|
|
base.sendUpdate();
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateTick(World world, BlockPos pos, IBlockState state, Random random){
|
|
|
|
if(!world.isRemote){
|
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2016-07-02 15:01:46 +02:00
|
|
|
if(tile instanceof TileEntityBase){
|
|
|
|
TileEntityBase base = (TileEntityBase)tile;
|
2016-12-18 17:28:29 +01:00
|
|
|
if(base.respondsToPulses()){
|
2016-07-02 15:01:46 +02:00
|
|
|
base.activateOnPulse();
|
|
|
|
}
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 22:14:30 +02:00
|
|
|
public void neighborsChangedCustom(World world, BlockPos pos){
|
|
|
|
this.updateRedstoneState(world, pos);
|
2016-09-09 18:40:09 +02:00
|
|
|
|
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
|
|
|
if(tile instanceof TileEntityBase){
|
|
|
|
TileEntityBase base = (TileEntityBase)tile;
|
2016-10-29 12:00:00 +02:00
|
|
|
if(base.shouldSaveDataOnChangeOrWorldStart()){
|
|
|
|
base.saveDataOnChangeOrWorldStart();
|
2016-09-09 18:40:09 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-20 22:14:30 +02:00
|
|
|
}
|
|
|
|
|
2015-12-03 18:40:43 +01:00
|
|
|
@Override
|
2016-11-19 21:11:17 +01:00
|
|
|
public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos otherPos){
|
|
|
|
super.neighborChanged(state, worldIn, pos, blockIn, otherPos);
|
2016-07-20 22:14:30 +02:00
|
|
|
this.neighborsChangedCustom(worldIn, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNeighborChange(IBlockAccess world, BlockPos pos, BlockPos neighbor){
|
|
|
|
super.onNeighborChange(world, pos, neighbor);
|
|
|
|
if(world instanceof World){
|
|
|
|
this.neighborsChangedCustom((World)world, pos);
|
|
|
|
}
|
2015-12-03 18:40:43 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 13:31:58 +01:00
|
|
|
public void updateRedstoneState(World world, BlockPos pos){
|
2015-12-24 00:34:47 +01:00
|
|
|
if(!world.isRemote){
|
2016-01-07 19:47:53 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2015-12-24 00:34:47 +01:00
|
|
|
if(tile instanceof TileEntityBase){
|
2016-07-02 15:01:46 +02:00
|
|
|
TileEntityBase base = (TileEntityBase)tile;
|
2016-01-07 19:47:53 +01:00
|
|
|
boolean powered = world.isBlockIndirectlyGettingPowered(pos) > 0;
|
2016-07-02 15:01:46 +02:00
|
|
|
boolean wasPowered = base.isRedstonePowered;
|
2016-01-02 21:36:42 +01:00
|
|
|
if(powered && !wasPowered){
|
2016-12-18 17:28:29 +01:00
|
|
|
if(base.respondsToPulses()){
|
2016-01-07 19:47:53 +01:00
|
|
|
world.scheduleUpdate(pos, this, this.tickRate(world));
|
2016-01-02 21:36:42 +01:00
|
|
|
}
|
2016-07-02 15:01:46 +02:00
|
|
|
base.setRedstonePowered(true);
|
2016-01-02 21:36:42 +01:00
|
|
|
}
|
|
|
|
else if(!powered && wasPowered){
|
2016-07-02 15:01:46 +02:00
|
|
|
base.setRedstonePowered(false);
|
2015-12-24 00:34:47 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-03 18:40:43 +01:00
|
|
|
}
|
2015-12-24 00:34:47 +01:00
|
|
|
}
|
|
|
|
|
2016-11-23 17:16:43 +01:00
|
|
|
protected boolean tryUseItemOnTank(EntityPlayer player, EnumHand hand, FluidTank tank){
|
|
|
|
ItemStack heldItem = player.getHeldItem(hand);
|
2017-06-29 13:00:11 +02:00
|
|
|
return StackUtil.isValid(heldItem) && FluidUtil.interactWithFluidHandler(player, hand, tank);
|
2016-11-23 17:16:43 +01:00
|
|
|
|
2016-05-06 23:23:29 +02:00
|
|
|
}
|
|
|
|
|
2015-12-24 00:34:47 +01:00
|
|
|
@Override
|
2016-02-01 17:49:55 +01:00
|
|
|
public void onBlockAdded(World world, BlockPos pos, IBlockState state){
|
|
|
|
this.updateRedstoneState(world, pos);
|
2015-12-03 18:40:43 +01:00
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase entity, ItemStack stack){
|
2016-07-02 15:01:46 +02:00
|
|
|
if(stack.hasTagCompound()){
|
2016-01-07 19:47:53 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2016-07-02 15:01:46 +02:00
|
|
|
if(tile instanceof TileEntityBase){
|
|
|
|
TileEntityBase base = (TileEntityBase)tile;
|
|
|
|
NBTTagCompound compound = stack.getTagCompound().getCompoundTag("Data");
|
2016-12-03 12:29:49 +01:00
|
|
|
if(compound != null){
|
|
|
|
base.readSyncableNBT(compound, TileEntityBase.NBTType.SAVE_BLOCK);
|
|
|
|
}
|
2015-12-19 10:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public void onBlockHarvested(World world, BlockPos pos, IBlockState state, EntityPlayer player){
|
2015-12-13 00:54:25 +01:00
|
|
|
if(!player.capabilities.isCreativeMode){
|
2017-01-30 21:13:44 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
|
|
|
if(tile instanceof TileEntityBase){
|
|
|
|
if(((TileEntityBase)tile).stopFromDropping){
|
|
|
|
player.sendMessage(new TextComponentTranslation("info."+ModUtil.MOD_ID+".machineBroke").setStyle(new Style().setColor(TextFormatting.RED)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:47:53 +01:00
|
|
|
this.dropBlockAsItem(world, pos, state, 0);
|
2016-04-30 20:14:01 +02:00
|
|
|
//dirty workaround because of Forge calling Item.onBlockStartBreak() twice
|
|
|
|
world.setBlockToAir(pos);
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
@Override
|
2016-03-18 23:47:22 +01:00
|
|
|
public boolean hasComparatorInputOverride(IBlockState state){
|
2015-12-19 10:30:39 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-17 18:47:46 +01:00
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
@Override
|
2016-03-18 23:47:22 +01:00
|
|
|
public int getComparatorInputOverride(IBlockState state, World world, BlockPos pos){
|
2016-01-07 19:47:53 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2016-11-28 12:51:45 +01:00
|
|
|
if(tile instanceof TileEntityBase){
|
|
|
|
return ((TileEntityBase)tile).getComparatorStrength();
|
2015-12-17 18:47:46 +01:00
|
|
|
}
|
2015-12-19 10:30:39 +01:00
|
|
|
return 0;
|
2015-12-17 18:47:46 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public ArrayList<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune){
|
2015-12-13 00:54:25 +01:00
|
|
|
ArrayList<ItemStack> drops = new ArrayList<ItemStack>();
|
|
|
|
|
2016-01-07 19:47:53 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2016-07-02 15:01:46 +02:00
|
|
|
if(tile instanceof TileEntityBase){
|
|
|
|
TileEntityBase base = (TileEntityBase)tile;
|
2016-11-27 23:04:26 +01:00
|
|
|
if(!base.stopFromDropping){
|
|
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
|
|
base.writeSyncableNBT(data, TileEntityBase.NBTType.SAVE_BLOCK);
|
|
|
|
|
|
|
|
//Remove unnecessarily saved default values to avoid unstackability
|
|
|
|
List<String> keysToRemove = new ArrayList<String>();
|
|
|
|
for(String key : data.getKeySet()){
|
|
|
|
NBTBase tag = data.getTag(key);
|
|
|
|
//Remove only ints because they are the most common ones
|
|
|
|
//Add else if below here to remove more types
|
|
|
|
if(tag instanceof NBTTagInt){
|
|
|
|
if(((NBTTagInt)tag).getInt() == 0){
|
|
|
|
keysToRemove.add(key);
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 23:04:26 +01:00
|
|
|
for(String key : keysToRemove){
|
|
|
|
data.removeTag(key);
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
2016-11-27 23:04:26 +01:00
|
|
|
ItemStack stack = new ItemStack(this.getItemDropped(state, tile.getWorld().rand, fortune), 1, this.damageDropped(state));
|
|
|
|
if(!data.hasNoTags()){
|
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
|
|
|
stack.getTagCompound().setTag("Data", data);
|
|
|
|
}
|
2015-12-15 18:51:13 +01:00
|
|
|
|
2016-11-27 23:04:26 +01:00
|
|
|
drops.add(stack);
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
2015-12-19 10:30:39 +01:00
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
return drops;
|
|
|
|
}
|
|
|
|
|
2016-01-08 17:25:31 +01:00
|
|
|
@Override
|
2016-03-18 23:47:22 +01:00
|
|
|
public EnumBlockRenderType getRenderType(IBlockState state){
|
|
|
|
return EnumBlockRenderType.MODEL;
|
2016-01-08 17:25:31 +01:00
|
|
|
}
|
2016-11-27 11:37:55 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void breakBlock(World world, BlockPos pos, IBlockState state){
|
|
|
|
if(this.shouldDropInventory(world, pos)){
|
|
|
|
this.dropInventory(world, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
super.breakBlock(world, pos, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean shouldDropInventory(World world, BlockPos pos){
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-20 21:34:07 +01:00
|
|
|
}
|