Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mods = peripheral.find("neuralInterface")
- if not (mods and mods.hasModule("plethora:introspection")) then error("Must have nueral interface w/ instrospection module") end
- local foodPreferences = {
- "minecraft:baked_potato",
- "minecraft:potato",
- "minecraft:wheat"
- }
- local hungerTextPos = nil
- local hungerTextSize = 0.5
- local hungerStatusText = nil
- local hungerItemText = nil
- local autoCheckFoodInterval = 100
- local cnv = mods.canvas()
- cnv.clear()
- if cnv then
- if not hungerTextPos then
- local cw, ch = cnv.getSize()
- hungerTextPos = {cw * 0.255, ch * 0.435}
- end
- local grp = cnv.addGroup(hungerTextPos)
- grp.clear()
- local xp, yp, b = hungerTextPos[1], hungerTextPos[2], 5 * hungerTextSize
- grp.addRectangle(xp, yp, b + hungerTextSize * 8 * 10, b + hungerTextSize * 2 * 12, 0xFFFFFF30)
- xp, yp = xp + b, yp + b
- hungerStatusText = grp.addText({xp, yp}, "Food: ??/??", 0x000000FF, hungerTextSize)
- hungerItemText = grp.addText({xp , yp + 10 * hungerTextSize}, "Unknown", 0x000000FF, hungerTextSize)
- print("Overlay glasses initialized!")
- end
- local inv = mods.getInventory()
- local cachedFoodSlot = nil
- local function findSlotWithFood(specificFoodName)
- local bestSlot, maxSaturation = nil, nil
- for slot, meta in pairs(inv.list()) do
- local itemMeta = inv.getItemMeta(slot)
- local itemData = inv.getItem(slot)
- if itemMeta and itemData then
- if specificFoodName and specificFoodName == itemMeta.name then
- if not itemData.consume then
- print(textutils.serializeJSON(itemMeta))
- error("Preffered food item is not consumable!")
- end
- return slot
- elseif itemMeta.saturation and inv.getItem(slot).consume then
- if (not bestSlot) or itemMeta.saturation > maxSaturation then
- bestSlot = slot
- maxSaturation = itemMeta.saturation
- end
- end
- end
- end
- if specificFoodName then
- return nil
- else
- return bestSlot
- end
- end
- local function findOptimumFoodSlot()
- for _, pref in ipairs(foodPreferences) do
- print("Checking preference: " .. pref)
- local foundSlot = findSlotWithFood(pref)
- if foundSlot then
- return foundSlot, true
- end
- end
- print("Could not find an optimal food slot")
- return findSlotWithFood(nil), false
- end
- local autoCheckFoodCount = autoCheckFoodInterval + 1
- while true do
- local data = mods.getMetaOwner()
- if data and data.food then
- while data.food.hungry or autoCheckFoodCount > autoCheckFoodInterval do
- autoCheckFoodCount = 0
- local toEat = {
- item = nil,
- meta = nil,
- slot = nil
- }
- if cachedFoodSlot then
- local cachedItem = inv.getItem(cachedFoodSlot)
- if cachedItem and cachedItem.consume then
- toEat.slot = cachedFoodSlot
- toEat.item = cachedItem
- toEat.meta = inv.getItemMeta(cachedFoodSlot)
- else
- cachedFoodSlot = nil
- end
- end
- if not toEat.item then
- local foodSlot, isPrefferedFoodType = findOptimumFoodSlot()
- if foodSlot then
- local foodItem = inv.getItem(foodSlot)
- if foodItem and foodItem.consume then
- local foodMeta = inv.getItemMeta(foodSlot)
- if isPrefferedFoodType then
- cachedFoodSlot = foodSlot
- if cnv then hungerItemText.setText(foodMeta.displayName) end
- else
- if cnv then hungerItemText.setText("No preferred food") end
- end
- toEat.slot = foodSlot
- toEat.item = foodItem
- toEat.meta = foodMeta
- end
- end
- end
- if toEat.item and toEat.item.consume then
- if data.food.hungry then toEat.item.consume() end
- if cnv then
- if toEat.meta then
- hungerStatusText.setText("Food: " .. toEat.meta.count .. "/" .. toEat.meta.maxCount)
- else hungerStatusText.setText("Food: ??/??") end
- end
- elseif toEat.item then
- print("Warning: Item to eat was consumable but now is not?!")
- if cnv then hungerStatusText.setText("Check logs!") os.sleep(3) end
- else
- if cnv then hungerStatusText.setText("No edible food!") end
- print("Player has no edible food!")
- break
- end
- data = mods.getMetaOwner()
- end
- autoCheckFoodCount = autoCheckFoodCount + 1
- os.sleep(0.25)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment