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;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.cyclops.commoncapabilities.api.capability.itemhandler.ISlotlessItemHandler;
|
|
|
|
import org.cyclops.commoncapabilities.capability.itemhandler.SlotlessItemHandlerConfig;
|
|
|
|
|
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;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class TileEntityItemViewerHopping extends TileEntityItemViewer {
|
2016-12-18 13:27:14 +01:00
|
|
|
|
2017-02-04 14:12:17 +01:00
|
|
|
private SlotlessableItemHandlerWrapper handlerToPullFrom;
|
|
|
|
private SlotlessableItemHandlerWrapper handlerToPushTo;
|
2016-12-18 13:27:14 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public TileEntityItemViewerHopping() {
|
2016-12-18 13:27:14 +01:00
|
|
|
super("itemViewerHopping");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void updateEntity() {
|
2016-12-18 13:27:14 +01:00
|
|
|
super.updateEntity();
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
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);
|
2019-05-02 09:10:29 +02: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) {
|
|
|
|
if (ActuallyAdditions.commonCapsLoaded) {
|
2017-02-04 14:12:17 +01:00
|
|
|
Object slotless = this.itemHandler.getSlotlessHandler();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (slotless instanceof ISlotlessItemHandler) {
|
|
|
|
ItemStack left = ((ISlotlessItemHandler) slotless).insertItem(item.getItem(), false);
|
2017-06-29 18:30:02 +02:00
|
|
|
item.setItem(left);
|
2016-12-18 13:27:14 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!StackUtil.isValid(left)) {
|
2017-02-04 14:12:17 +01:00
|
|
|
item.setDead();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IItemHandler handler = this.itemHandler.getNormalHandler();
|
2019-05-02 09:10:29 +02:00
|
|
|
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
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!StackUtil.isValid(left)) {
|
2017-02-04 14:12:17 +01:00
|
|
|
item.setDead();
|
|
|
|
break;
|
|
|
|
}
|
2016-12-18 14:27:10 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-18 13:27:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02: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
|
2019-05-02 09:10:29 +02:00
|
|
|
public void saveDataOnChangeOrWorldStart() {
|
2016-12-18 13:27:14 +01:00
|
|
|
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));
|
2019-05-02 09:10:29 +02:00
|
|
|
if (from != null && !(from instanceof TileEntityItemViewer)) {
|
2017-02-04 14:12:17 +01:00
|
|
|
IItemHandler normal = null;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (from.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN)) {
|
2017-02-04 14:12:17 +01:00
|
|
|
normal = from.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object slotless = null;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (ActuallyAdditions.commonCapsLoaded) {
|
|
|
|
if (from.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, EnumFacing.DOWN)) {
|
2017-02-04 14:12:17 +01:00
|
|
|
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);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.world.isBlockLoaded(toPos)) {
|
2017-01-18 14:26:56 +01:00
|
|
|
TileEntity to = this.world.getTileEntity(toPos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (to != null && !(to instanceof TileEntityItemViewer)) {
|
2017-02-04 14:12:17 +01:00
|
|
|
IItemHandler normal = null;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (to.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite())) {
|
2017-02-04 14:18:56 +01:00
|
|
|
normal = to.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite());
|
2017-02-04 14:12:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Object slotless = null;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (ActuallyAdditions.commonCapsLoaded) {
|
|
|
|
if (to.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, facing.getOpposite())) {
|
2017-02-04 14:18:56 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|