View difference between Paste ID: ZnHMn60g and 4XYCedMC
SHOW: | | - or go back to the newest paste.
1-
--[[
1+
--[[Gestion base 1.0 : Ce script permet de faire un suivis des stocks d'une base (tank, chest et barrels) avec une station principale et des secondaires. Pour les chest et barrels il faut utiliser "peripheral proxy".--]]
2-
	Base Monitor 1.9.1
2+
3-
		This script allows you to monitor caches, tanks, barrels and chests
3+
local isMain = true; -- base principale (qui va recevoire les information sdes autres bases
4-
		Monitoring chests requires an open peripheral proxy, the same goes for
4+
local myid = os.computerLabel()
5-
		buildcraft tanks.
5+
local id = myid -- "Base principale"; -- nom de la base
6-
		You can build one of more computers and set one of them as the main server by
6+
local UpdateIntervalSeconds = 10; -- reduire le temps si server trop charge
7-
		setting isMain to true. This will let it receive information from other computers.
7+
local ItemsFullPercentAt = 256; -- stock minimum d'objet (barre %)
8-
		For the main server to receive info from other computers you need to attach a
8+
local FluidFullPercentAt = 64000; -- stock minimum de liquide (barre %)
9-
		wireless modem to each computer.
9+
10-
		Peripherals can be attached to any side you like, the script will automatically
10+
local VersionInfo = "Gestion stock"; -- nom de la version
11-
		work out where the peripherals are.
11+
12-
	Changes since 1.9
12+
13-
		Added support for minecraft chests behind Advanced Peripherals
13+
14-
		Added support for Liquid tanks behind Advanced Peripherals
14+
15-
	Changes since 1.8
15+
16-
		Added support for the Advanced Peripherals Block Reader
16+
local chestTextColor = colors.white; -- couleur text (chest)
17-
		Added support for Liquids in the RS Bridge
17+
local chestBackColor = colors.purple; -- couleur fond (chest)
18-
	Changes since 1.7
18+
19-
		Updated the script for ComputerCraft Tweaked.
19+
20-
		Chests and Tanks have different function names when getting info via modems.
20+
21-
		Added checks to support both new and old methods.
21+
22-
		Added support for industrial foregoing blackhole tanks.
22+
23-
		Replacing multiple spaces in names with a single space.
23+
local blacklist = {"xxxx"}; -- element a exclure de l'affichage ex: "iron" n'affichera pas Iron ore, Iron Block, Iron Helmet, ...
24-
		Updated Energy section to support new function names.
24+
local whitelist = {""}; -- ajouter les therme a afficher. si whitelist = "" pour desactiver
25-
	Changes since 1.6
25+
26-
		Made significant changed to how peripherals are checked and processed.
26+
27-
		Redesigned how the contents is processed and stored so that there is a timestamp.
27+
28-
		The timestamp allows remote computer information to be processed correctly.
28+
29-
		Each computer must have a unique id set, if the same id is used for two systems,
29+
30-
		then the last one to update the table will win and you won't see all the content.
30+
print("Demmarrage "..VersionInfo);
31-
		Added ItemsFullPercentAt and FluidFullPercentAt to set what level is considered full.
31+
32-
	Changes since 1.5
32+
33-
        Added a workaround to ignore BuildCraft pipes if attached
33+
34-
    Changes since 1.4
34+
35-
        Added support for PeripheralPlusOne on Minecraft 1.12.2
35+
36-
        Currently only supports ME Systems via the ME Bridge
36+
37-
	Changes since 1.3
37+
38-
		Changed filterexclude to blacklist and added whitelist
38+
39-
	Changes since 1.2
39+
40-
		Changed the way ME systems are processed as it was very slow.
40+
41-
		Added filtering so that certain names of items can be excluded.
41+
function padString (sText, iLen) -- longueur des text
42-
		This was again a problem on ME systems with a lot of items in them.
42+
43-
	Changes since 1.1
43+
44-
		Fixed a bug with tanks. Used to check if getInventoryName existed to determine if the
44+
45-
		peripheral is a tank or not. Now using the getType function instead.
45+
46-
		Changed the string.match to string.find
46+
47-
	http://youtu.be/KT-2hKjUpGA
47+
48-
--]]
48+
49
	if (iTextLen > iLen) then
50-
local isMain = true;
50+
51-
local id = "Main Base";
51+
52-
local UpdateIntervalSeconds = 60;
52+
53-
local ItemsFullPercentAt = 256;
53+
54-
local FluidFullPercentAt = 256;
54+
55
56-
local VersionInfo = "Base Monitor 1.9.1";
56+
function prepmonitor(objectmon) -- preparation du moniteur
57
	mon = peripheral.wrap(objectmon);
58
	if (mon.isColor() == false) then
59
		titleTextColor = colors.black;
60
		titleBackColor = colors.white;
61
		tankTextColor = colors.black;
62-
local chestTextColor = colors.white;
62+
63-
local chestBackColor = colors.purple;
63+
64
		chestBackColor = colors.white;
65
		cacheTextColor = colors.black;
66
		cacheBackColor = colors.white;
67
		powerTextColor = colors.black;
68
		powerBackColor = colors.white;
69-
--[[
69+
70-
	Add or remove values here to exclude items from being displayed.
70+
71-
	It is case insensitive and it will look for matches.
71+
72-
	So an item called "Nether Brick" will not be displayed if any of the following is set
72+
function updateTable(strSource,strName,strAmount,timestamp,strLegend) -- gestion des la table et affichage information
73-
	"Brick","brick","rick","ck"
73+
74-
	This also means that of you set a filter call "iron" then it will remove all items containing iron.
74+
75-
	it will not show Iron Ore, Iron Block, Iron Helmet, etc.
75+
76-
--]]
76+
77-
local blacklist = {"Flesh","Nugget","Brain","Sapling","Seed","egg","eye"};
77+
78-
--[[
78+
79-
	Add or remove values here to only show items matching these names.
79+
80-
	It is case insensitive and it will look for matches.
80+
81-
	So an item called "Nether Brick" will be displayed if any of the following is set
81+
82-
	"Brick","brick","rick","ck"
82+
83-
	This also means that of you set a filter called "iron" then it will show all items containing iron.
83+
84-
	it will show show Iron Ore, Iron Block, Iron Helmet, etc.
84+
85-
	To disable the whitelist set it to local whitelist = "";
85+
86-
	To enable the whitelist set it to local whitelist = {"Diamond","Gold"};
86+
87-
--]]
87+
88-
local whitelist = "";
88+
89
			end
90
		end
91
	end
92
	if (ContentData[strSource] == nil) then
93
		ContentData[strSource] = {};
94
	end
95-
print("Starting "..VersionInfo);
95+
96
		ContentData[strSource][timestamp] = {};
97
	end
98
	if (ContentData[strSource][timestamp][strName] == nil) then
99
		ContentData[strSource][timestamp][strName] = {};
100
	end
101
	if (ContentData[strSource][timestamp][strName]["count"] == nil) then
102
		ContentData[strSource][timestamp][strName]["count"] = strAmount;
103
	else
104
		ContentData[strSource][timestamp][strName]["count"] = ContentData[strSource][timestamp][strName]["count"] + strAmount;
105
	end
106-
function padString (sText, iLen)
106+
107
end
108
109
function printmon(strName,strAmount,strMax,strLegend)
110
	local textColor;
111
	local backColor;
112
	local FullPercentAt = ItemsFullPercentAt;
113
	if (strLegend == "#") then
114
		textColor = chestTextColor;
115
		backColor = chestBackColor;
116
		strLegend = "";
117
	end
118
	if (strLegend == "+") then
119
		textColor = tankTextColor;
120
		backColor = tankBackColor;
121-
function prepmonitor(objectmon)
121+
122
		strLegend = "";
123
	end
124
	if (strLegend == "*") then
125
		textColor = powerTextColor;
126
		backColor = powerBackColor;
127
		strLegend = "";
128
	end
129
	if (strLegend == "$") then
130
		textColor = cacheTextColor;
131
		backColor = cacheBackColor;
132
		strLegend = "";
133
	end
134
	local line = string.format("%s  %3i%s",padString(strName,NameLen+1),strAmount,padString(strLegend,1));
135
	if (strAmount >= 1000000) then
136
		line = string.format("%s  %3iM%s",padString(strName,NameLen),math.floor(strAmount/1000000),padString(strLegend,1));
137-
function updateTable(strSource,strName,strAmount,timestamp,strLegend)
137+
138
		line = string.format("%s  %3iK%s",padString(strName,NameLen),math.floor(strAmount/1000),padString(strLegend,1));
139
	end
140
141
	local ColPadding = 0;
142
	if (CurColumn > 0) then
143
		ColPadding = 1;
144
	end
145
	local CurX = math.floor((CurColumn*ColumnWidth))+math.floor(CurColumn*ColPadding)+1;
146
 	if (CurColumn == 0) then
147
		--  print("CurX:"..CurX);
148
 	end
149
	mon.setCursorPos(CurX,CurLine);
150
	--local percent = strAmount / strMax * 100;
151
	mon.setBackgroundColor(backColor);
152
	local percent = strAmount / FullPercentAt * 100;
153
	if (percent > 100) then percent = 100; end
154
	local barlength = math.floor(percent / 100 * (string.len(line)-1));
155
 	--if (CurColumn == 0) then
156
   	--	barlength = barlength + 1;
157
	--end
158
159
	if (string.len(line) > barlength) then
160
		local msg = string.sub(line,1,barlength);
161
		mon.setTextColor(textColor);
162
		mon.write(msg);
163
		--[[if (percent == 0) then
164
			mon.setBackgroundColor();
165
		else
166
			mon.setBackgroundColor(colors.black);
167
		end--]]
168
		mon.setBackgroundColor(colors.black);
169
		mon.setTextColor(backColor);
170
		mon.write(string.sub(line,barlength+1,-2))
171
	else
172
		local spaces = barlength - string.len(line);
173
		mon.write(line);
174
		mon.write(string.rep(" ",spaces));
175
	end
176
177
	mon.setTextColor(colors.white);
178
	CurColumn = CurColumn + 1;
179
	if (CurColumn > MaxColumn) then
180
		CurColumn = 0;
181
		CurLine = CurLine + 1;
182
	end
183
	return true;
184
end
185
186
function findMonitor() -- recherche moniteur
187
	for i,name in pairs(peripherals) do
188
		for j,method in pairs(peripheral.getMethods(name)) do
189
			if (method == 'getCursorPos') then
190
				prepmonitor(name);
191
			end
192
		end
193
	end
194
end
195
196
function findWirelessModem() -- recherche modem
197
	local foundWireless = false;
198
	for i,name in pairs(peripherals) do
199
		for j,method in pairs(peripheral.getMethods(name)) do
200
			if (method == 'isWireless') then
201
				wmod = peripheral.wrap(name);
202
				if (wmod.isWireless()) then
203
					wmod.closeAll();
204
					foundWireless = true;
205
					break;
206
				else
207
					wmod = {};
208
				end
209
			end
210
		end
211
		if (foundWireless) then
212
			break;
213
		end
214
	end
215
end
216
217
function collectLocalInfo()
218
	local timestamp = os.clock();
219
	for i,name in pairs(peripherals) do
220
		local p = peripheral.wrap(name);
221
		local displayNames = {};
222
		if (p.getTankInfo ~= nil) then -- RECHERCHE TANK
223
			print("Processing Tank");
224
			local iteminfo = p.getTankInfo();
225
			if (iteminfo ~= nil) then
226
				local displayname = "Empty";
227
				local amount = 0;
228
				if (iteminfo[1].contents) then
229
					displayname = iteminfo[1].contents.rawName;
230
					amount = iteminfo[1].contents.amount;
231
					amount = math.floor(amount/1000);
232
				end
233
				if (amount ~= 0) then
234
					updateTable(id,displayname,amount,timestamp,"+");
235
				end
236
			end
237
		end
238
		if (p.getStoredItems ~= nil) then -- RECHERCHE STOCKAGE ITEM (barrel)
239
			print("Processing Cache");
240
			local iteminfo = p.getStoredItems();
241
			if (iteminfo) then
242
				local displayname = iteminfo.display_name;
243
				updateTable(id,displayname,iteminfo.qty,timestamp,"$");
244
			end
245
		end
246
		if (p.getEnergyStored ~= nil) then -- RECHERCHE STOCAGE ENERGY
247
			print("Processing Energy");
248
			local energy = p.getEnergyStored();
249
			if (energy ~= nil) then
250
				updateTable(id,"Energy",energy,timestamp,"*");
251-
-- Find a monitor
251+
252-
function findMonitor()
252+
253
		if (p.getInventorySize ~= nil) then -- RECHERCHE STOCKAGE ITEM (chest)
254
			print("Processing Chest");
255
			local chestSize = p.getInventorySize();
256
			if (chestSize ~= nil) then
257
				local items = {};
258
				for j=1,chestSize,1 do
259
					local iteminfo = p.getStackInSlot(j);
260
					if (iteminfo) then
261
						displayname = iteminfo.display_name;
262-
-- Find a wireless modem
262+
263-
function findWirelessModem()
263+
264
								items[displayname] = iteminfo.qty;
265
							else
266
								items[displayname] = items[displayname] + iteminfo.qty;
267
							end
268
						end
269
					end
270
				end
271
				local k = 0;
272
				for key,val in pairs(items) do
273
					k = k + 1;
274
					updateTable(id,key,val,timestamp,"#");
275
				end
276
			end
277
		end
278
		if (p.getAvailableItems ~= nil) then -- RECHERCHE DONNEES ME / APPLIED ENERGETIC
279
			print("Processing ME System");
280
			local itemarray = p.getAvailableItems();
281
			if (itemarray ~= nil) then
282
				for j=1,table.getn(itemarray),1 do
283
					if (itemarray[j].size > 0) then
284
						local fingerprint = itemarray[j].fingerprint;
285
						if ( (displayNames[fingerprint.id] == nil) or (displayNames[fingerprint.id] == fingerprint.id)) then
286
							local ItemDetail = p.getItemDetail(fingerprint);
287
							if (ItemDetail ~= nil) then
288
								displayNames[fingerprint.id] = ItemDetail.basic().display_name;
289-
		if (p.getBlockData ~= nil) then
289+
290-
			print("Processing Advanced Peripherals");
290+
291-
			local blockdata = p.getBlockData();
291+
292-
			if (blockdata.Items ~= nil) then
292+
293-
				local blockitems;
293+
294-
				if (blockdata.Items.Items ~= nil) then
294+
295-
					blockitems = blockdata.Items.Items;
295+
296
			end
297-
					blockitems = blockdata.Items;
297+
298
        if (p.listItems ~= nil) then -- RECHERCHE BRIDGE ME
299-
				if (blockitems) then
299+
			print("Processing ME Bridge");
300-
					print("Processing Item Storage via Advanced Peripherals");
300+
301-
					local items = {};
301+
302-
					for j in pairs(blockitems) do
302+
303-
						local iteminfo = blockitems[j];
303+
304-
						displayname = iteminfo.id;
304+
305-
						if (blockitems[j].Item ~= nil) then -- Workaround for Mekanism bins
305+
306-
							displayname = blockitems[j].Item.id;
306+
307
					end
308
				end
309-
							if(string.find(displayname,":") ~= nil) then
309+
310-
								k = string.find(displayname,":")+1;
310+
311-
								displayname = string.sub(displayname,k);
311+
312
	end
313-
							displayname = string.gsub(displayname,"%s+"," ");
313+
314-
							local itemcount = iteminfo.Count;
314+
315-
							if (itemcount == nil and iteminfo.SizeOverride ~= nil) then
315+
function updateMonitor() -- mise a jour moniteur
316-
								itemcount = iteminfo.SizeOverride;
316+
317
	ColumnWidth = NameLen + 7;
318-
							if (itemcount == nil) then
318+
319-
								itemcount = 0;
319+
320
	mon.clear();
321
	CurColumn = 0;
322-
								items[displayname] = itemcount;
322+
323
	mon.setCursorPos(1,1);
324-
								items[displayname] = items[displayname] + itemcount;
324+
325
	mon.setTextScale(0.5);
326
	mon.write(VersionInfo);
327
	-- Tri par nom de base
328-
					local k = 0;
328+
329-
					for key,val in pairs(items) do
329+
330-
						k = k + 1;
330+
331-
						updateTable(id,key,val,timestamp,"#");
331+
332
	table.sort(sortedSources);
333
	local name = "";
334
	for i,source in ipairs(sortedSources) do
335-
			if (blockdata.GasTanks) then
335+
336-
				print("Processing Gas Storage via Advanced Peripherals");
336+
337
			CurColumn = 0;
338-
				for j in pairs(blockdata.GasTanks) do
338+
339-
					local iteminfo = blockdata.GasTanks[j];
339+
340-
					displayname = iteminfo.stored.gasName;
340+
341-
					if (displayname) then
341+
			mon.write(padString("Stock de "..name,x-1));
342-
						if(string.find(displayname,":") ~= nil) then
342+
343-
							k = string.find(displayname,":")+1;
343+
344-
							displayname = string.sub(displayname,k);
344+
345
		for timestamp in pairs(ContentData[source]) do
346-
						displayname = string.gsub(displayname,"%s+"," ");
346+
347-
						local amount = 0;
347+
348-
						if (not items[displayname]) then
348+
349-
							amount = iteminfo.stored.amount;
349+
350-
							if (amount == nil) then
350+
351-
								amount = 0;
351+
352
				latest = sortedTimestamps[j];
353-
							amount = math.floor(amount/1000);
353+
354-
							items[displayname] = amount;
354+
355-
						else
355+
356-
							amount = iteminfo.stored.amount;
356+
357-
							if (amount == nil) then
357+
358-
								amount = 0;
358+
359-
							end	
359+
360-
							amount = math.floor(amount/1000);
360+
361-
							items[displayname] = items[displayname] + amount;
361+
362
			printmon(displayname,count,max,legend);
363
		end
364
	end
365
end
366
367
-----------------------------------------------PROGRAM---------------------------------------------------
368-
					updateTable(id,key,val,timestamp,"+");
368+
369
findWirelessModem();
370
371-
			if (blockdata.FluidTanks) then
371+
372-
				print("Processing Fluid Storage via Advanced Peripherals");
372+
373
		wmod.open(mainChannel);
374-
				for j in pairs(blockdata.FluidTanks) do
374+
375-
					local iteminfo = blockdata.FluidTanks[j];
375+
		print("Pas de modem sur le PC principal");
376-
					displayname = iteminfo.stored.FluidName;
376+
377-
					if (displayname) then
377+
378-
						if(string.find(displayname,":") ~= nil) then
378+
379-
							k = string.find(displayname,":")+1;
379+
380-
							displayname = string.sub(displayname,k);
380+
381
local timerUpdate = os.startTimer(UpdateIntervalSeconds);
382-
						displayname = string.gsub(displayname,"%s+"," ");
382+
383-
						local amount = 0;
383+
384-
						if (not items[displayname]) then
384+
385-
							amount = iteminfo.stored.Amount;
385+
386-
							if (amount == nil) then
386+
387-
								amount = 0;
387+
388
-- Main program loop
389-
							amount = math.floor(amount/1000);
389+
390-
							items[displayname] = amount;
390+
391-
						else
391+
	print("Event recus :"..event);
392-
							amount = iteminfo.stored.Amount;
392+
393-
							if (amount == nil) then
393+
394-
								amount = 0;
394+
395-
							end	
395+
396-
							amount = math.floor(amount/1000);
396+
397-
							items[displayname] = items[displayname] + amount;
397+
398
				end
399
			end
400
			updateMonitor();
401
			wirelessEventCount = 0;
402
			timerUpdate = os.startTimer(UpdateIntervalSeconds);
403
		end
404-
					updateTable(id,key,val,timestamp,"+");
404+
405
	if (event == "modem_message") then
406
		if (isMain == true) then
407
			wirelessEventCount = wirelessEventCount + 1;
408-
		if (p.getTankInfo ~= nil or p.getFilledPercentage ~= nil or p.tanks) then
408+
409
				if (ContentData[source] == nil) then
410-
			local iteminfo;
410+
411-
			if (p.getTankInfo ~= nil) then
411+
412-
				iteminfo = p.getTankInfo();
412+
413
			end
414-
			if (p.getFilledPercentage ~= nil) then
414+
415-
				iteminfo = p.getStored();
415+
416
			end
417-
			if (p.tanks ~= nil) then
417+
418-
				tankinfo = p.tanks(); -- Industrial Foregoing blackhole tank
418+
419-
				if (tankinfo[1] ~= nil) then
419+
420-
					iteminfo = tankinfo[1];
420+
421
		updateMonitor();
422
	end
423
	if (event == "peripheral") or (event == "peripheral_detach") then
424
		print("Updating the peripheral list");
425
		peripherals = peripheral.getNames();
426-
				if (iteminfo[1] ~= nil) then
426+
427-
					if (iteminfo[1].contents) then
427+
end