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

150 lines
7.4 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;
2021-11-13 18:02:42 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
2024-03-04 21:38:02 +01:00
import net.neoforged.neoforge.capabilities.Capabilities;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.items.IItemHandler;
2021-02-27 16:33:00 +01:00
import java.util.List;
2024-03-04 21:38:02 +01:00
import java.util.Optional;
2021-02-27 16:33:00 +01:00
public class TileEntityItemInterfaceHopping extends TileEntityItemInterface {
private SlotlessableItemHandlerWrapper handlerToPullFrom;
private SlotlessableItemHandlerWrapper handlerToPushTo;
2024-03-02 21:23:08 +01:00
public TileEntityItemInterfaceHopping(BlockPos pos, BlockState state) {
super(ActuallyBlocks.ITEM_INTERFACE_HOPPING.getTileEntityType(), pos, state);
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityItemInterfaceHopping tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityItemInterfaceHopping tile) {
tile.serverTick();
// TODO: [port] validate tile is the correct way to get total game time getGameTime
if (level.getLevelData().getGameTime() % 10 == 0) {
if (tile.handlerToPullFrom != null) {
WorldUtil.doItemInteraction(tile.handlerToPullFrom, tile.itemHandler, 4);
} else {
if (level.getLevelData().getGameTime() % 20 == 0) {
//TODO hmm?
AABB axisAlignedBB = new AABB(pos.getX(), pos.getY() + 0.5, pos.getZ(), pos.getX() + 1, pos.getY() + 2, pos.getZ() + 1);
List<ItemEntity> items = level.getEntities(EntityType.ITEM, axisAlignedBB, EntitySelector.ENTITY_STILL_ALIVE);
if (items != null && !items.isEmpty()) {
for (ItemEntity item : items) {
if (item != null && item.isAlive()) {
if (ActuallyAdditions.commonCapsLoaded) {
Object slotless = tile.itemHandler.getSlotlessHandler();
// 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;
// }
// }
}
2024-03-04 21:38:02 +01:00
Optional<IItemHandler> handler = Optional.ofNullable(tile.itemHandler.getNormalHandler());
2024-03-02 21:23:08 +01:00
handler.ifPresent(cap -> {
for (int i = 0; i < cap.getSlots(); i++) {
ItemStack left = cap.insertItem(i, item.getItem(), false);
item.setItem(left);
2024-03-02 21:23:08 +01:00
if (!StackUtil.isValid(left)) {
item.discard();
break;
}
}
2024-03-02 21:23:08 +01:00
});
}
}
}
}
}
2024-03-02 21:23:08 +01:00
if (tile.handlerToPushTo != null) {
WorldUtil.doItemInteraction(tile.itemHandler, tile.handlerToPushTo, 4);
}
}
}
}
@Override
2019-05-02 09:10:29 +02:00
public void saveDataOnChangeOrWorldStart() {
super.saveDataOnChangeOrWorldStart();
this.handlerToPullFrom = null;
this.handlerToPushTo = null;
2024-03-02 21:23:08 +01:00
BlockEntity from = this.level.getBlockEntity(this.getBlockPos().relative(Direction.UP));
if (from != null && !(from instanceof TileEntityItemInterface)) {
2024-03-04 21:38:02 +01:00
IItemHandler normal = this.level.getCapability(Capabilities.ItemHandler.BLOCK, from.getBlockPos(), 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-11-13 18:02:42 +01:00
BlockState state = this.level.getBlockState(this.getBlockPos());
//Direction facing = state.getValue(BlockStateProperties.FACING);
Direction facing = Direction.DOWN; //TODO temp, facing missing
2021-11-13 18:02:42 +01:00
BlockPos toPos = this.getBlockPos().relative(facing);
if (this.level.isLoaded(toPos)) {
2024-03-02 21:23:08 +01:00
BlockEntity to = this.level.getBlockEntity(toPos);
if (to != null && !(to instanceof TileEntityItemInterface)) {
2024-03-04 21:38:02 +01:00
IItemHandler normal = this.level.getCapability(Capabilities.ItemHandler.BLOCK, to.getBlockPos(), 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);
}
}
}
}