All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mmb.engine.craft.Craftings Maven / Gradle / Ivy

Go to download

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<

There is a newer version: 0.6
Show newest version
/**
 * 
 */
package mmb.engine.craft;

import java.util.Objects;

import mmb.engine.debug.Debugger;
import mmb.engine.inv.Inventory;
import mmb.engine.inv.io.InventoryReader;
import mmb.engine.inv.io.InventoryWriter;
import mmb.engine.item.ItemEntry;

/**
 *
 * @author oskar
 *
 */
public class Craftings {
	private Craftings() {}
	public static final Debugger debug = new Debugger("CRAFTING PROCESSOR");
	
	/**
	 * Performs a crafting transaction
	 * @param inItems a set of input items
	 * @param reader inventory reader
	 * @param outItems a set of output items
	 * @param writer inventory writer
	 * @param amount number of transactions to perform
	 * @return number of crafting transactions
	 */
	public static int transact(RecipeOutput inItems, InventoryReader reader, RecipeOutput outItems, InventoryWriter writer, int amount) {
		int transactable = writer.toInsertBulk(outItems, amount);
		int moved = reader.extractBulk(inItems, transactable);
		return writer.bulkInsert(outItems, moved);
	}
	/**
	 * Performs a crafting transaction
	 * @param inItems a set of input items
	 * @param reader source inventory
	 * @param outItems a set of output items
	 * @param writer inventory writer
	 * @param amount number of transactions to perform
	 * @return number of crafting transactions
	 */
	public static int transact(RecipeOutput inItems, Inventory reader, RecipeOutput outItems, InventoryWriter writer, int amount) {
		return transact(inItems, reader.createReader(), outItems, writer, amount);
	}
	/**
	 * Performs a crafting transaction
	 * @param inItems a set of input items
	 * @param reader inventory reader
	 * @param outItems a set of output items
	 * @param writer destination inventory
	 * @param amount number of transactions to perform
	 * @return number of crafting transactions
	 */
	public static int transact(RecipeOutput inItems, InventoryReader reader, RecipeOutput outItems, Inventory writer, int amount) {
		return transact(inItems, reader, outItems, writer.createWriter(), amount);
	}
	/**
	 * Performs a crafting transaction
	 * @param inItems a set of input items
	 * @param reader source inventory
	 * @param outItems a set of output items
	 * @param writer destination inventory
	 * @param amount number of transactions to perform
	 * @return number of crafting transactions
	 */
	public static int transact(RecipeOutput inItems, Inventory reader, RecipeOutput outItems, Inventory writer, int amount) {
		return transact(inItems, reader.createReader(), outItems, writer.createWriter(), amount);
}
	
	public static boolean transact(SingleItem in, RecipeOutput out, Inventory from, Inventory to) {
		return transact(in.item(), in.amount(), out, from, to);
	}
	public static boolean transact(ItemEntry entry, int inAmount, RecipeOutput out, Inventory from, Inventory to) {
		Objects.requireNonNull(from, "from is null");
		if(from.get(entry).amount() < inAmount) return false;
		//Calculate capacity
		double volume = out.outVolume();
		double remainCapacity = to.capacity() - to.volume();
		if(volume > remainCapacity) {
			debug.printl("Required "+volume+" capacity, got "+remainCapacity);
			return false;
		} //Not enough space in the output
		//Withdraw from input
		from.extract(entry, inAmount);
		out.produceResults(to.createWriter());
		return true;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy