View difference between Paste ID: 2CQGCCY1 and cXTMm7G1
SHOW: | | - or go back to the newest paste.
1-
--File: /startup
1+
--File: /worldServer
2-
--Install : pastebin get cXTMm7G1 startup
2+
3
dofile("/cattech/common.lua")
4-
function downloadFromPastebin(pastebinCode, filename)
4+
dofile("/cattech/serverops.lua")
5-
    local url = "https://pastebin.com/raw/" .. pastebinCode  -- Construct the URL
5+
6-
    local response, err = http.get(url)  -- Perform the HTTP GET request
6+
function putItemsInContainer(x,y,z,items)
7
	local itemsString = table.concat(items,",")
8-
    if response then
8+
	local cmd = "/data merge block " .. x .. " " .. y .. " " .. z .. " {Items:[" .. itemsString .. "]}"
9-
        local file = fs.open(filename, "w")  -- Open a file for writing
9+
	log("debug","putItemInContainer : " .. cmd)
10-
        file.write(response.readAll())  -- Write the response to the file
10+
	executeCommand(cmd)
11-
        file.close()  -- Close the file
11+
12-
        response.close()  -- Close the HTTP response
12+
13-
        print(pastebinCode  .. " -> " .. filename)
13+
function restockStore() 
14-
    else
14+
	local items = {}
15-
        error("Pastebin download " .. pasetebinCode .. " error: " .. err)
15+
	for slot = 0, 8 do
16-
    end
16+
		table.insert(items ,"{Slot:" .. slot .. ",Count:" .. 1 .. ",id:\"minecraft:beef\"}")
17
		table.insert(items ,"{Slot:" .. slot+9 .. ",Count:" .. 1 .. ",id:\"minecraft:mutton\"}")
18
		table.insert(items ,"{Slot:" .. slot+18 .. ",Count:" .. 1 .. ",id:\"minecraft:porkchop\"}")
19-
-- Function to create a directory
19+
	end
20-
function createDirectory(dirName)
20+
	putItemsInContainer(43,88,51,items)
21-
    if not fs.exists(dirName) then  -- Check if the directory already exists
21+
22-
        fs.makeDir(dirName)  -- Create the directory
22+
	items = {}
23-
        print("Directory created: " .. dirName)
23+
	for slot = 0, 26 do
24-
    else
24+
		table.insert(items ,"{Slot:" .. slot .. ",Count:" .. 1 .. ",id:\"minecraft:leather\"}")
25-
        print("Directory already exists: " .. dirName)
25+
	end
26-
    end
26+
	putItemsInContainer(43,88,50,items)
27
28
	items = {}
29-
-- Main program starts here
29+
	for slot = 0, 8 do
30-
print("Downloading programs from Pastebin...")
30+
		table.insert(items ,"{Slot:" .. slot .. ",Count:" .. 1 .. ",id:\"minecraft:rabbit\"}")
31-
fs.makeDir("cattech" )
31+
		table.insert(items ,"{Slot:" .. slot+9 .. ",Count:" .. 1 .. ",id:\"minecraft:rabbit_hide\"}")
32-
downloadFromPastebin("gBBQJrAH", "/cattech/file.lua")
32+
		table.insert(items ,"{Slot:" .. slot+18 .. ",Count:" .. 1 .. ",id:\"minecraft:rabbit_foot\"}")
33-
dofile("/cattech/file.lua")
33+
	end
34-
downloadNamedFileFromPastebin("0xjcxant")
34+
	putItemsInContainer(43,88,49,items)
35
36-
dofile("/cattech/updateAll.lua")
36+
	items = {}
37
	for slot = 0, 8 do
38-
-- Define an array where each element is a pair: {pattern, program}
38+
		table.insert(items ,"{Slot:" .. slot .. ",Count:" .. 1 .. ",id:\"minecraft:ink_sac\"}")
39-
local patternPrograms = {
39+
		table.insert(items ,"{Slot:" .. slot+9 .. ",Count:" .. 1 .. ",id:\"minecraft:glow_ink_sac\"}")
40-
    --{"MiningTurtle%d+", "mining_program.lua"}, -- Matches "MiningTurtle" followed by digits
40+
	end
41-
    {"WorldServer", "/worldServer"},
41+
	putItemsInContainer(43,88,48,items)
42-
    {"testing", "/testCode"},
42+
43-
}
43+
	items = {}
44
	for slot = 0, 8 do
45-
local initialFuel = turtle.getFuelLevel()
45+
		table.insert(items ,"{Slot:" .. slot .. ",Count:" .. 1 .. ",id:\"minecraft:chicken\"}")
46-
print("Starting at " .. initialFuel .. " fuel")
46+
		table.insert(items ,"{Slot:" .. slot+9 .. ",Count:" .. 1 .. ",id:\"minecraft:feather\"}")
47
	end
48-
local turtleName = os.getComputerLabel()
48+
	putItemsInContainer(43,88,47,items)
49
50-
if not turtleName then
50+
	items = {}
51-
    print("Turtle has no name, no auto start.")
51+
	for slot = 0, 8 do
52-
    return
52+
		table.insert(items ,"{Slot:" .. slot .. ",Count:" .. 1 .. ",id:\"waystones:waystone\"}")
53-
else 
53+
	end
54-
    for _, pair in ipairs(patternPrograms) do
54+
	putItemsInContainer(43,88,45,items)
55-
        local pattern, program = pair[1], pair[2]
55+
56-
        -- Check if the turtle's name matches the pattern
56+
57-
        if string.match(turtleName, pattern) then
57+
58-
            -- Check if the program exists
58+
-- Make sure the modem is open for communication
59-
            if fs.exists(program) then
59+
local modemSide = "top"
60-
                print("Pattern matched: " .. pattern .. ". Starting program: " .. program)
60+
rednet.open(modemSide)
61-
                shell.run(program)
61+
62-
                return
62+
log("info","Listening as 'worldServer'")
63-
            else
63+
64-
                print("Error: Program " .. program .. " not found.")
64+
local chunkTimeout = 0
65-
                return
65+
local restockTimeout = 0
66-
            end
66+
while true do
67-
        end
67+
    local senderId, message, protocol = rednet.receive("worldServer",5)
68-
    end
68+
69-
    print("Turtle name " .. turtleName .. " no autostart")
69+
    if message then
70-
end
70+
		local processed=false
71
	    local cmd,data = string.match(message, "^(%S+)%s+(.+)$")
72
73
	    if cmd == "chunkLoad" then
74
	        log("debug","Chunkload coordinates received: " .. data)
75
76
			local x, y, z = data:match("(%S+)%s+(%S+)%s+(%S+)")
77
78
			addChunkLoad(tonumber(x),tonumber(y),tonumber(z),5)
79
			chunkTimeout = 0 -- Trigger a new check immediately.
80
81
			processed=true
82
	    end
83
84
		if not processed then
85
	        log("info","Unhandled [" .. senderId .. "]: " .. message)
86
	    end
87
	end
88
89
	io.write(".")
90
91
	if (restockTimeout < deltaTime(0)) then
92
		log("debug","Restocking Store")
93
		restockStore()
94
		restockTimeout = deltaTime(60 * 10) -- Restock every 15 minutres
95
	end
96
97
	log("debug",chunkTimeout .. "=C=" .. deltaTime(0))
98
	if (chunkTimeout < deltaTime(0)) then
99
		log("debug","Chunks Processing")
100
		handleChunkLoad()
101
		chunkTimeout = deltaTime(60 * 1) -- Check in 1 minute to handle chunks
102
	end
103
end
104
105
106