Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MIN_CAP = 10;
- ALLOW_CAP = 80;
- TEST_INTERVAL = 3;
- MSG_INTERVAL = 10 * 60;
- CMD_NAME = "wsp";
- CHAT_NAME = "§6WS Power";
- local accus = {peripheral.find("modular_accumulator")};
- local chatBox = peripheral.find("chatBox");
- local doNotice = false;
- local rsOutput = false;
- local forceOutput = false;
- local forcedOutput = false;
- redstone.setOutput("top", rsOutput)
- local maxCap = 0;
- for _, accu in ipairs(accus) do
- maxCap = maxCap + accu.getCapacity();
- end
- string.startswith = function(self, str)
- return self:find('^' .. str) ~= nil
- end
- local function getEnergyPercent()
- local total = 0;
- for _, accu in ipairs(accus) do
- total = total + accu.getEnergy();
- end
- return math.floor(total / maxCap * 100);
- end
- local function getAlert(amt, error)
- local msg = amt .. "% base power remaining.";
- if forceOutput then
- msg = msg .. " Forcing output: " .. (forcedOutput and "OFF" or "ON");
- end
- return msg;
- end
- local function chatAlert(amt, error)
- error = error or false;
- local msg = getAlert(amt, error);
- print(msg);
- local color = error and "§c" or "§b";
- chatBox.sendMessage(msg, CHAT_NAME, "<>", color)
- end
- local function chatAlertPlayer(msg, username, error)
- error = error or false;
- local color = error and "§c" or "§b";
- chatBox.sendMessageToPlayer(msg, username, CHAT_NAME, "<>");
- end
- local function modPower()
- local remain = getEnergyPercent();
- if remain < MIN_CAP then
- if not rsOutput then chatAlert(remain, true); end
- rsOutput = true
- elseif rsOutput and remain > ALLOW_CAP then
- if rsOutput then chatAlert(remain); end
- rsOutput = false
- end
- local activePower = false;
- if forceOutput then activePower = forcedOutput
- else activePower = rsOutput
- end
- redstone.setOutput("top", activePower);
- end
- modPower();
- local updateTimer = os.startTimer(MSG_INTERVAL);
- local checkTimer = os.startTimer(TEST_INTERVAL);
- while true do
- local event, id, message, _, isHidden = os.pullEvent();
- if event == "timer" then
- if id == updateTimer then
- if not rsOutput and doNotice then
- chatAlert(getEnergyPercent());
- end
- updateTimer = os.startTimer(MSG_INTERVAL);
- end
- if id == checkTimer then
- modPower();
- checkTimer = os.startTimer(TEST_INTERVAL);
- end
- elseif event == "chat" and isHidden and string.startswith(message, CMD_NAME) then
- if message == CMD_NAME .. " on" then
- forceOutput = true;
- forcedOutput = false;
- chatAlertPlayer("Forcing output ON.", id);
- elseif message == CMD_NAME .. " off" then
- forceOutput = true;
- forcedOutput = true;
- chatAlertPlayer("Forcing output OFF.", id);
- elseif message == CMD_NAME .. " reset" then
- forceOutput = false;
- chatAlertPlayer("Reset.", id);
- elseif message == CMD_NAME .. " status" then
- local msg = getAlert(getEnergyPercent());
- chatAlertPlayer(msg, id);
- elseif message == CMD_NAME .. " notify" then
- doNotice = not doNotice;
- chatAlertPlayer("Updates: " .. (doNotice and "ON" or "OFF"), id);
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment