craftClient = getMinecraftClient(); player = craftClient.getPlayer(); world = craftClient.getWorld(); eventIDs = {}; programState = "off"; programStateInfo = ""; slotIDs = {"craftingTable":[1,2,3,4,5,6,7,8,9]}; validChestBlocks = ["ShulkerBox","GenericContainer"]; guiBlockLocations = {}; recipieStorage = {}; debug(true); //setup fun calculateRecipieCost(recipie){ recipieCost = {}; foreach (ingredient : recipie){ ingredient = ingredient.getItemId(); if(ingredient != "air"){ try{ recipieCost.get(ingredient); //fails if absent recipieCost.put(ingredient, recipieCost.get(ingredient)+1); //print("expanded existing slot for " + ingredient); } catch (error) { recipieCost.put(ingredient,1); //print("added new slot for " + ingredient); } } //else {print("skipped air");} } player.message("at a price of" + recipieCost); return recipieCost; } fun scanRecipie(blockType){ slotsToScan = slotIDs.get(blockType); craftingRecipie = [player.getItemForSlot(0)]; player.message("scanned the following Recipie:"); foreach (slot : slotsToScan){ craftingRecipie.append(player.getItemForSlot(slot)); player.message("\"" + player.getItemForSlot(slot).getItemId() + "\" for slot " + slot); } player.message("resulting in \"" + player.getItemForSlot(0).getItemId() + "\""); return craftingRecipie; } fun saveNewRecipie(isMain){ craftClient.playSound("block.note_block.pling",1,2); craftingRecipie = scanRecipie("craftingTable"); recipieData = {}; resultItem = craftingRecipie.removeIndex(0).getItemId(); craftingCost = calculateRecipieCost(craftingRecipie); recipieData.put("recipie",craftingRecipie); recipieData.put("cost",craftingCost); recipieStorage.put(resultItem,recipieData); if (isMain == true){ recipieStorage.put("mainItem",resultItem); } programStateInfo = ": ready to craft!"; craftClient.removeGameEvent("onOpenScreen",eventIDs.get("onOpenScreen")); player.closeScreen(); print(recipieStorage); print(recipieStorage.get(resultItem).get("recipie")); print(recipieStorage.get(resultItem).get("cost")); } fun setupInputChest(){ craftClient.playSound("block.note_block.pling",1,2); guiBlockLocations.put("inputBlockYaw", player.getYaw()); guiBlockLocations.put("inputBlockPitch", player.getPitch()); programStateInfo = ": output container"; player.closeScreen(); } fun setupOutputChest(){ craftClient.playSound("block.note_block.pling",1,2); guiBlockLocations.put("outputBlockYaw", player.getYaw()); guiBlockLocations.put("outputBlockPitch", player.getPitch()); programStateInfo = ": crafting table"; player.closeScreen(); } fun setupCraftingTable(){ craftClient.playSound("block.note_block.pling",1,2); guiBlockLocations.put("craftBlockYaw", player.getYaw()); guiBlockLocations.put("craftBlockPitch", player.getPitch()); programStateInfo = ": crafting recipie [C to scan]"; eventIDs.put("onKeyPressC", craftClient.addGameEvent("onKeyPress", fun(key) { if (key == "c" && player.getCurrentScreen().getScreenName() == "Crafting"){ saveNewRecipie(true); craftClient.removeGameEvent("onKeyPress",eventIDs.get("onKeyPressC")); } })); } fun setupCraftValues(){ //reset craftingRecipie = []; guiBlockLocations.clear(); if (eventIDs.get("onOpenScreen")!= null){ craftClient.removeGameEvent("onOpenScreen",eventIDs.get("onOpenScreen")); } //set program state programState = "waiting for setup"; programStateInfo = ": input container"; runThreaded( fun(){ while(programState == "waiting for setup"){ player.messageActionBar(programState.uppercase() + programStateInfo); } },[] ); //add event for screen check eventIDs.put("onOpenScreen", craftClient.addGameEvent("onOpenScreen", fun(screen){ screen = screen.getScreenName(); //print(screen); if (screen == "Crafting" && programState == "waiting for setup" && programStateInfo == ": crafting table"){ setupCraftingTable(); } else if (validChestBlocks.contains(screen) == true && programState == "waiting for setup"){ if (programStateInfo == ": input container"){ setupInputChest(); } else { if (programStateInfo == ": output container"){ setupOutputChest(); } } } }) ); } fun countItemsOfType(itemID){ itemCount = 0; itemStacks = player.getAllSlotsFor(craftClient.itemFromString(itemID)); foreach (item : itemStacks){ itemCount = itemCount + player.getItemForSlot(item).getCount(); } return itemCount; } fun canAffordRecipie(recipie){ recipieCost = recipieStorage.get(recipie).get("cost"); ingredients = recipieCost.getKeys(); ranOut = []; //print("ingredients: " + ingredients.toString() + " cost:" + recipieCost.toString()); foreach (ingredient : ingredients){ print(ingredient + " is: " + countItemsOfType(ingredient).toString() + " | should: " + recipieCost.get(ingredient).toString()); if(countItemsOfType(ingredient) < recipieCost.get(ingredient)){ ranOut.append(ingredient); } } if (ranOut.isEmpty() == true){ print("empty"); return true; } else { return ranOut; } } fun lookAtBlock(map,yawKey,pitchKey){ player.look(map.get(yawKey),map.get(pitchKey)); } //event related stuff fun craftingScreen(screen){ }; fun lookCommand(arguments){ argument = ""; try { if (len(arguments) > 0) { argument = arguments.getIndex(1); if (argument == "crafting"){ lookAtBlock(guiBlockLocations,"craftBlockYaw","craftBlockPitch"); } else{ if (argument == "input"){ lookAtBlock(guiBlockLocations,"inputBlockYaw","inputBlockPitch"); } else { if (argument == "output"){ lookAtBlock(guiBlockLocations,"outputBlockYaw","outputBlockPitch"); } } } } } catch (error) { print(error); } } //events craftClient.addCommand("crafting",[ ["setup","look","start"] ]); getMinecraftClient().addGameEvent("onClickSlot", fun(slotNum) { //print(slotNum); }); craftClient.addGameEvent("onKeyPress", fun(key) { try { if (key == "h" && player.getCurrentScreen().getScreenName() == "Crafting") { while (canAffordRecipie(recipieStorage.get("mainItem")) == true) { player.craft(recipieStorage.get(recipieStorage.get("mainItem")).get("recipie")); sleep(100); } print("missing ingredient: " + canAffordRecipie(recipieStorage.get("mainItem"))); continue; } } catch (error) {print(error);} }); /* craftClient.addGameEvent("onInteractItem", fun (item){ print(countItemsOfType(item.getItemId())); }); */ //todo: redo using try/catch craftClient.addGameEvent("onCommand", fun(command, arguments) { if (command == "crafting"){ if (arguments.isEmpty() == true){ //check if list is empty player.message("invalid syntax"); } else { argument = ""; argument = arguments.getIndex(0); if (argument == "look"){ lookCommand(arguments); } else if (argument == "setup"){ setupCraftValues(); } } } }); hold();