zootsuitman

Untitled

Nov 29th, 2023 (edited)
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. MIN_CAP = 10;
  2. ALLOW_CAP = 80;
  3. TEST_INTERVAL = 3;
  4. MSG_INTERVAL = 10 * 60;
  5. CMD_NAME = "wsp";
  6. CHAT_NAME = "§6WS Power";
  7.  
  8. local accus = {peripheral.find("modular_accumulator")};
  9. local chatBox = peripheral.find("chatBox");
  10.  
  11. local doNotice = false;
  12.  
  13. local rsOutput = false;
  14. local forceOutput = false;
  15. local forcedOutput = false;
  16. redstone.setOutput("top", rsOutput)
  17.  
  18. local maxCap = 0;
  19. for _, accu in ipairs(accus) do
  20.   maxCap = maxCap + accu.getCapacity();
  21. end
  22.  
  23. string.startswith = function(self, str)
  24.   return self:find('^' .. str) ~= nil
  25. end
  26.  
  27. local function getEnergyPercent()
  28.   local total = 0;
  29.   for _, accu in ipairs(accus) do
  30.     total = total + accu.getEnergy();
  31.   end
  32.   return math.floor(total / maxCap * 100);
  33. end
  34.  
  35. local function getAlert(amt, error)
  36.   local msg = amt .. "% base power remaining.";
  37.   if forceOutput then
  38.     msg = msg .. " Forcing output: " .. (forcedOutput and "OFF" or "ON");
  39.   end
  40.   return msg;
  41. end
  42.  
  43. local function chatAlert(amt, error)
  44.   error = error or false;
  45.   local msg = getAlert(amt, error);
  46.   print(msg);
  47.  
  48.   local color = error and "§c" or "§b";
  49.   chatBox.sendMessage(msg, CHAT_NAME, "<>", color)
  50. end
  51.  
  52. local function chatAlertPlayer(msg, username, error)
  53.   error = error or false;
  54.  
  55.   local color = error and "§c" or "§b";
  56.   chatBox.sendMessageToPlayer(msg, username, CHAT_NAME, "<>");
  57. end
  58.  
  59. local function modPower()
  60.   local remain = getEnergyPercent();
  61.  
  62.   if remain < MIN_CAP then
  63.     if not rsOutput then chatAlert(remain, true); end
  64.     rsOutput = true
  65.   elseif rsOutput and remain > ALLOW_CAP then
  66.     if rsOutput then chatAlert(remain); end
  67.     rsOutput = false
  68.   end
  69.  
  70.   local activePower = false;
  71.   if forceOutput then activePower = forcedOutput
  72.   else activePower = rsOutput
  73.   end
  74.  
  75.   redstone.setOutput("top", activePower);
  76. end
  77.  
  78. modPower();
  79.  
  80. local updateTimer = os.startTimer(MSG_INTERVAL);
  81. local checkTimer = os.startTimer(TEST_INTERVAL);
  82.  
  83. while true do
  84.   local event, id, message, _, isHidden = os.pullEvent();
  85.  
  86.   if event == "timer" then
  87.     if id == updateTimer then
  88.       if not rsOutput and doNotice then
  89.         chatAlert(getEnergyPercent());
  90.       end
  91.       updateTimer = os.startTimer(MSG_INTERVAL);
  92.     end
  93.     if id == checkTimer then
  94.       modPower();
  95.       checkTimer = os.startTimer(TEST_INTERVAL);
  96.     end
  97.   elseif event == "chat" and isHidden and string.startswith(message, CMD_NAME) then
  98.     if message == CMD_NAME .. " on" then
  99.       forceOutput = true;
  100.       forcedOutput = false;
  101.       chatAlertPlayer("Forcing output ON.", id);
  102.     elseif message == CMD_NAME .. " off" then
  103.       forceOutput = true;
  104.       forcedOutput = true;
  105.       chatAlertPlayer("Forcing output OFF.", id);
  106.     elseif message == CMD_NAME .. " reset" then
  107.       forceOutput = false;
  108.       chatAlertPlayer("Reset.", id);
  109.     elseif message == CMD_NAME .. " status" then
  110.       local msg = getAlert(getEnergyPercent());
  111.       chatAlertPlayer(msg, id);
  112.     elseif message == CMD_NAME .. " notify" then
  113.       doNotice = not doNotice;
  114.       chatAlertPlayer("Updates: " .. (doNotice and "ON" or "OFF"), id);
  115.     end
  116.   end
  117. end
Advertisement
Add Comment
Please, Sign In to add comment