Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import: AAMw1hB3; // math
- #import: A60Ncqat; // database
- #import:vCAdipqP; // better peripherals
- initRecipes:{
- $recipes$ = [];
- while (true) {
- inp = input("in,out,ident,chance");
- if (inp==""){
- return();
- };
- in,out,identifier,chance = splitText(inp,",");
- multiplier = floor(1.0/tonumber(chance) + 1);
- $recipes$.add({"in"=in,"out"=out,"multiplier"=multiplier,
- "identifier" = identifier});
- };
- };
- // minecraft:soul_sand,minecraft:quartz,minecraft:cobblestone,0.48
- // minecraft:soul_sand,minecraft:quartz,minecraft:cobblestone,0.48
- // minecraft:red_sand,minecraft:gold_nugget,minecraft:gravel,0.36
- estimateToCraft(identifier):{
- transferred = 0;
- changed = true;
- while(changed){
- changed = false;
- items = $chestCobblestone$.call("list")@table;
- slots = items.keys();
- for(i=0;i<slots.length;i++){
- slot = slots[i];
- item = items[slot]@table;
- if (item["name"]!=identifier) {
- continue;
- };
- t = $chestCobblestone$.call("pushItems", $chestIncOut$.getName(),
- slot, 64);
- transferred = transferred + t;
- if (t>0){
- changed = true;
- };
- };
- if (changed){
- sleep(1);
- };
- };
- return(transferred);
- };
- transferAll(chestSrc, chestTarget, type):{
- transferred = 0;
- items = chestSrc.call("list")@table;
- slots = items.keys();
- for(i=0;i<slots.length;i++){
- slot = slots[i];
- item = items[slot]@table;
- if (item["name"] != type){
- continue;
- };
- transferred = transferred + chestSrc.call("pushItems",
- chestTarget.getName(), slot, 64);
- };
- return(transferred);
- };
- transfer(chestSrc, chestTarget, toTransfer, type):{
- while(toTransfer > 0){
- items = chestSrc.call("list")@table;
- slots = items.keys();
- for(i=0;i<slots.length;i++){
- slot = slots[i];
- item = items[slot]@table;
- if (item["name"]!=type) {continue;};
- if (toTransfer<=0){
- return();
- };
- t = chestSrc.call("pushItems", chestTarget.getName(),
- slot, toTransfer);
- toTransfer = toTransfer - t;
- };
- };
- };
- craft(toCraft, sourceItem, targetItem, multiplier):{
- craftedTotal = 0;
- while(toCraft>0){
- toTransfer = multiplier * toCraft;
- transfer($chestInput$, $chestIncIn$, toTransfer, sourceItem);
- print("transferred " + toTransfer +" "+sourceItem);
- sleep(20);
- crafted = transferAll($chestResult$, $chestIncOut$, targetItem);
- craftedTotal = craftedTotal + crafted;
- print("crafted "+crafted+" "+targetItem);
- toCraft = toCraft - crafted;
- };
- return(craftedTotal);
- };
- craftingJob(recipe):{
- in = recipe["in"];
- out = recipe["out"];
- multiplier = recipe["multiplier"];
- identifier = recipe["identifier"];
- while(true) {
- toCraft = estimateToCraft(identifier);
- if (toCraft <= 0){
- transferAll($chestResult$, $chestIncOut$, out);
- sleep(1);
- continue;
- }else{
- print(toCraft+" "+out+" requested");
- perCycle = floor(64/multiplier);
- while (toCraft > perCycle){
- toCraft = toCraft - craft(perCycle, in, out, multiplier);
- };
- craft(toCraft, in, out, multiplier);
- print("crafting done");
- };
- };
- };
- main: {
- $recipes$ = loadValue("recipes");
- if ($recipes$ == null){
- initRecipes();
- saveValue("recipes", $recipes$);
- };
- $chestCobblestone$ = wrapPeripheral("top");
- $chestInput$ = wrapPeripheral("right");
- $chestResult$ = wrapPeripheral("left");
- $chestIncIn$ = wrapPeripheral("back");
- $chestIncOut$ = wrapPeripheral("front");
- for(i=0;i<$recipes$.length;i++){
- startThread(@craftingJob, $recipes$[i]);
- };
- };
Add Comment
Please, Sign In to add comment