abc123mewot

AutofeedV1

Oct 14th, 2023
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. local mods = peripheral.find("neuralInterface")
  2. if not (mods and mods.hasModule("plethora:introspection")) then error("Must have nueral interface w/ instrospection module") end
  3.  
  4.  
  5. local foodPreferences = {
  6.     "minecraft:baked_potato",
  7.     "minecraft:potato",
  8.     "minecraft:wheat"
  9. }
  10.  
  11. local hungerTextPos = nil
  12. local hungerTextSize = 0.5
  13. local hungerStatusText = nil
  14. local hungerItemText = nil
  15. local autoCheckFoodInterval = 100
  16.  
  17. local cnv = mods.canvas()
  18. cnv.clear()
  19. if cnv then
  20.     if not hungerTextPos then
  21.         local cw, ch = cnv.getSize()
  22.         hungerTextPos = {cw * 0.255, ch * 0.435}
  23.     end
  24.     local grp = cnv.addGroup(hungerTextPos)
  25.     grp.clear()
  26.     local xp, yp, b = hungerTextPos[1], hungerTextPos[2], 5 * hungerTextSize
  27.     grp.addRectangle(xp, yp, b + hungerTextSize * 8 * 10, b + hungerTextSize * 2 * 12, 0xFFFFFF30)
  28.     xp, yp = xp + b, yp + b
  29.     hungerStatusText = grp.addText({xp, yp}, "Food: ??/??", 0x000000FF, hungerTextSize)
  30.     hungerItemText = grp.addText({xp , yp + 10 * hungerTextSize}, "Unknown", 0x000000FF, hungerTextSize)
  31.     print("Overlay glasses initialized!")
  32. end
  33. local inv = mods.getInventory()
  34. local cachedFoodSlot = nil
  35.  
  36. local function findSlotWithFood(specificFoodName)
  37.     local bestSlot, maxSaturation = nil, nil
  38.     for slot, meta in pairs(inv.list()) do
  39.         local itemMeta = inv.getItemMeta(slot)
  40.         local itemData = inv.getItem(slot)
  41.         if itemMeta and itemData then
  42.             if specificFoodName and specificFoodName == itemMeta.name then
  43.                 if not itemData.consume then
  44.                     print(textutils.serializeJSON(itemMeta))
  45.                     error("Preffered food item is not consumable!")
  46.                 end
  47.                 return slot
  48.             elseif itemMeta.saturation and inv.getItem(slot).consume then
  49.                 if (not bestSlot) or itemMeta.saturation > maxSaturation then
  50.                     bestSlot = slot
  51.                     maxSaturation = itemMeta.saturation
  52.                 end
  53.             end
  54.         end
  55.     end
  56.     if specificFoodName then
  57.         return nil
  58.     else
  59.         return bestSlot
  60.     end
  61. end
  62. local function findOptimumFoodSlot()
  63.     for _, pref in ipairs(foodPreferences) do
  64.         print("Checking preference: " .. pref)
  65.         local foundSlot = findSlotWithFood(pref)
  66.         if foundSlot then
  67.             return foundSlot, true
  68.         end
  69.     end
  70.     print("Could not find an optimal food slot")
  71.     return findSlotWithFood(nil), false
  72. end
  73.  
  74. local autoCheckFoodCount = autoCheckFoodInterval + 1
  75.  
  76. while true do
  77.     local data = mods.getMetaOwner()
  78.     if data and data.food then
  79.         while data.food.hungry or autoCheckFoodCount > autoCheckFoodInterval do
  80.             autoCheckFoodCount = 0
  81.             local toEat = {
  82.                 item = nil,
  83.                 meta = nil,
  84.                 slot = nil
  85.             }
  86.             if cachedFoodSlot then
  87.                 local cachedItem = inv.getItem(cachedFoodSlot)
  88.                 if cachedItem and cachedItem.consume then
  89.                     toEat.slot = cachedFoodSlot
  90.                     toEat.item = cachedItem
  91.                     toEat.meta = inv.getItemMeta(cachedFoodSlot)
  92.                 else
  93.                     cachedFoodSlot = nil
  94.                 end
  95.             end
  96.             if not toEat.item then
  97.                 local foodSlot, isPrefferedFoodType = findOptimumFoodSlot()
  98.                 if foodSlot then
  99.                     local foodItem = inv.getItem(foodSlot)
  100.                     if foodItem and foodItem.consume then
  101.                         local foodMeta = inv.getItemMeta(foodSlot)
  102.                         if isPrefferedFoodType then
  103.                             cachedFoodSlot = foodSlot
  104.                             if cnv then hungerItemText.setText(foodMeta.displayName) end
  105.                         else
  106.                             if cnv then hungerItemText.setText("No preferred food") end
  107.                         end
  108.                         toEat.slot = foodSlot
  109.                         toEat.item = foodItem
  110.                         toEat.meta = foodMeta
  111.                     end
  112.                 end
  113.             end
  114.             if toEat.item and toEat.item.consume then
  115.                 if data.food.hungry then toEat.item.consume() end
  116.                 if cnv then
  117.                     if toEat.meta then
  118.                         hungerStatusText.setText("Food: " .. toEat.meta.count .. "/" .. toEat.meta.maxCount)
  119.                     else hungerStatusText.setText("Food: ??/??") end
  120.                 end
  121.             elseif toEat.item then
  122.                 print("Warning: Item to eat was consumable but now is not?!")
  123.                 if cnv then hungerStatusText.setText("Check logs!") os.sleep(3) end
  124.             else
  125.                 if cnv then hungerStatusText.setText("No edible food!") end
  126.                 print("Player has no edible food!")
  127.                 break
  128.             end
  129.             data = mods.getMetaOwner()
  130.         end
  131.         autoCheckFoodCount = autoCheckFoodCount + 1
  132.         os.sleep(0.25)
  133.     end
  134. end
  135.  
Advertisement
Add Comment
Please, Sign In to add comment