Created
January 11, 2018 21:31
-
-
Save mkarneim/f1a9ec69aa098a718222c4a53eff0aef to your computer and use it in GitHub Desktop.
WoL Claim spell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local pkg = {} | |
| local base = Vec3(0,0,0) | |
| local homes = {} | |
| local owners = {} | |
| local width = 16*1 | |
| local function saveData() | |
| local text = "" | |
| for i=1,#homes do | |
| local home = homes[i] | |
| local owner = owners[i] | |
| if i>1 then | |
| text = text..", " | |
| end | |
| text = text.."{"..home.x..","..home.y.."," ..home.z..",'"..owner.."'".."}" | |
| end | |
| spell.pos = base | |
| spell.block = Blocks.get("command_block"):withNbt({Command=text}) | |
| end | |
| local function loadData() | |
| spell.pos = base | |
| if spell.block.nbt then | |
| local text = spell.block.nbt.Command | |
| if text ~= nil then | |
| local code = "return {"..text.."}" | |
| local func = load(code) | |
| local list = func() | |
| homes = {} | |
| owners = {} | |
| for i,chunk in pairs(list) do | |
| local home = Vec3(chunk[1],chunk[2],chunk[3]) | |
| local owner = chunk[4] | |
| table.insert(homes,home) | |
| table.insert(owners,owner) | |
| end | |
| end | |
| end | |
| end | |
| local function onRightClick(event) | |
| local changed = false | |
| spell.pos = event.pos | |
| spell:move(event.face) | |
| if spell.block.nbt and spell.block.nbt.Owner then | |
| local name = spell.block.nbt.Owner.Name | |
| local pos = spell.pos | |
| --say("New home: "..name.." -> "..pos) | |
| table.insert(homes,pos) | |
| table.insert(owners,name) | |
| changed = true | |
| end | |
| if changed then | |
| saveData() | |
| end | |
| end | |
| local function blockIsPlayerHead(pos,owner) | |
| spell.pos = pos | |
| if spell.block.nbt and spell.block.nbt.Owner then | |
| local name = spell.block.nbt.Owner.Name | |
| if name == owner then | |
| return true | |
| end | |
| end | |
| return false | |
| end | |
| local function updateHomes() | |
| local changed = false | |
| for i=#homes,1,-1 do | |
| local home = homes[i] | |
| local owner = owners[i] | |
| if not blockIsPlayerHead(home,owner) then | |
| table.remove(homes,i) | |
| table.remove(owners,i) | |
| changed = true | |
| end | |
| end | |
| if changed then | |
| saveData() | |
| end | |
| end | |
| local function clearHomes() | |
| local changed = false | |
| local air = Blocks.get("air") | |
| for i=#homes,1,-1 do | |
| local home = homes[i] | |
| local owner = owners[i] | |
| if blockIsPlayerHead(home,owner) then | |
| spell.pos = home | |
| spell.block = air | |
| changed = true | |
| end | |
| end | |
| if changed then | |
| homes = {} | |
| ownerd = {} | |
| saveData() | |
| end | |
| end | |
| local function updatePlayer( player) | |
| local areas = {} | |
| for i=1,#homes do | |
| local home = homes[i] | |
| local owner = owners[i] | |
| if player.name ~= owner then | |
| areas[#areas+1] = { | |
| minX=home.x-width, | |
| maxX=home.x+width, | |
| minZ=home.z-width, | |
| maxZ=home.z+width | |
| } | |
| end | |
| end | |
| if player.gamemode == "survival" and player:isInside( areas) then | |
| player.gamemode = "adventure" | |
| elseif player.gamemode == "adventure" and not player:isInside( areas) then | |
| player.gamemode = "survival" | |
| end | |
| end | |
| function pkg.unclaim() | |
| spell:singleton("mickkay.claim") | |
| base = spell.pos | |
| loadData() | |
| updateHomes() | |
| clearHomes() | |
| end | |
| function pkg.claim(options) | |
| spell:singleton("mickkay.claim") | |
| options = options or {width=width} | |
| base = spell.pos | |
| width = options.width | |
| loadData() | |
| updateHomes() | |
| saveData() | |
| local q = Events.connect("RightClickBlockEvent") | |
| while true do | |
| local event = q:pop(20) | |
| if event ~= nil then | |
| onRightClick(event) | |
| end | |
| local players = Entities.find("@a") | |
| for i,player in pairs(players) do | |
| updatePlayer(player) | |
| end | |
| updateHomes() | |
| end | |
| end | |
| Help.on(pkg.claim) [[ | |
| () - Allows players to 'claim' an area by placing their own head in the center of it. | |
| ]] | |
| return pkg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment