PrettyPipes/src/main/java/de/ellpeck/prettypipes/blocks/pipe/PipeTileEntity.java

157 lines
5.7 KiB
Java
Raw Normal View History

2020-04-14 01:38:48 +02:00
package de.ellpeck.prettypipes.blocks.pipe;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.UpgradeItem;
2020-04-14 17:14:24 +02:00
import de.ellpeck.prettypipes.network.NetworkEdge;
import de.ellpeck.prettypipes.network.PipeItem;
import de.ellpeck.prettypipes.network.PipeNetwork;
import net.minecraft.block.BlockState;
2020-04-14 01:38:48 +02:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
2020-04-14 17:14:24 +02:00
import net.minecraft.nbt.ListNBT;
import net.minecraft.tileentity.ITickableTileEntity;
2020-04-14 01:38:48 +02:00
import net.minecraft.tileentity.TileEntity;
2020-04-14 17:14:24 +02:00
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
2020-04-14 01:38:48 +02:00
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
2020-04-14 17:14:24 +02:00
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
2020-04-14 01:38:48 +02:00
import net.minecraftforge.items.ItemStackHandler;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2020-04-14 17:14:24 +02:00
import java.util.ArrayList;
2020-04-15 18:35:00 +02:00
import java.util.Arrays;
2020-04-14 17:14:24 +02:00
import java.util.List;
2020-04-14 01:38:48 +02:00
2020-04-14 17:14:24 +02:00
public class PipeTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity {
2020-04-14 01:38:48 +02:00
public final ItemStackHandler upgrades = new ItemStackHandler(3) {
@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return stack.getItem() instanceof UpgradeItem;
}
};
2020-04-14 17:14:24 +02:00
public final List<PipeItem> items = new ArrayList<>();
2020-04-14 01:38:48 +02:00
public PipeTileEntity() {
super(Registry.pipeTileEntity);
}
@Override
public ITextComponent getDisplayName() {
return new TranslationTextComponent("container." + PrettyPipes.ID + ".pipe");
}
@Nullable
@Override
public Container createMenu(int window, PlayerInventory inv, PlayerEntity player) {
return new PipeContainer(Registry.pipeContainer, window, player, this);
}
@Override
public CompoundNBT write(CompoundNBT compound) {
compound.put("upgrades", this.upgrades.serializeNBT());
2020-04-14 17:14:24 +02:00
ListNBT list = new ListNBT();
for (PipeItem item : this.items)
list.add(item.serializeNBT());
compound.put("items", list);
2020-04-14 01:38:48 +02:00
return super.write(compound);
}
@Override
public void read(CompoundNBT compound) {
this.upgrades.deserializeNBT(compound.getCompound("upgrades"));
2020-04-14 17:14:24 +02:00
this.items.clear();
ListNBT list = compound.getList("items", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < list.size(); i++)
this.items.add(new PipeItem(list.getCompound(i)));
2020-04-14 01:38:48 +02:00
super.read(compound);
}
2020-04-14 17:14:24 +02:00
2020-04-14 21:04:41 +02:00
@Override
public CompoundNBT getUpdateTag() {
// by default, this is just writeInternal, but we
// want to sync the current pipe items on load too
return this.write(new CompoundNBT());
}
2020-04-14 17:14:24 +02:00
@Override
public void tick() {
2020-04-14 21:04:41 +02:00
if (!this.world.isAreaLoaded(this.pos, 1))
return;
2020-04-14 17:14:24 +02:00
for (int i = this.items.size() - 1; i >= 0; i--)
this.items.get(i).updateInPipe(this);
// TODO make this extraction module stuff proper
PipeNetwork network = PipeNetwork.get(this.world);
for (int i = 0; i < this.upgrades.getSlots(); i++) {
if (this.upgrades.getStackInSlot(i).getItem() != Registry.extractionUpgradeItem)
continue;
BlockState state = this.getBlockState();
for (Direction dir : Direction.values()) {
if (!state.get(PipeBlock.DIRECTIONS.get(dir)).isConnected())
continue;
IItemHandler handler = this.getItemHandler(dir);
if (handler != null) {
for (int j = 0; j < handler.getSlots(); j++) {
ItemStack stack = handler.extractItem(j, 64, true);
if (!stack.isEmpty() && network.tryInsertItem(this.pos, this.pos.offset(dir), stack)) {
handler.extractItem(j, 64, false);
return;
}
}
}
}
return;
}
}
public BlockPos getAvailableDestination(ItemStack stack) {
2020-04-14 18:51:43 +02:00
for (int i = 0; i < this.upgrades.getSlots(); i++) {
if (this.upgrades.getStackInSlot(i).getItem() == Registry.extractionUpgradeItem)
return null;
}
2020-04-14 17:14:24 +02:00
for (Direction dir : Direction.values()) {
IItemHandler handler = this.getItemHandler(dir);
if (handler == null)
continue;
if (ItemHandlerHelper.insertItem(handler, stack, true).isEmpty())
return this.pos.offset(dir);
}
return null;
}
2020-04-15 02:16:23 +02:00
// TODO module priority
public int getPriority() {
return 0;
}
2020-04-14 17:14:24 +02:00
private IItemHandler getItemHandler(Direction dir) {
BlockState state = this.getBlockState();
if (!state.get(PipeBlock.DIRECTIONS.get(dir)).isConnected())
return null;
TileEntity tile = this.world.getTileEntity(this.pos.offset(dir));
if (tile == null)
return null;
2020-04-14 18:51:43 +02:00
return tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir.getOpposite()).orElse(null);
2020-04-14 17:14:24 +02:00
}
2020-04-15 18:35:00 +02:00
public boolean isConnectedInventory(Direction dir) {
return this.getItemHandler(dir) != null;
}
public boolean isConnectedInventory() {
return Arrays.stream(Direction.values()).anyMatch(this::isConnectedInventory);
}
2020-04-14 01:38:48 +02:00
}