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

mmb.content.stn.CablingTool 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<

The newest version!
/**
 * 
 */
package mmb.content.stn;

import java.util.Objects;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import mmb.NN;
import mmb.Nil;
import mmb.data.variables.DataValueDouble;
import mmb.engine.inv.io.InventoryReader;
import mmb.engine.inv.io.InventoryWriter;
import mmb.engine.item.Item;
import mmb.engine.item.ItemEntityMutable;
import mmb.engine.item.ItemType;
import mmb.engine.json.JsonTool;

/**
 * @author oskar
 *
 */
public final class CablingTool extends ItemEntityMutable {
	/** Amount of cable in spools */
	public final DataValueDouble amt = new DataValueDouble(0);
	/** The item required to load the cabling tool */
	@NN public static final Item WIRE = STN.STN_a.wire;
	/** Maximum wire in spools */
	public final double MAX_WIRE = 1000;
	
	/** Creates an STN cabling tool */
	public CablingTool() {
		//empty
	}
	/** Copy constructor */
	protected CablingTool(CablingTool original) {
		amt.set(original.amt.getDouble());
	}

	@Override
	public CablingTool itemClone() {
		return new CablingTool(this);
	}

	@Override
	public void load(@Nil JsonNode data) {
		if(data == null || !data.isObject()) return;
		JsonNode amtNode = data.get("amt");
		if(amtNode != null) amt.set(amtNode.asDouble());
	}
	@Override
	public JsonNode save() {
		ObjectNode node = JsonTool.newObjectNode();
		node.put("amt", amt.getDouble());
		return node;
	}
	
	public boolean unloadWire(InventoryWriter inv) {
		if(amt.getDouble() < 0) return false;
		int write = inv.insert(WIRE);
		boolean result = write==1;
		if(result) amt.set(amt.getDouble()-1);
		return result;
	}
	public boolean loadWire(InventoryReader inv) {
		double remain = MAX_WIRE - amt.getDouble();
		if(remain < 1) return false;
		int write = 0;
		switch(inv.level()) {
		case RANDOM:
			write = inv.extract(WIRE, 1);
			break;
		case SKIPPING:
			while(inv.currentItem() != WIRE){
				if(!inv.hasNext()) return false;
				inv.next();
				//look for the wire
			}
			break;
		case SEQUENTIAL:
			if(inv.currentItem() != WIRE) return false;
			write = inv.extract(1);
			break;
		default:
			Objects.requireNonNull(inv.level(), "Extraction level is null");
			throw new IllegalArgumentException("Invalid extraction level: "+inv.level());
		}
		boolean result = write==1;
		if(result) amt.set(amt.getDouble()-1);
		return result;
	}
	@Override
	public ItemType type() {
		return STN.STN_cabler;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy