mmb.engine.craft.rgroups.AbstractRecipeGroup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multimachinebuilder Show documentation
Show all versions of multimachinebuilder Show documentation
Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world.
THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<
/**
*
*/
package mmb.engine.craft.rgroups;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;
import javax.swing.JComponent;
import javax.swing.ListCellRenderer;
import io.vavr.Tuple2;
import mmb.NN;
import mmb.engine.craft.GlobalRecipeRegistrar;
import mmb.engine.craft.PlugAndPlayRecipeCellRenderer;
import mmb.engine.craft.Recipe;
import mmb.engine.craft.RecipeGroup;
import mmb.engine.craft.RecipeOutput;
import mmb.engine.craft.rgroups.ComplexCatRecipeGroup.ComplexCatalyzedRecipe;
import mmb.engine.item.ItemEntry;
import mmb.engine.settings.GlobalSettings;
import mmb.menu.world.craft.RecipeList;
import mmb.menu.world.window.TabRecipes;
import monniasza.collects.Collects;
import monniasza.collects.Identifiable;
import monniasza.collects.selfset.HashSelfSet;
import monniasza.collects.selfset.SelfSet;
/**
* An implementation aid for recipe groups.
* @dontextend This class should not be extended directly.
* Extend {@link AbstractRecipeGroupCatalyzed} or {@link AbstractRecipeGroupUncatalyzed} instead
* @author oskar
* @param the backend input identification type
* @param type of recipes
*/
public abstract class AbstractRecipeGroup<@NN Tbackend, @NN Trecipe extends Recipe<@NN Trecipe>&Identifiable> implements RecipeGroup<@NN Trecipe>{
@NN private final ListCellRenderer cellRenderer;
@NN private final String title;
@NN private final String id;
/**
* Create a recipe tab. Invoked once per recipe group
* @return
*/
protected final Tuple2 createTab(){
return new Tuple2(title, new RecipeList<>(this));
}
protected AbstractRecipeGroup(String id, Class rtype) {
//Backend generation
this.rtype = rtype;
recipesBackend = HashSelfSet.createNonnull(rtype);
recipesView = Collects.unmodifiableSelfSet(recipesBackend);
this.title = GlobalSettings.$res("machine-"+id);
this.id=id;
cellRenderer = new PlugAndPlayRecipeCellRenderer<>(createView());
Supplier> sup = this::createTab;
TabRecipes.add(sup);
GlobalRecipeRegistrar.addRecipeGroup(this);
}
@Override
public String title() {
return title;
}
@Override
public String id() {
return id;
}
@Override
public ListCellRenderer super Trecipe> cellRenderer() {
return cellRenderer;
}
//Recipe listing backend
/** Type of recipes */
@NN public final Class rtype;
@NN private final SelfSet recipesBackend;
@NN private final SelfSet recipesView;
@Override
public @NN SelfSet recipes() {
return recipesView;
}
//Supported items
@NN private final Set supportedBackend = new HashSet<>();
@NN private final Set supportedView = Collections.unmodifiableSet(supportedBackend);
@Override
public Set extends ItemEntry> supportedItems() {
return supportedView;
}
//Recipe insertion
protected void insert(Trecipe recipe) {
Tbackend recipeid = recipe.id();
Set items = items4id(recipeid);
//Supported items addition
for(ItemEntry item: items) {
supportedBackend.add(item);
}
//Recipe addition
recipesBackend.add(recipe);
//GlobalRecipeRegistrar
GlobalRecipeRegistrar.addRecipe(recipe);
}
//Backend conversion
public abstract Set items4id(Tbackend id);
}