Skip to content

Commit

Permalink
Implement Tank tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Apr 8, 2017
1 parent fbe4278 commit fb1e631
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main/java/nova/core/component/fluid/FluidHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public static FluidHandler singleTank(int capacity, Predicate<Fluid> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,7 +78,7 @@ public FluidHandlerSimple(boolean resizable, Tank... tanks) {

@Override
public Set<Tank> getTanks() {
return tanks;
return Collections.unmodifiableSet(tanks);
}

@Override
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/nova/core/component/fluid/FluidHandlerWrapper.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Tank> tankFilter;

public FluidHandlerWrapper(FluidHandler handler, Predicate<Tank> tankFilter) {
this.handler = handler;
this.tankFilter = tankFilter;
}

@Override
public Optional<Fluid> 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<Fluid> removeFluid(int amount, boolean simulate) {
Optional<Fluid> 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<Tank> getTanks() {
return handler.getTanks().stream().filter(tankFilter).collect(Collectors.toSet());
}
}
3 changes: 3 additions & 0 deletions src/main/java/nova/core/component/fluid/Tank.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@ default boolean hasFluidType(FluidFactory sample) {
return false;
}

default Optional<String> getTag() {
return Optional.empty();
}
}
16 changes: 16 additions & 0 deletions src/main/java/nova/core/component/fluid/TankSimple.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class TankSimple implements Tank, Storable, Syncable {
private Optional<Fluid> containedFluid = Optional.empty();
private OptionalInt capacity;
private Predicate<Fluid> fluidFilter = f -> true;
private Optional<String> name = Optional.empty();

public TankSimple() {
this.capacity = OptionalInt.empty();
Expand All @@ -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))
Expand Down Expand Up @@ -173,4 +184,9 @@ public void read(Packet packet) {
public void write(Packet packet) {
containedFluid = Optional.of((Fluid) packet.readStorable());
}

@Override
public Optional<String> getTag() {
return name;
}
}

0 comments on commit fb1e631

Please sign in to comment.