diff --git a/src/main/java/nova/core/component/fluid/FluidHandler.java b/src/main/java/nova/core/component/fluid/FluidHandler.java index ec170a75b..847aa999c 100644 --- a/src/main/java/nova/core/component/fluid/FluidHandler.java +++ b/src/main/java/nova/core/component/fluid/FluidHandler.java @@ -45,6 +45,10 @@ public static FluidHandler singleTank(int capacity, Predicate fluidFilter return FluidHandlerSimple.simpleSingleTank(capacity, fluidFilter); } + public static FluidHandler multiTank(Tank... tanks) { + return new FluidHandlerSimple(false, tanks); + } + @Override public int getFluidAmount() { return getTanks().stream().mapToInt(Tank::getFluidAmount).sum(); diff --git a/src/main/java/nova/core/component/fluid/FluidHandlerSimple.java b/src/main/java/nova/core/component/fluid/FluidHandlerSimple.java index 3a4b39bf0..26bbd18e4 100644 --- a/src/main/java/nova/core/component/fluid/FluidHandlerSimple.java +++ b/src/main/java/nova/core/component/fluid/FluidHandlerSimple.java @@ -20,7 +20,6 @@ package nova.core.component.fluid; -import nova.core.component.Component; import nova.core.component.SidedComponent; import nova.core.retention.Data; import nova.core.retention.Storable; @@ -79,7 +78,7 @@ public FluidHandlerSimple(boolean resizable, Tank... tanks) { @Override public Set getTanks() { - return tanks; + return Collections.unmodifiableSet(tanks); } @Override diff --git a/src/main/java/nova/core/component/fluid/FluidHandlerWrapper.java b/src/main/java/nova/core/component/fluid/FluidHandlerWrapper.java new file mode 100644 index 000000000..e36987af0 --- /dev/null +++ b/src/main/java/nova/core/component/fluid/FluidHandlerWrapper.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2017 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NOVA. If not, see . + */ +package nova.core.component.fluid; + +import java.util.Optional; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +/** + * @author ExE Boss + */ +public class FluidHandlerWrapper extends FluidHandler { + + private final FluidHandler handler; + private final Predicate tankFilter; + + public FluidHandlerWrapper(FluidHandler handler, Predicate tankFilter) { + this.handler = handler; + this.tankFilter = tankFilter; + } + + @Override + public Optional removeFluid(Fluid fluid, boolean simulate) { + if (fluid.amount() == 0) + return Optional.empty(); + + Fluid f = fluid.withAmount(0); + Fluid r = fluid.clone(); + + for (Tank tank : getTanks()) { + if (!tank.hasFluidType(fluid)) + continue; + + int removed = tank.removeFluid(r).get().amount(); + r.remove(removed); + f.add(removed); + + if (r.amount() == 0) + break; + } + + return Optional.of(f).filter(fl -> fl.amount() > 0); + } + + @Override + public Optional removeFluid(int amount, boolean simulate) { + Optional fluid = getTanks().stream() + .filter(Tank::hasFluid) + .findFirst() + .flatMap(Tank::getFluid) + .map(f -> f.withAmount(0)); + + if (amount == 0 || !fluid.isPresent()) + return fluid; + + Fluid f = fluid.get(); + + for (Tank tank : getTanks()) { + if (!tank.hasFluidType(f)) + continue; + + int removed = tank.removeFluid(amount).get().amount(); + amount -= removed; + f.add(removed); + + if (amount == 0) + break; + } + + return Optional.of(f).filter(fl -> fl.amount() > 0); + } + + @Override + public int addFluid(Fluid fluid, boolean simulate) { + if (fluid.amount() == 0) + return 0; + + Fluid f = fluid.clone(); + int added = 0; + + for (Tank tank : getTanks()) { + int a = tank.addFluid(f, simulate); + added += a; + f.remove(a); + if (f.amount() == 0) + break; + } + + return added; + } + + @Override + public Set getTanks() { + return handler.getTanks().stream().filter(tankFilter).collect(Collectors.toSet()); + } +} diff --git a/src/main/java/nova/core/component/fluid/Tank.java b/src/main/java/nova/core/component/fluid/Tank.java index e39cf08f9..ce003fc43 100644 --- a/src/main/java/nova/core/component/fluid/Tank.java +++ b/src/main/java/nova/core/component/fluid/Tank.java @@ -82,4 +82,7 @@ default boolean hasFluidType(FluidFactory sample) { return false; } + default Optional getTag() { + return Optional.empty(); + } } diff --git a/src/main/java/nova/core/component/fluid/TankSimple.java b/src/main/java/nova/core/component/fluid/TankSimple.java index d72bec619..450465711 100644 --- a/src/main/java/nova/core/component/fluid/TankSimple.java +++ b/src/main/java/nova/core/component/fluid/TankSimple.java @@ -38,6 +38,7 @@ public class TankSimple implements Tank, Storable, Syncable { private Optional containedFluid = Optional.empty(); private OptionalInt capacity; private Predicate fluidFilter = f -> true; + private Optional name = Optional.empty(); public TankSimple() { this.capacity = OptionalInt.empty(); @@ -52,12 +53,22 @@ public TankSimple removeCapacity() { return this; } + public TankSimple removeTag() { + this.name = Optional.empty(); + return this; + } + public TankSimple setCapacity(int capacity) { this.capacity = OptionalInt.of(capacity); setFluid(containedFluid); return this; } + public TankSimple setTag(String name) { + this.name = Optional.of(name); + return this; + } + @Override public int addFluid(Fluid fluid, boolean simulate) { if (fluid.amount() == 0 || !fluidFilter.test(fluid)) @@ -173,4 +184,9 @@ public void read(Packet packet) { public void write(Packet packet) { containedFluid = Optional.of((Fluid) packet.readStorable()); } + + @Override + public Optional getTag() { + return name; + } }