ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockCompost.java

173 lines
7.9 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("BlockCompost.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
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.blocks;
import de.ellpeck.actuallyadditions.api.recipe.CompostRecipe;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityCompost;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
2016-03-18 23:47:22 +01:00
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
2016-05-02 17:09:23 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.TextFormatting;
2016-05-02 17:09:23 +02:00
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2016-05-02 17:09:23 +02:00
import java.util.List;
public class BlockCompost extends BlockContainerBase implements IHudDisplay{
2016-05-02 17:09:23 +02:00
protected static final AxisAlignedBB AABB_LEGS = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.3125D, 1.0D);
protected static final AxisAlignedBB AABB_WALL_NORTH = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 0.125D);
protected static final AxisAlignedBB AABB_WALL_SOUTH = new AxisAlignedBB(0.0D, 0.0D, 0.875D, 1.0D, 1.0D, 1.0D);
protected static final AxisAlignedBB AABB_WALL_EAST = new AxisAlignedBB(0.875D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D);
protected static final AxisAlignedBB AABB_WALL_WEST = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 0.125D, 1.0D, 1.0D);
2016-05-02 17:12:31 +02:00
private static final AxisAlignedBB AABB = new AxisAlignedBB(0.0625, 0, 0.0625, 1-0.0625, 11*0.0625, 1-0.0625);
2016-05-02 17:09:23 +02:00
public BlockCompost(String name){
2016-04-20 21:39:03 +02:00
super(Material.WOOD, name);
this.setHarvestLevel("axe", 0);
this.setHardness(0.5F);
this.setResistance(5.0F);
2016-04-20 21:39:03 +02:00
this.setSoundType(SoundType.WOOD);
}
2016-05-02 17:09:23 +02:00
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos){
return AABB;
2016-03-18 23:47:22 +01:00
}
2016-05-02 17:09:23 +02:00
2015-10-03 10:19:40 +02:00
@Override
public void addCollisionBoxToList(IBlockState state, World worldIn, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, Entity entityIn){
2016-05-02 17:09:23 +02:00
addCollisionBoxToList(pos, entityBox, collidingBoxes, AABB_LEGS);
addCollisionBoxToList(pos, entityBox, collidingBoxes, AABB_WALL_WEST);
addCollisionBoxToList(pos, entityBox, collidingBoxes, AABB_WALL_NORTH);
addCollisionBoxToList(pos, entityBox, collidingBoxes, AABB_WALL_EAST);
addCollisionBoxToList(pos, entityBox, collidingBoxes, AABB_WALL_SOUTH);
}
2015-10-03 10:19:40 +02:00
@Override
2016-03-18 23:47:22 +01:00
public boolean isOpaqueCube(IBlockState state){
2015-10-03 10:19:40 +02:00
return false;
}
2016-02-20 15:23:03 +01:00
@Override
2016-03-18 23:47:22 +01:00
public boolean isFullCube(IBlockState state){
2016-02-20 15:23:03 +01:00
return false;
}
@Override
2016-11-19 21:11:17 +01:00
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing f6, float f7, float f8, float f9){
ItemStack stackPlayer = player.getHeldItem(hand);
if(!world.isRemote){
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileEntityCompost){
TileEntityCompost compost = (TileEntityCompost)tile;
ItemStack slot = compost.getStackInSlot(0);
CompostRecipe recipeIn = TileEntityCompost.getRecipeForInput(slot);
if(!StackUtil.isValid(slot) || recipeIn != null){
if(StackUtil.isValid(stackPlayer)){
CompostRecipe recipeHand = TileEntityCompost.getRecipeForInput(stackPlayer);
if(recipeHand != null && (recipeIn == null || recipeIn == recipeHand)){
int maxAdd = Math.min(StackUtil.getStackSize(recipeHand.input), StackUtil.getStackSize(stackPlayer));
if(!StackUtil.isValid(slot)){
ItemStack stackToAdd = stackPlayer.copy();
stackToAdd = StackUtil.setStackSize(stackToAdd, maxAdd);
compost.setInventorySlotContents(0, stackToAdd);
player.inventory.decrStackSize(player.inventory.currentItem, maxAdd);
return true;
}
else{
ItemStack stackIn = slot.copy();
if(StackUtil.getStackSize(stackIn) < StackUtil.getStackSize(recipeHand.input)){
int sizeAdded = Math.min(maxAdd, StackUtil.getStackSize(recipeHand.input)-StackUtil.getStackSize(stackIn));
stackIn = StackUtil.addStackSize(stackIn, sizeAdded);
compost.setInventorySlotContents(0, stackIn);
player.inventory.decrStackSize(player.inventory.currentItem, sizeAdded);
return true;
}
}
}
2016-05-19 20:05:12 +02:00
}
2015-10-02 16:48:01 +02:00
}
else{
if(!StackUtil.isValid(stackPlayer)){
player.setHeldItem(hand, slot.copy());
2016-11-21 14:56:45 +01:00
compost.setInventorySlotContents(0, StackUtil.getNull());
return true;
2016-05-19 20:05:12 +02:00
}
else if(ItemUtil.canBeStacked(stackPlayer, slot)){
int addedStackSize = Math.min(StackUtil.getStackSize(slot), stackPlayer.getMaxStackSize()-StackUtil.getStackSize(stackPlayer));
ItemStack stackToAdd = stackPlayer.copy();
stackToAdd = StackUtil.addStackSize(stackToAdd, addedStackSize);
player.setHeldItem(hand, stackToAdd);
compost.decrStackSize(0, addedStackSize);
return true;
2016-05-19 20:05:12 +02:00
}
2015-10-02 16:48:01 +02:00
}
}
}
else{
return true;
}
return false;
}
@Override
public TileEntity createNewTileEntity(World world, int meta){
return new TileEntityCompost();
}
@Override
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.UNCOMMON;
}
@Override
2015-12-22 07:28:27 +01:00
@SideOnly(Side.CLIENT)
2016-07-25 22:37:14 +02:00
public void displayHud(Minecraft minecraft, EntityPlayer player, ItemStack stack, RayTraceResult posHit, ScaledResolution resolution){
2016-11-26 21:32:27 +01:00
TileEntity tile = minecraft.world.getTileEntity(posHit.getBlockPos());
if(tile instanceof TileEntityCompost){
ItemStack slot = ((TileEntityCompost)tile).getStackInSlot(0);
String strg;
if(!StackUtil.isValid(slot)){
strg = "Empty";
}
else{
strg = slot.getItem().getItemStackDisplayName(slot);
AssetUtil.renderStackToGui(slot, resolution.getScaledWidth()/2+15, resolution.getScaledHeight()/2-29, 1F);
}
2016-03-18 23:47:22 +01:00
minecraft.fontRendererObj.drawStringWithShadow(TextFormatting.YELLOW+""+TextFormatting.ITALIC+strg, resolution.getScaledWidth()/2+35, resolution.getScaledHeight()/2-25, StringUtil.DECIMAL_COLOR_WHITE);
}
}
}