2016-12-18 13:27:14 +01:00
|
|
|
/*
|
|
|
|
* This file ("TileEntityItemViewerHopping.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
|
|
|
|
* http://ellpeck.de/actaddlicense
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-12-18 13:27:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2017-02-04 14:12:17 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2016-12-18 13:27:14 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
2017-02-04 14:12:17 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper;
|
2016-12-18 13:27:14 +01:00
|
|
|
import net.minecraft.block.BlockHopper;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
2017-01-18 14:26:56 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-12-18 13:27:14 +01:00
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
|
|
|
import net.minecraftforge.items.IItemHandler;
|
2017-02-04 14:12:17 +01:00
|
|
|
import org.cyclops.commoncapabilities.api.capability.itemhandler.ISlotlessItemHandler;
|
|
|
|
import org.cyclops.commoncapabilities.capability.itemhandler.SlotlessItemHandlerConfig;
|
2016-12-18 13:27:14 +01:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class TileEntityItemViewerHopping extends TileEntityItemViewer{
|
|
|
|
|
2017-02-04 14:12:17 +01:00
|
|
|
private SlotlessableItemHandlerWrapper handlerToPullFrom;
|
|
|
|
private SlotlessableItemHandlerWrapper handlerToPushTo;
|
2016-12-18 13:27:14 +01:00
|
|
|
|
|
|
|
public TileEntityItemViewerHopping(){
|
|
|
|
super("itemViewerHopping");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
|
|
|
super.updateEntity();
|
|
|
|
|
|
|
|
if(!this.world.isRemote && this.world.getTotalWorldTime()%10 == 0){
|
|
|
|
if(this.handlerToPullFrom != null){
|
2017-02-04 14:12:17 +01:00
|
|
|
WorldUtil.doItemInteraction(this.handlerToPullFrom, this.itemHandler, 4);
|
2016-12-18 13:27:14 +01:00
|
|
|
}
|
2016-12-18 14:27:10 +01:00
|
|
|
else{
|
|
|
|
if(this.world.getTotalWorldTime()%20 == 0){
|
|
|
|
List<EntityItem> items = this.world.getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(this.pos.getX(), this.pos.getY()+0.5, this.pos.getZ(), this.pos.getX()+1, this.pos.getY()+2, this.pos.getZ()+1));
|
|
|
|
if(items != null && !items.isEmpty()){
|
|
|
|
for(EntityItem item : items){
|
|
|
|
if(item != null && !item.isDead){
|
2017-02-04 14:12:17 +01:00
|
|
|
if(ActuallyAdditions.commonCapsLoaded){
|
|
|
|
Object slotless = this.itemHandler.getSlotlessHandler();
|
|
|
|
if(slotless instanceof ISlotlessItemHandler){
|
2017-06-29 18:30:02 +02:00
|
|
|
ItemStack left = ((ISlotlessItemHandler)slotless).insertItem(item.getItem(), false);
|
|
|
|
item.setItem(left);
|
2016-12-18 13:27:14 +01:00
|
|
|
|
2017-02-04 14:12:17 +01:00
|
|
|
if(!StackUtil.isValid(left)){
|
|
|
|
item.setDead();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IItemHandler handler = this.itemHandler.getNormalHandler();
|
|
|
|
if(handler != null){
|
|
|
|
for(int i = 0; i < handler.getSlots(); i++){
|
2017-06-29 18:30:02 +02:00
|
|
|
ItemStack left = handler.insertItem(i, item.getItem(), false);
|
|
|
|
item.setItem(left);
|
2017-02-04 14:12:17 +01:00
|
|
|
|
|
|
|
if(!StackUtil.isValid(left)){
|
|
|
|
item.setDead();
|
|
|
|
break;
|
|
|
|
}
|
2016-12-18 14:27:10 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-18 13:27:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 14:27:10 +01:00
|
|
|
if(this.handlerToPushTo != null){
|
2017-02-04 14:12:17 +01:00
|
|
|
WorldUtil.doItemInteraction(this.itemHandler, this.handlerToPushTo, 4);
|
2016-12-18 13:27:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void saveDataOnChangeOrWorldStart(){
|
|
|
|
super.saveDataOnChangeOrWorldStart();
|
|
|
|
|
2017-01-18 14:26:56 +01:00
|
|
|
this.handlerToPullFrom = null;
|
|
|
|
this.handlerToPushTo = null;
|
|
|
|
|
2016-12-18 13:27:14 +01:00
|
|
|
TileEntity from = this.world.getTileEntity(this.pos.offset(EnumFacing.UP));
|
2017-02-04 14:12:17 +01:00
|
|
|
if(from != null && !(from instanceof TileEntityItemViewer)){
|
|
|
|
IItemHandler normal = null;
|
|
|
|
if(from.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN)){
|
|
|
|
normal = from.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object slotless = null;
|
|
|
|
if(ActuallyAdditions.commonCapsLoaded){
|
|
|
|
if(from.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, EnumFacing.DOWN)){
|
|
|
|
slotless = from.getCapability(SlotlessItemHandlerConfig.CAPABILITY, EnumFacing.DOWN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handlerToPullFrom = new SlotlessableItemHandlerWrapper(normal, slotless);
|
2016-12-18 13:27:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IBlockState state = this.world.getBlockState(this.pos);
|
|
|
|
EnumFacing facing = state.getValue(BlockHopper.FACING);
|
|
|
|
|
2017-01-18 14:26:56 +01:00
|
|
|
BlockPos toPos = this.pos.offset(facing);
|
|
|
|
if(this.world.isBlockLoaded(toPos)){
|
|
|
|
TileEntity to = this.world.getTileEntity(toPos);
|
2017-02-04 14:12:17 +01:00
|
|
|
if(to != null && !(to instanceof TileEntityItemViewer)){
|
|
|
|
IItemHandler normal = null;
|
2017-02-04 14:18:56 +01:00
|
|
|
if(to.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite())){
|
|
|
|
normal = to.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite());
|
2017-02-04 14:12:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Object slotless = null;
|
|
|
|
if(ActuallyAdditions.commonCapsLoaded){
|
2017-02-04 14:18:56 +01:00
|
|
|
if(to.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, facing.getOpposite())){
|
|
|
|
slotless = to.getCapability(SlotlessItemHandlerConfig.CAPABILITY, facing.getOpposite());
|
2017-02-04 14:12:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handlerToPushTo = new SlotlessableItemHandlerWrapper(normal, slotless);
|
2017-01-18 14:26:56 +01:00
|
|
|
}
|
2016-12-18 14:27:44 +01:00
|
|
|
}
|
2016-12-18 13:27:14 +01:00
|
|
|
}
|
|
|
|
}
|