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

257 lines
10 KiB
Java
Raw Normal View History

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;
2016-12-05 15:09:37 +01:00
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelayEnergy.Mode;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2016-12-05 15:09:37 +01:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
2016-09-01 20:50:44 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
2016-12-05 15:09:37 +01:00
import net.minecraft.util.text.TextFormatting;
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;
2016-11-19 21:11:17 +01:00
import net.minecraftforge.fluids.capability.IFluidTankProperties;
2016-12-05 15:09:37 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2016-09-01 20:50:44 +02:00
import java.util.HashMap;
import java.util.HashSet;
2016-09-01 20:50:44 +02:00
import java.util.Map;
import java.util.Set;
2016-09-01 20:50:44 +02:00
import java.util.concurrent.ConcurrentHashMap;
2016-12-29 17:19:31 +01:00
public class TileEntityLaserRelayFluids extends TileEntityLaserRelay{
2016-09-01 20:50:44 +02:00
public final ConcurrentHashMap<EnumFacing, TileEntity> handlersAround = new ConcurrentHashMap<EnumFacing, TileEntity>();
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
2016-09-01 20:50:44 +02:00
public TileEntityLaserRelayFluids(){
super("laserRelayFluids", LaserType.FLUID);
2016-11-19 21:11:17 +01:00
for(int i = 0; i < this.fluidHandlers.length; i++){
final EnumFacing facing = EnumFacing.values()[i];
this.fluidHandlers[i] = new IFluidHandler(){
@Override
public IFluidTankProperties[] getTankProperties(){
return new IFluidTankProperties[0];
}
@Override
public int fill(FluidStack resource, boolean doFill){
return TileEntityLaserRelayFluids.this.transmitFluid(facing, resource, doFill);
}
@Override
public FluidStack drain(FluidStack resource, boolean doDrain){
return null;
}
@Override
public FluidStack drain(int maxDrain, boolean doDrain){
return null;
}
};
}
2016-09-01 20:50:44 +02:00
}
@Override
public void updateEntity(){
super.updateEntity();
if(!this.world.isRemote){
if(this.mode == Mode.INPUT_ONLY){
for(EnumFacing side : this.handlersAround.keySet()){
WorldUtil.doFluidInteraction(this.handlersAround.get(side), this, side.getOpposite(), Integer.MAX_VALUE);
}
}
}
}
@Override
public boolean shouldSaveDataOnChangeOrWorldStart(){
return true;
}
2016-09-01 20:50:44 +02:00
@Override
public void saveDataOnChangeOrWorldStart(){
Map<EnumFacing, TileEntity> old = new HashMap<EnumFacing, TileEntity>(this.handlersAround);
boolean change = false;
2016-09-01 20:50:44 +02:00
this.handlersAround.clear();
2016-09-01 20:50:44 +02:00
for(EnumFacing side : EnumFacing.values()){
BlockPos pos = this.getPos().offset(side);
if(this.world.isBlockLoaded(pos)){
TileEntity tile = this.world.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
if(tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side.getOpposite())){
this.handlersAround.put(side, tile);
TileEntity oldTile = old.get(side);
if(oldTile == null || !tile.equals(oldTile)){
change = true;
}
}
2016-09-01 20:50:44 +02:00
}
}
}
if(change || old.size() != this.handlersAround.size()){
Network network = this.getNetwork();
if(network != null){
network.changeAmount++;
}
}
2016-09-01 20:50:44 +02:00
}
@Override
2016-11-19 21:11:17 +01:00
public IFluidHandler getFluidHandler(EnumFacing facing){
return this.fluidHandlers[facing == null ? 0 : facing.ordinal()];
2016-09-01 20:50:44 +02:00
}
2016-11-19 21:11:17 +01:00
private int transmitFluid(EnumFacing from, FluidStack stack, boolean doFill){
2016-09-01 20:50:44 +02:00
int transmitted = 0;
2016-12-05 15:09:37 +01:00
if(stack != null && this.mode != Mode.OUTPUT_ONLY){
Network network = this.getNetwork();
2016-09-01 20:50:44 +02:00
if(network != null){
2016-11-19 21:11:17 +01:00
transmitted = this.transferFluidToReceiverInNeed(from, network, stack, doFill);
2016-09-01 20:50:44 +02:00
}
}
return transmitted;
}
2016-11-19 21:11:17 +01:00
private int transferFluidToReceiverInNeed(EnumFacing from, Network network, FluidStack stack, boolean doFill){
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
Set<BlockPos> alreadyChecked = new HashSet<BlockPos>();
2016-09-01 20:50:44 +02:00
Set<TileEntityLaserRelayFluids> relaysThatWork = new HashSet<TileEntityLaserRelayFluids>();
2016-09-01 20:50:44 +02:00
int totalReceiverAmount = 0;
2016-09-12 16:13:39 +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);
2016-09-01 20:50:44 +02:00
if(relayTile instanceof TileEntityLaserRelayFluids){
TileEntityLaserRelayFluids theRelay = (TileEntityLaserRelayFluids)relayTile;
2016-12-05 15:09:37 +01:00
if(theRelay.mode != Mode.INPUT_ONLY){
boolean workedOnce = false;
for(EnumFacing facing : theRelay.handlersAround.keySet()){
if(theRelay != this || facing != from){
TileEntity tile = theRelay.handlersAround.get(facing);
EnumFacing opp = facing.getOpposite();
if(tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opp)){
IFluidHandler cap = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opp);
if(cap != null && cap.fill(stack, false) > 0){
totalReceiverAmount++;
workedOnce = true;
}
}
}
2016-12-05 15:09:37 +01:00
}
if(workedOnce){
2016-12-05 15:09:37 +01:00
relaysThatWork.add(theRelay);
}
2016-09-01 20:50:44 +02:00
}
}
}
}
}
if(totalReceiverAmount > 0 && !relaysThatWork.isEmpty()){
int amountPer = stack.amount/totalReceiverAmount;
if(amountPer <= 0){
amountPer = stack.amount;
}
for(TileEntityLaserRelayFluids theRelay : relaysThatWork){
for(Map.Entry<EnumFacing, TileEntity> receiver : theRelay.handlersAround.entrySet()){
2016-09-01 20:50:44 +02:00
if(receiver != null){
EnumFacing side = receiver.getKey();
EnumFacing opp = side.getOpposite();
TileEntity tile = receiver.getValue();
if(!alreadyChecked.contains(tile.getPos())){
alreadyChecked.add(tile.getPos());
if(theRelay != this || side != from){
2016-11-19 21:11:17 +01:00
if(tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opp)){
2016-09-01 20:50:44 +02:00
IFluidHandler cap = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opp);
if(cap != null){
FluidStack copy = stack.copy();
copy.amount = amountPer;
transmitted += cap.fill(copy, doFill);
}
}
//If everything that could be transmitted was transmitted
if(transmitted >= stack.amount){
return transmitted;
}
}
}
}
}
}
}
return transmitted;
}
2016-12-05 15:09:37 +01:00
@Override
@SideOnly(Side.CLIENT)
public String getExtraDisplayString(){
2017-04-01 19:40:39 +02:00
return StringUtil.localize("info."+ModUtil.MOD_ID+".laserRelay.fluid.extra")+": "+TextFormatting.DARK_RED+StringUtil.localize(this.mode.name)+TextFormatting.RESET;
2016-12-05 15:09:37 +01:00
}
@Override
@SideOnly(Side.CLIENT)
public String getCompassDisplayString(){
return TextFormatting.GREEN+StringUtil.localize("info."+ModUtil.MOD_ID+".laserRelay.energy.display");
2016-12-05 15:09:37 +01:00
}
@Override
public void onCompassAction(EntityPlayer player){
this.mode = this.mode.getNext();
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type);
if(type != NBTType.SAVE_BLOCK){
compound.setString("Mode", this.mode.toString());
}
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readSyncableNBT(compound, type);
if(type != NBTType.SAVE_BLOCK){
String modeStrg = compound.getString("Mode");
if(modeStrg != null && !modeStrg.isEmpty()){
this.mode = Mode.valueOf(modeStrg);
}
}
}
2016-09-01 20:50:44 +02:00
}