ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewerHopping.java

137 lines
6.5 KiB
Java
Raw Normal View History

/*
* 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
*/
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper;
2021-02-27 16:33:00 +01:00
import net.minecraft.block.BlockState;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
2021-02-27 16:33:00 +01:00
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
2021-02-27 16:33:00 +01:00
import java.util.List;
2019-05-02 09:10:29 +02:00
public class TileEntityItemViewerHopping extends TileEntityItemViewer {
private SlotlessableItemHandlerWrapper handlerToPullFrom;
private SlotlessableItemHandlerWrapper handlerToPushTo;
2019-05-02 09:10:29 +02:00
public TileEntityItemViewerHopping() {
2021-02-27 16:33:00 +01:00
super(ActuallyTiles.ITEMVIEWERHOPPING_TILE.get());
}
@Override
2019-05-02 09:10:29 +02:00
public void updateEntity() {
super.updateEntity();
2021-02-27 16:33:00 +01:00
// TODO: [port] validate this is the correct way to get total game time getGameTime
if (!this.world.isRemote && this.world.getWorldInfo().getGameTime() % 10 == 0) {
2019-05-02 09:10:29 +02:00
if (this.handlerToPullFrom != null) {
WorldUtil.doItemInteraction(this.handlerToPullFrom, this.itemHandler, 4);
2019-05-02 09:10:29 +02:00
} else {
2021-02-27 16:33:00 +01:00
if (this.world.getWorldInfo().getGameTime() % 20 == 0) {
List<ItemEntity> items = this.world.getEntitiesWithinAABB(ItemEntity.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));
2019-05-02 09:10:29 +02:00
if (items != null && !items.isEmpty()) {
2021-02-27 16:33:00 +01:00
for (ItemEntity item : items) {
if (item != null && item.isAlive()) {
2019-05-02 09:10:29 +02:00
if (ActuallyAdditions.commonCapsLoaded) {
Object slotless = this.itemHandler.getSlotlessHandler();
2021-02-27 16:33:00 +01:00
// TODO: [port] add back?
// if (slotless instanceof ISlotlessItemHandler) {
// ItemStack left = ((ISlotlessItemHandler) slotless).insertItem(item.getItem(), false);
// item.setItem(left);
//
// if (!StackUtil.isValid(left)) {
// item.remove();
// continue;
// }
// }
}
2021-02-27 16:33:00 +01:00
LazyOptional<IItemHandler> handler = this.itemHandler.getNormalHandler();
handler.ifPresent(cap -> {
for (int i = 0; i < cap.getSlots(); i++) {
ItemStack left = cap.insertItem(i, item.getItem(), false);
item.setItem(left);
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(left)) {
2021-02-27 16:33:00 +01:00
item.remove();
break;
}
}
2021-02-27 16:33:00 +01:00
});
}
}
}
}
}
2019-05-02 09:10:29 +02:00
if (this.handlerToPushTo != null) {
WorldUtil.doItemInteraction(this.itemHandler, this.handlerToPushTo, 4);
}
}
}
@Override
2019-05-02 09:10:29 +02:00
public void saveDataOnChangeOrWorldStart() {
super.saveDataOnChangeOrWorldStart();
this.handlerToPullFrom = null;
this.handlerToPushTo = null;
TileEntity from = this.world.getTileEntity(this.pos.offset(Direction.UP));
2019-05-02 09:10:29 +02:00
if (from != null && !(from instanceof TileEntityItemViewer)) {
2021-02-27 16:33:00 +01:00
LazyOptional<IItemHandler> normal = from.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.DOWN);
Object slotless = null;
2021-02-27 16:33:00 +01:00
// TODO: [port] add back
// if (ActuallyAdditions.commonCapsLoaded) {
// if (from.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, Direction.DOWN)) {
// slotless = from.getCapability(SlotlessItemHandlerConfig.CAPABILITY, Direction.DOWN);
// }
// }
this.handlerToPullFrom = new SlotlessableItemHandlerWrapper(normal, slotless);
}
2021-02-26 22:15:48 +01:00
BlockState state = this.world.getBlockState(this.pos);
2021-02-27 16:33:00 +01:00
Direction facing = state.get(BlockStateProperties.FACING);
BlockPos toPos = this.pos.offset(facing);
2019-05-02 09:10:29 +02:00
if (this.world.isBlockLoaded(toPos)) {
TileEntity to = this.world.getTileEntity(toPos);
2019-05-02 09:10:29 +02:00
if (to != null && !(to instanceof TileEntityItemViewer)) {
2021-02-27 16:33:00 +01:00
LazyOptional<IItemHandler> normal = to.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite());
Object slotless = null;
2021-02-27 16:33:00 +01:00
// TODO: [port] Add back
// if (ActuallyAdditions.commonCapsLoaded) {
// if (to.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, facing.getOpposite())) {
// slotless = to.getCapability(SlotlessItemHandlerConfig.CAPABILITY, facing.getOpposite());
// }
// }
this.handlerToPushTo = new SlotlessableItemHandlerWrapper(normal, slotless);
}
}
}
}