Skip to content

Instantly share code, notes, and snippets.

@Cougar
Last active August 29, 2015 14:18
Show Gist options
  • Save Cougar/2c9cd3ee530d3e356265 to your computer and use it in GitHub Desktop.
Save Cougar/2c9cd3ee530d3e356265 to your computer and use it in GitHub Desktop.
minecraft-wrapper protection.py plugin worinkg draft
import json, os, traceback
NAME = "protection"
AUTHOR = "C0ugar"
ID = "net.version6.minecraft.plugins.protection"
SUMMARY = "Protection commands"
DESCRIPTION = """Protection plugin."""
WEBSITE = ""
VERSION = (0, 1)
class Main:
def __init__(self, api, log):
self.api = api
self.minecraft = api.minecraft
self.log = log
self.log.debug("protection __init__")
def onEnable(self):
self.data = self.api.getStorage("protection", True)
self.api.registerHelp("Protection", "Commands from the Protection plugin", [
("/prot", "prot", None),
])
self.data["protection"] = []
self.api.registerEvent("player.dig", self.dig)
self.api.registerEvent("player.place", self.place)
self.api.registerEvent("player.interact", self.interact)
self._add_prot("Test", -20, -20, 20, 20, []) # test position around zero for #172
self._add_prot("Server", -945, -1149, -1045, -1249, []) # spawn
self._add_prot("Server", 7541, 4588, 7581, 4628) # winter
self._add_prot("Server", 479, -7782, 499, -7762) # north
self._add_prot("Server", 451, 5304, 471, 5314) # south
self._add_prot("Server", 6089, 18, 6109, 38) # east
self._add_prot("Server", -7897, -1500, -7917, -1520) # west
self._add_prot("C0ugar", 1435, -2979, 1348, -2903)
self._add_prot("KingTonis", -1706, 8225, -1609, 8168, [ "KingTonis", "KingZombieHans" ])
def onDisable(self):
self.data = []
self.data["protection"] = []
self.data.save()
def _add_prot(self, owner, x1, z1, x2, z2, members = []):
if not owner in members:
members.append(owner)
self.data["protection"].append({ "owner": owner, "members": members, "south": max(z1, z2), "north": min(z1, z2), "west": min(x1, x2), "east": max(x1, x2)})
def dig(self, payload):
player = payload["player"]
prot = self._is_prot(player, payload["position"])
if not prot:
self.log.debug("prot: no prot... ")
return True
if player.username in prot["members"]:
self.log.debug("prot: your prot... ");
return True
if payload["action"] == "begin_break":
self.log.debug("prot: protected 1 !")
player.message({"text": "You don't have %s's permission to build here." % prot["owner"], "color": "red"});
self.log.debug("prot: send bedrock")
player.getClient().send(0x23, "position|varint", (payload["position"], 7 << 4))
self.log.debug("prot: sent")
return False
self.log.debug("prot: protected 2 !")
player.message({"text": "You don't have %s's permission to build here." % prot["owner"], "color": "red"});
def place(self, payload):
player = payload["player"]
prot = self._is_prot(player, payload["position"])
if not payload["item"]:
if not prot:
return True
if player.username in prot["members"]:
return True
player.getClient().send(0x23, "position|varint", (payload["position"], 0))
return False
if payload["item"]["id"] == 280: # Stick
if not prot:
player.message({"text": "No one has claimed this block.", "color": "aqua"})
return True
player.message({"text": "This block has been claimed by %s." % prot["owner"], "color": "aqua"})
dx = prot["east"] - prot["west"]
dy = prot["south"] - prot["north"]
player.message({"text": " %d x %d = %d" % (dx, dy, dx * dy), "color": "aqua"})
else:
if not prot:
return True
if player.username in prot["members"]:
return True
player.message({"text": "You don't have %s's permission to build here." % prot["owner"], "color": "red"});
player.getClient().send(0x23, "position|varint", (payload["position"], 0))
return False
def interact(self, payload):
player = payload["player"]
self.log.debug("interact: %s" % player)
prot = self._is_prot(player, payload["position"])
if not prot:
self.log.debug("prot int: no prot... ");
return True
if player.username in prot["members"]:
self.log.debug("prot int: member")
return True
self.log.debug("prot int: protected 3 !")
return True
def _is_prot(self, player, position):
x, y, z = position;
self.log.debug("prot: test (%d, %d, %d)!" % (x, y, z))
for prot in self.data["protection"]:
#self.log.debug("prot: test? (%d, %d, %d, %d)" % (prot["east"], prot["west"], prot["south"], prot["north"]))
if x > prot["east"] or x < prot["west"] or z > prot["south"] or z < prot["north"]:
continue
if player.username in prot["members"]:
self.log.debug("prot: your land (%d, %d, %d, %d) owner=%s, members=%s" % (prot["east"], prot["west"], prot["south"], prot["north"], prot["owner"], str(prot["members"])))
return prot
self.log.debug("prot: Not your land (%d, %d, %d, %d)" % (prot["east"], prot["west"], prot["south"], prot["north"]))
return prot
self.log.debug("prot: free land")
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment