2016-09-01 20:50:44 +02:00
|
|
|
/*
|
|
|
|
* This file ("TileEntityLaserRelayFluids.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-09-01 20:50:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2016-09-12 16:13:39 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
|
2016-09-01 20:50:44 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.LaserType;
|
|
|
|
import de.ellpeck.actuallyadditions.api.laser.Network;
|
2018-05-10 11:38:58 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2016-12-05 15:09:37 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelayEnergy.Mode;
|
2017-02-16 18:47:14 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
|
2017-01-14 00:14:26 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2016-09-01 20:50:44 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2016-09-01 20:50:44 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-12-05 15:09:37 +01:00
|
|
|
import net.minecraft.util.text.TextFormatting;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-09-01 20:50:44 +02:00
|
|
|
import net.minecraftforge.fluids.FluidStack;
|
|
|
|
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
|
|
|
import net.minecraftforge.fluids.capability.IFluidHandler;
|
2021-02-26 22:15:48 +01:00
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2016-09-01 20:50:44 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class TileEntityLaserRelayFluids extends TileEntityLaserRelay {
|
2016-09-01 20:50:44 +02:00
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
public final ConcurrentHashMap<Direction, TileEntity> handlersAround = new ConcurrentHashMap<>();
|
2016-11-19 21:11:17 +01:00
|
|
|
private final IFluidHandler[] fluidHandlers = new IFluidHandler[6];
|
2016-12-18 17:50:31 +01:00
|
|
|
private Mode mode = Mode.BOTH;
|
2016-11-19 21:11:17 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public TileEntityLaserRelayFluids() {
|
2021-02-27 16:33:00 +01:00
|
|
|
super(ActuallyTiles.LASERRELAYFLUIDS_TILE.get(), LaserType.FLUID);
|
2016-11-19 21:11:17 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int i = 0; i < this.fluidHandlers.length; i++) {
|
2021-02-27 13:24:45 +01:00
|
|
|
Direction facing = Direction.values()[i];
|
2021-02-27 16:33:00 +01:00
|
|
|
|
|
|
|
// TODO: [port] this might just not work due to the new contract
|
2019-05-02 09:10:29 +02:00
|
|
|
this.fluidHandlers[i] = new IFluidHandler() {
|
2016-11-19 21:11:17 +01:00
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public int getTanks() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public FluidStack getFluidInTank(int tank) {
|
|
|
|
return FluidStack.EMPTY;
|
2016-11-19 21:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public int getTankCapacity(int tank) {
|
|
|
|
return 0;
|
2016-11-19 21:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public boolean isFluidValid(int tank, @Nonnull FluidStack stack) {
|
|
|
|
return false;
|
2016-11-19 21:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public int fill(FluidStack resource, FluidAction action) {
|
|
|
|
return TileEntityLaserRelayFluids.this.transmitFluid(facing, resource, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public FluidStack drain(FluidStack resource, FluidAction action) {
|
|
|
|
return FluidStack.EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public FluidStack drain(int maxDrain, FluidAction action) {
|
|
|
|
return FluidStack.EMPTY;
|
2016-11-19 21:11:17 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-09-01 20:50:44 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 00:14:26 +01:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void updateEntity() {
|
2017-01-14 00:14:26 +01:00
|
|
|
super.updateEntity();
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!this.world.isRemote) {
|
|
|
|
if (this.mode == Mode.INPUT_ONLY) {
|
2021-02-27 13:24:45 +01:00
|
|
|
for (Direction side : this.handlersAround.keySet()) {
|
2017-01-14 00:14:26 +01:00
|
|
|
WorldUtil.doFluidInteraction(this.handlersAround.get(side), this, side.getOpposite(), Integer.MAX_VALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:40:09 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean shouldSaveDataOnChangeOrWorldStart() {
|
2016-09-09 18:40:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-01 20:50:44 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void saveDataOnChangeOrWorldStart() {
|
2021-02-27 13:24:45 +01:00
|
|
|
Map<Direction, TileEntity> old = new HashMap<>(this.handlersAround);
|
2016-11-03 10:49:41 +01:00
|
|
|
boolean change = false;
|
2016-09-01 20:50:44 +02:00
|
|
|
|
2017-01-14 00:14:26 +01:00
|
|
|
this.handlersAround.clear();
|
2021-02-27 13:24:45 +01:00
|
|
|
for (Direction side : Direction.values()) {
|
2016-09-01 20:50:44 +02:00
|
|
|
BlockPos pos = this.getPos().offset(side);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.world.isBlockLoaded(pos)) {
|
2017-01-18 14:26:56 +01:00
|
|
|
TileEntity tile = this.world.getTileEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile != null && !(tile instanceof TileEntityLaserRelay)) {
|
2021-02-27 16:33:00 +01:00
|
|
|
if (tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side.getOpposite()).isPresent()) {
|
2017-01-18 14:26:56 +01:00
|
|
|
this.handlersAround.put(side, tile);
|
|
|
|
|
|
|
|
TileEntity oldTile = old.get(side);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (oldTile == null || !tile.equals(oldTile)) {
|
2017-01-18 14:26:56 +01:00
|
|
|
change = true;
|
|
|
|
}
|
2016-11-03 10:49:41 +01:00
|
|
|
}
|
2016-09-01 20:50:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (change || old.size() != this.handlersAround.size()) {
|
2017-01-18 14:26:56 +01:00
|
|
|
Network network = this.getNetwork();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (network != null) {
|
2016-11-03 10:49:41 +01:00
|
|
|
network.changeAmount++;
|
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
}
|
2016-09-01 20:50:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
// TODO: [port] super hacky, find better way of handling this.
|
2016-09-01 20:50:44 +02:00
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IFluidHandler> getFluidHandler(Direction facing) {
|
|
|
|
return LazyOptional.of(() -> this.fluidHandlers[facing == null
|
2021-02-26 22:15:48 +01:00
|
|
|
? 0
|
2021-02-27 16:33:00 +01:00
|
|
|
: facing.ordinal()]);
|
2016-09-01 20:50:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
private int transmitFluid(Direction from, FluidStack stack, IFluidHandler.FluidAction action) {
|
2016-09-01 20:50:44 +02:00
|
|
|
int transmitted = 0;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (stack != null && this.mode != Mode.OUTPUT_ONLY) {
|
2017-01-18 14:26:56 +01:00
|
|
|
Network network = this.getNetwork();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (network != null) {
|
2021-02-27 16:33:00 +01:00
|
|
|
transmitted = this.transferFluidToReceiverInNeed(from, network, stack, action);
|
2016-09-01 20:50:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return transmitted;
|
|
|
|
}
|
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
private int transferFluidToReceiverInNeed(Direction from, Network network, FluidStack stack, IFluidHandler.FluidAction action) {
|
2016-09-01 20:50:44 +02:00
|
|
|
int transmitted = 0;
|
|
|
|
//Keeps track of all the Laser Relays and Energy Acceptors that have been checked already to make nothing run multiple times
|
2019-02-27 19:53:05 +01:00
|
|
|
Set<BlockPos> alreadyChecked = new HashSet<>();
|
2016-09-01 20:50:44 +02:00
|
|
|
|
2019-02-27 19:53:05 +01:00
|
|
|
Set<TileEntityLaserRelayFluids> relaysThatWork = new HashSet<>();
|
2016-09-01 20:50:44 +02:00
|
|
|
int totalReceiverAmount = 0;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (IConnectionPair pair : network.connections) {
|
|
|
|
for (BlockPos relay : pair.getPositions()) {
|
|
|
|
if (relay != null && this.world.isBlockLoaded(relay) && !alreadyChecked.contains(relay)) {
|
2016-09-01 20:50:44 +02:00
|
|
|
alreadyChecked.add(relay);
|
2016-11-26 21:32:27 +01:00
|
|
|
TileEntity relayTile = this.world.getTileEntity(relay);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (relayTile instanceof TileEntityLaserRelayFluids) {
|
|
|
|
TileEntityLaserRelayFluids theRelay = (TileEntityLaserRelayFluids) relayTile;
|
|
|
|
if (theRelay.mode != Mode.INPUT_ONLY) {
|
2016-12-18 11:53:14 +01:00
|
|
|
boolean workedOnce = false;
|
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
for (Direction facing : theRelay.handlersAround.keySet()) {
|
2019-05-02 09:10:29 +02:00
|
|
|
if (theRelay != this || facing != from) {
|
2017-01-14 00:14:26 +01:00
|
|
|
TileEntity tile = theRelay.handlersAround.get(facing);
|
2021-02-27 13:24:45 +01:00
|
|
|
Direction opp = facing.getOpposite();
|
2021-02-27 16:33:00 +01:00
|
|
|
|
|
|
|
boolean received = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opp).map(cap -> cap.fill(stack, IFluidHandler.FluidAction.SIMULATE) > 0).orElse(false);
|
|
|
|
if (received) {
|
|
|
|
totalReceiverAmount++;
|
|
|
|
workedOnce = true;
|
2016-12-18 11:53:14 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
2016-12-01 18:16:01 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (workedOnce) {
|
2016-12-05 15:09:37 +01:00
|
|
|
relaysThatWork.add(theRelay);
|
|
|
|
}
|
2016-09-01 20:50:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (totalReceiverAmount > 0 && !relaysThatWork.isEmpty()) {
|
2021-02-27 16:33:00 +01:00
|
|
|
int amountPer = stack.getAmount() / totalReceiverAmount <= 0
|
|
|
|
? stack.getAmount() / totalReceiverAmount
|
|
|
|
: stack.getAmount();
|
2016-09-01 20:50:44 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (TileEntityLaserRelayFluids theRelay : relaysThatWork) {
|
2021-02-27 13:24:45 +01:00
|
|
|
for (Map.Entry<Direction, TileEntity> receiver : theRelay.handlersAround.entrySet()) {
|
2019-05-02 09:10:29 +02:00
|
|
|
if (receiver != null) {
|
2021-02-27 13:24:45 +01:00
|
|
|
Direction side = receiver.getKey();
|
|
|
|
Direction opp = side.getOpposite();
|
2016-09-01 20:50:44 +02:00
|
|
|
TileEntity tile = receiver.getValue();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!alreadyChecked.contains(tile.getPos())) {
|
2016-09-01 20:50:44 +02:00
|
|
|
alreadyChecked.add(tile.getPos());
|
2019-05-02 09:10:29 +02:00
|
|
|
if (theRelay != this || side != from) {
|
2021-02-27 16:33:00 +01:00
|
|
|
transmitted += tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opp).map(cap -> {
|
|
|
|
int trans = 0;
|
|
|
|
FluidStack copy = stack.copy();
|
|
|
|
copy.setAmount(amountPer);
|
|
|
|
trans += cap.fill(copy, action);
|
|
|
|
return trans;
|
|
|
|
}).orElse(0);
|
2016-09-01 20:50:44 +02:00
|
|
|
|
|
|
|
//If everything that could be transmitted was transmitted
|
2021-02-27 16:33:00 +01:00
|
|
|
if (transmitted >= stack.getAmount()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
return transmitted;
|
|
|
|
}
|
2016-09-01 20:50:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return transmitted;
|
|
|
|
}
|
2016-12-05 15:09:37 +01:00
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-05-02 09:10:29 +02:00
|
|
|
public String getExtraDisplayString() {
|
|
|
|
return StringUtil.localize("info." + ActuallyAdditions.MODID + ".laserRelay.fluid.extra") + ": " + TextFormatting.DARK_RED + StringUtil.localize(this.mode.name) + TextFormatting.RESET;
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-05-02 09:10:29 +02:00
|
|
|
public String getCompassDisplayString() {
|
|
|
|
return TextFormatting.GREEN + StringUtil.localize("info." + ActuallyAdditions.MODID + ".laserRelay.energy.display");
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void onCompassAction(PlayerEntity player) {
|
2016-12-05 15:09:37 +01:00
|
|
|
this.mode = this.mode.getNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-12-05 15:09:37 +01:00
|
|
|
super.writeSyncableNBT(compound, type);
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putString("Mode", this.mode.toString());
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-12-05 15:09:37 +01:00
|
|
|
super.readSyncableNBT(compound, type);
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2016-12-05 15:09:37 +01:00
|
|
|
String modeStrg = compound.getString("Mode");
|
2019-05-02 09:10:29 +02:00
|
|
|
if (modeStrg != null && !modeStrg.isEmpty()) {
|
2016-12-05 15:09:37 +01:00
|
|
|
this.mode = Mode.valueOf(modeStrg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 20:50:44 +02:00
|
|
|
}
|