ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/misc/DispenserHandlerFillBucket.java

84 lines
3.5 KiB
Java
Raw Normal View History

/*
* This file ("DispenserHandlerFillBucket.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/
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.misc;
2016-01-08 13:31:58 +01:00
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockDispenser;
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
import net.minecraft.dispenser.IBlockSource;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityDispenser;
import net.minecraft.util.EnumFacing;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.IFluidBlock;
public class DispenserHandlerFillBucket extends BehaviorDefaultDispenseItem{
@Override
public ItemStack dispenseStack(IBlockSource source, ItemStack emptyBucket){
EnumFacing facing = BlockDispenser.getFacing(source.getBlockMetadata());
int x = source.getBlockTileEntity().getPos().getX()+facing.getFrontOffsetX();
int y = source.getBlockTileEntity().getPos().getY()+facing.getFrontOffsetY();
int z = source.getBlockTileEntity().getPos().getZ()+facing.getFrontOffsetZ();
ItemStack filledBucket = this.tryFillBucket(source, x, y, z, emptyBucket);
//Bucket couldn't be filled
if(filledBucket == null){
return new BehaviorDefaultDispenseItem().dispense(source, emptyBucket);
}
emptyBucket.stackSize--;
//Only one bucket was there -> new bucket gets placed in slot
if(emptyBucket.stackSize <= 0){
emptyBucket = filledBucket.copy();
}
//Not enough space for the bucket in the inventory?
else if(((TileEntityDispenser)source.getBlockTileEntity()).addItemStack(filledBucket.copy()) < 0){
new BehaviorDefaultDispenseItem().dispense(source, filledBucket.copy());
}
2015-10-12 04:31:35 +02:00
//Filled Bucket or Empty Buckets because either they weren't filled or the full one was dispensed out because of missing space
return emptyBucket;
}
private ItemStack tryFillBucket(IBlockSource source, int x, int y, int z, ItemStack bucket){
2016-01-08 13:31:58 +01:00
BlockPos pos = new BlockPos(x, y, z);
Block block = PosUtil.getBlock(pos, source.getWorld());
2016-04-20 21:39:03 +02:00
if(block == Blocks.WATER || block == Blocks.FLOWING_WATER){
2016-01-08 13:31:58 +01:00
if(PosUtil.getMetadata(pos, source.getWorld()) == 0){
source.getWorld().setBlockToAir(pos);
2016-04-20 21:39:03 +02:00
return new ItemStack(Items.WATER_BUCKET);
}
}
2016-04-20 21:39:03 +02:00
else if(block == Blocks.LAVA || block == Blocks.FLOWING_LAVA){
2016-01-08 13:31:58 +01:00
if(PosUtil.getMetadata(pos, source.getWorld()) == 0){
source.getWorld().setBlockToAir(pos);
2016-04-20 21:39:03 +02:00
return new ItemStack(Items.LAVA_BUCKET);
}
}
else if(block instanceof IFluidBlock && ((IFluidBlock)block).canDrain(source.getWorld(), pos)){
ItemStack stack = FluidContainerRegistry.fillFluidContainer(((IFluidBlock)block).drain(source.getWorld(), pos, false), bucket);
if(stack != null){
((IFluidBlock)block).drain(source.getWorld(), pos, true);
return stack;
}
}
return null;
}
}