This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
xLife's profile picture

Enderchest / Endertank Register --See which Colorcodes are used

Started by xLife, 04 October 2014 - 03:46 AM
xLife #1
Posted 04 October 2014 - 05:46 AM
This script only works with advanced Computers!

In this script you can register your colorcodes for an Enderchest / Endertank and anybody that has access to that computer, can see which colorcodes are used.


Screenshots:
Spoiler



Changelog:
Spoilerv1.3
Fixed for CC 1.6 is now working again
v1.2
Max Characters are now 16!
v1.1
Got rid of the big table. Now there are 12 Enderchests instead of 6 per Site.

The Code:
Spoiler
--#This is a Luascript for Computercraft.
--#It only works on advanced computers.
--#It should prevent people from using the same Colorcode.
--#I hope you like it!
--#© xilef8899

function debugWr(msg)
   --term.setCursorPos(1,1)
   --term.write(msg)
end

function box (txt, x, y, b, h, bg, tc)
   term.setBackgroundColor(bg)
   term.setTextColor(tc)
   for i1 = 1, h do
	  term.setCursorPos(x,y+i1-1)
	  nh = h
	  if nh <= 1 then nh = nh*2 elseif nh <= 3 then nh = nh*1.5 end
	  if i1 == math.floor(nh/2) then
		 for i2 = 1, math.floor((b-string.len(txt))/2) do
			term.write(" ")
		 end
		 term.write(txt)
		 for i2 = 1, math.ceil((b-string.len(txt))/2) do
			term.write(" ")
		 end
	  else
		 for i2 = 1, b do
			term.write(" ")
		 end
	  end
   end
end

function cl()
   term.setBackgroundColor(colors.black)
   term.clear()
end

nColors = {
colors.white, colors.orange, colors.magenta, colors.lightBlue,
colors.yellow, colors.lime, colors.pink, colors.gray,
colors.lightGray, colors.cyan, colors.purple, colors.blue,
colors.brown, colors.green, colors.red, colors.black
}

--#// START of Startmenupart //--
function displaymenu()
   cl()
   box("Menu",2,2,49,1,colors.lightGray,colors.black)
   box("Register",2,4,24,7,colors.gray,colors.white)
   box("Enderchest",2,7,24,1,colors.gray,colors.white)
   box("List",2,12,24,7,colors.gray,colors.white)
   box("Enderchests",2,15,24,1,colors.gray,colors.white)
   box("Register",27,4,24,7,colors.gray,colors.white)
   box("Endertank",27,7,24,1,colors.gray,colors.white)
   box("List",27,12,24,7,colors.gray,colors.white)
   box("Endertanks",27,15,24,1,colors.gray,colors.white)
end

function checkmenu()
   displaymenu()
   local event, button, xPos, yPos = os.pullEvent("mouse_click")
   if xPos >= 2 and xPos <= 25 and yPos >= 4 and yPos <= 10 then
	  displayreg("chest")
	  debugWr(1)
   elseif xPos >= 2 and xPos <= 25 and yPos >= 12 and yPos <= 18 then
	  nSite = 1 --The Actual Site
	  displaylist("chest")
	  debugWr(2)
   elseif xPos >= 27 and xPos <= 50 and yPos >= 4 and yPos <= 10 then
	  displayreg("tank")
	  debugWr(3)
   elseif xPos >= 27 and xPos <= 50 and yPos >= 12 and yPos <= 18 then
	  nSite = 1 --The Actual Site
	  displaylist("tank")
	  debugWr(4)
   end
   debugWr("button: "..button.." X: "..xPos.." Y: "..yPos)
end
--#// END of Startmenupart //--

--#// START of Registerpart //--
function displayreg(tankchest)
   cl()
   box("Register Ender"..tankchest,2,2,49,1,colors.lightGray,colors.black)
   box("",23,6,7,3,colors.gray,colors.white)
   term.setBackgroundColor(colors.black)
   term.setCursorPos(24,7)
   term.write(" ")
   term.setCursorPos(26,7)
   term.write(" ")
   term.setCursorPos(28,7)
   term.write(" ")
   displaycol()
   displaySure()
   checkname(tankchest)
   checkcol()
   checkSure(tankchest)
end

function displaycol()
   for i = 1, #nColors do
	  term.setCursorPos(i*3+2,10)
	  term.setBackgroundColor(nColors[i])
	  term.write(" ")
   end
end

function displaySure()
   term.setCursorPos(23,12)
   term.setBackgroundColor(colors.lime)
   term.write("Yes/")
   term.setBackgroundColor(colors.red)
   term.write("No?")
end

function checkname(tankchest)
   box("Type a name! (1-16 Characters) Press Enter",2,18,49,1,colors.lightGray,colors.black)
   box("Name: ".."				",14,4,23,1,colors.gray,colors.black)
   term.setCursorPos(20,4)
   input = read()
   if string.len(input) > 16 or string.len(input) < 1 then
	  displayreg(tankchest)
   end
end

function checkcol()
   box("Click 3 Colors!",2,18,49,1,colors.lightGray,colors.black)
   ColCode = {}
   repeat
	  local event, button, xPos,yPos = os.pullEvent("mouse_click")
	  for i = 1, #nColors do
		 if xPos == i*3+2 and yPos == 10 then
			table.insert(ColCode, nColors[i])
		 end
	  end
	  displayColCode()
   until #ColCode == 3
end

function displayColCode()
   for i = 1, #ColCode do
   term.setCursorPos(22+i*2,7)
   term.setBackgroundColor(ColCode[i])
   term.write(" ")
   end
end

function checkSure(tankchest)
   box("Are you Sure?",2,18,49,1,colors.lightGray,colors.black)
   repeat
	  local event, button, xPos,yPos = os.pullEvent("mouse_click")
	  if xPos >= 23 and xPos <= 26 and yPos == 12 then
		 save(tankchest)
		 cl()
		 box("Ender"..tankchest.." saved!",2,9,49,3,colors.lime,colors.black)
	  elseif xPos >= 27 and xPos <= 29 and yPos == 12 then
		 cl()
		 box("Ender"..tankchest.." discarded!",2,9,49,3,colors.red,colors.black)
	  end
   until xPos >= 23 and xPos <= 29 and yPos == 12
   sleep(2.5)
end

function save(tankchest) --#tank / Chest -- Name -- ColCode
   local nFile = fs.open(tankchest, fs.exists(tankchest) and "a" or "w")
   nFile.writeLine(string.gsub(textutils.serialize({input, ColCode[1], ColCode[2], ColCode[3]}), "\n", ""))
   nFile.flush()
   nFile.close()
end
--#// END of Registerpart //--

--#// START of Listpart //--
function displaylist(tankchest)
   repeat
	  cl()
	  box("List of all Ender"..tankchest.."s! Site: "..nSite,2,2,49,1,colors.lightGray,colors.black)
	  --#  " 1 | 2 | 3	  Name	   | 1 | 2 | 3 " 51  19
	  --#  "	 Name	  | 1 | 2 | 3 " 51  19
	  collectdata(tankchest)
	  if sites > 0 then
		 displaydata()
		 checkdata(tankchest)
	  else
		 cl()
		 box("There are no Ender"..tankchest.."s registered",2,9,49,3,colors.red,colors.black)
		 sleep(3)
		 menu = true
	  end
   until menu == true
end
--#   Table   -   Site   -   Row   -   String,number,number,number
--#   Data   -   1~999   -   1~6   -   Name,Col1,Col2,Col3
--#data[site][row][1][1] = name
--#data[site][row][1][2] = Col1
--#data[site][row][1][3] = Col2
--#data[site][row][1][4] = Col3
function collectdata(tankchest)
   data = {}
   if fs.exists(tankchest) then
	  nFile = fs.open(tankchest, "r")
	  sites = 1 --#Number of Sites
	  stop = false
	  row = 0
	  repeat
		 str = nFile.readLine()
		 if str ~= nil then
			if row == 0 then
			   table.insert(data, {{},{},{},{},{},{},{},{},{},{},{},{}})
			end
			row = row+1
			table.insert(data[sites][row], textutils.unserialize(str))
		 else stop = true
		 end
		 if row == 12 then sites = sites+1 row = 0 end
	  until stop == true
	  if sites >= 1 and row == 0 then sites = sites-1 end
	  nFile.close()
   end
end

function displaydata()
   if data[nSite] ~= nil then
	  nRows = 0
	  for i = 1, 12 do
		 if data[nSite][i][1] ~= nil then
			nRows = nRows+1
		 end
	  end
	  term.setBackgroundColor(colors.gray)
	  if nRows > 6 then x = 2 else x = 1 end
	  for i = 1, nRows*2+x do
		 for i2 = 1, 7 do
			if i <= 13 then
			   term.setCursorPos(i2+1,i+3)
			else
			   term.setCursorPos(i2+26,(i-12)+2)
			end
			term.write(" ")
		 end
	  end
	  for row = 1, nRows do
		 if row <= 6 then
			for i = 1, 3 do --#Displaying the Colors for the Row
			   term.setCursorPos(1+i*2,row*2+3)
			   term.setBackgroundColor(data[nSite][row][1][i+1])
			   term.write(" ")
			end
			term.setBackgroundColor(colors.gray)
			term.setCursorPos(10,row*2+3)
			term.write(data[nSite][row][1][1])
		 else
			for i = 1, 3 do
			   term.setCursorPos(26+i*2,(row-6)*2+3)
			   term.setBackgroundColor(data[nSite][row][1][i+1])
			   term.write(" ")
			end
			term.setBackgroundColor(colors.gray)
			term.setCursorPos(35,(row-6)*2+3)
			term.write(data[nSite][row][1][1])
		 end
	  end
   end
end

function checkdata()
   menu = false
   box("<<",2,18,10,1,colors.gray,colors.black)
   box("Back to the Menu",12,18,29,1,colors.red,colors.black)
   box(">>",41,18,10,1,colors.gray,colors.black)
   repeat
	  local event, button, xPos,yPos = os.pullEvent("mouse_click")
	  if xPos >= 2 and xPos <= 11 and yPos == 18 and nSite > 1 then
		 nSite = nSite-1
	  elseif xPos >= 12 and xPos <= 40 and yPos == 18 then
		 menu = true
	  elseif xPos >= 41 and xPos <= 50 and yPos == 18 and nSite < sites then
		 nSite = nSite+1
	  end
   until xPos >= 2 and xPos <= 50 and yPos == 18
end
--#// END of Listpart //--

while true do
   checkmenu()
end


Pastebin: http://pastebin.com/CzADqdyH


PS: Sorry for my bad English!
Edited on 09 May 2015 - 10:04 PM
Dragon53535 #2
Posted 04 October 2014 - 10:05 AM
Quick tip, the forum uses a python like comment system

--#This is a forum and lua safe comment.
--This isn't a forum safe comment.
--OH GOD I AM GREEN! SOMEONE MAKE IT STOP!
Other than that you should indent your code, it makes it easier to read. (and debug)

Code wise, it looks neat.
xLife #3
Posted 04 October 2014 - 02:52 PM
Thank you. I've edited it. The comments should be working now. I've also replaced tab with 3 spaces. :D/>
kwstoudt #4
Posted 10 October 2014 - 10:03 PM
Is it ok if I distribute this on a server I play on? (will not be making in-game or irl money off of it)
xLife #5
Posted 13 October 2014 - 08:13 PM
@kwstoudt: Of course, you can use it however you want. :D/>
kwstoudt #6
Posted 14 October 2014 - 06:18 PM
thanks!
Jonnhycraft #7
Posted 12 June 2015 - 08:58 PM
Could you make a program like that to work on a pocket computer? I think it would be better if you could carry this information around with you, in case you need to place another tank or chest somewhere else.