Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish and improve fluid implementation #291

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public void preInit(FMLPreInitializationEvent evt) {
Game.blocks().init();
Game.items().init();
Game.entities().init();
Game.fluids().init();

//Load preInit
progressBar = ProgressManager.push("Pre-initializing NOVA wrappers", (novaModWrappers.isEmpty() ? 1 : novaModWrappers.size()) + novaWrappers.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public void preInit(FMLPreInitializationEvent evt) {
Game.blocks().init();
Game.items().init();
Game.entities().init();
Game.fluids().init();

//Load preInit
progressBar = ProgressManager.push("Pre-initializing NOVA wrappers", (novaModWrappers.isEmpty() ? 1 : novaModWrappers.size()) + novaWrappers.size());
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/nova/core/component/fluid/Fluid.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package nova.core.component.fluid;

import nova.core.block.BlockFactory;
import nova.core.component.exception.ComponentException;
import nova.core.component.misc.FactoryProvider;
import nova.core.retention.Data;
import nova.core.retention.Storable;
import nova.core.retention.Store;
Expand All @@ -29,19 +31,20 @@

import java.util.Optional;

// TODO: Should this extend ComponentProvider?
public class Fluid implements Identifiable, Storable, Cloneable {
/**
* 1000 liters = 1 cubic meter
*/
public static final int bucketVolume = 1000;
public static final int BUCKET_VOLUME = 1000;
/**
* Fluid amount is measured in liters.
*/
@Store(key = "amount")
private int amount = 1;

//TODO: Public instance variable is not good practice
public FluidFactory factory;
private FluidFactory factory = null;

/**
* @return Amount of fluid
Expand Down Expand Up @@ -104,7 +107,7 @@ public Fluid withAmount(int amount) {
* @return The block. There may be no block associated with this fluid.
*/
public Optional<BlockFactory> getBlockFactory() {
return Optional.empty();
return Game.blocks().get(getID());
}

@Override
Expand All @@ -125,9 +128,25 @@ public boolean sameType(Fluid stack) {
return stack.getID().equals(getID());
}

void initFactory(FluidFactory factory) {
if (this.factory == null) {
this.factory = factory;
} else {
throw new ComponentException("Attempt to add two components of the type %s to " + this, FactoryProvider.class);
}
}

public final FluidFactory getFactory() {
if (factory != null) {
return factory;
} else {
throw new ComponentException("Attempt to get component that does not exist: %s", FactoryProvider.class);
}
}

@Override
public final String getID() {
return factory.getID();
return getFactory().getID();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/nova/core/component/fluid/FluidFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public FluidFactory(String id, Supplier<Fluid> constructor) {
@Override
public Fluid build() {
Fluid build = super.build();
build.factory = this;
build.initFactory(this);
return build;
}

Expand Down
64 changes: 64 additions & 0 deletions src/main/java/nova/core/component/fluid/FluidFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
*/

package nova.core.component.fluid;

import java.util.function.Predicate;

/**
* A filter that only accepts a specific sub-type of {@link Fluid}. For use with tanks.
*
* @author ExE Boss
*/
@FunctionalInterface
public interface FluidFilter extends Predicate<Fluid> {

/**
* Returns an {@link FluidFilter} that accepts an {@link Fluid} of the same
* type as the provided.
*
* @param fluid
* @return FluidFilter
*/
static FluidFilter of(Fluid fluid) {
return fluid::sameType;
}

/**
* Returns an {@link FluidFilter} that accepts an {@link Fluid} of the same
* type as provided.
*
* @param id
* @return FluidFilter
*/
static FluidFilter of(String id) {
return (other) -> id.equals(other.getID());
}

/**
* Accepts any {@link Fluid} that has a &gt;= amount than provided.
*
* @param amount
* @return FluidFilter
*/
static FluidFilter of(int amount) {
return (other) -> other.amount() >= amount;
}
}
75 changes: 56 additions & 19 deletions src/main/java/nova/core/component/fluid/FluidHandler.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,81 @@
/*
* Copyright (c) 2015 NOVA, All rights reserved.
* 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 General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 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 General Public License for more details.
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* 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;
*/
package nova.core.component.fluid;

import nova.core.component.Component;
import nova.core.util.Direction;
import nova.core.retention.Storable;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* @author Calclavia
* @author ExE Boss
*/
public class FluidHandler extends Component {
public abstract class FluidHandler extends Component implements FluidIO, TankProvider, Storable {

public Set<Tank> tanks = new HashSet<>();
public Function<Direction, Set<Tank>> sidedTanks = direction -> tanks;
public static FluidHandler singleTank() {
return FluidHandlerSimple.simpleSingleTank();
}

public FluidHandler() {
public static FluidHandler singleTank(int capacity) {
return FluidHandlerSimple.simpleSingleTank(capacity);
}

public FluidHandler(Tank... tanks) {
this.tanks.addAll(Arrays.asList(tanks));
public static FluidHandler singleTank(Predicate<Fluid> fluidFilter) {
return FluidHandlerSimple.simpleSingleTank(fluidFilter);
}

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();
}

@Override
public boolean isEmpty() {
return getTanks().isEmpty() || getTanks().stream().allMatch(Tank::isEmpty);
}

@Override
public boolean hasFluid() {
return !getTanks().isEmpty() && getTanks().stream().anyMatch(Tank::hasFluid);
}

@Override
public boolean hasFluidType(String fluidID) {
return !getTanks().isEmpty() && getTanks().stream().anyMatch(t -> t.hasFluidType(fluidID));
}

@Override
public boolean hasFluidType(Fluid sample) {
return !getTanks().isEmpty() && getTanks().stream().anyMatch(t -> t.hasFluidType(sample));
}

@Override
public boolean hasFluidType(FluidFactory sample) {
return !getTanks().isEmpty() && getTanks().stream().anyMatch(t -> t.hasFluidType(sample));
}
}
Loading