Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #script by MegaStar
- #Version 1.2
- from pyspades.constants import *
- from commands import add, get_player, admin
- from pyspades.server import grenade_packet
- from pyspades.common import Vertex3
- from pyspades.world import Grenade
- from pyspades.server import block_action, set_color
- from pyspades.common import make_color
- import json
- RANGE_CLAIM = 10 #size of the sector, remember that it is double the size, therefore it would be 20x20 blocks
- MULTI_CLAIMS = True
- #-------------------------------------------------------------#
- #Quick functions to build and remove a block
- global_claimed = []
- def fastblock(connection, (x, y, z), color): #to place a block
- set_color.value = make_color(*color)
- set_color.player_id = 32
- connection.protocol.send_contained(set_color)
- block_action.player_id = 32
- block_action.value = BUILD_BLOCK
- block_action.x = x
- block_action.y = y
- block_action.z = z
- connection.protocol.send_contained(block_action)
- connection.protocol.map.set_point(x, y, z, color)
- def removeblock(connection, (x, y, z)): #to remove a block
- block_action.player_id = connection.player_id
- block_action.value = DESTROY_BLOCK
- block_action.x = x
- block_action.y = y
- block_action.z = z
- connection.protocol.send_contained(block_action)
- connection.protocol.map.remove_point(x, y, z)
- #-------------------------------------------------------------#
- #-------------------------------------------------------------#
- #Open, edit and save data
- def open_data():
- with open("users.json", "r") as users:
- datos = json.load(users)
- return datos
- def save_data(data):
- with open("users.json","w") as save_data:
- json.dump(data, save_data)
- def edit_claims(connection, key, user, type):
- for claim_users in connection.protocol.claim_coords:
- if claim_users[0] == key:
- if type == "remove":
- claim_users[3].remove(user)
- return
- elif type == "add":
- claim_users[3].append(user)
- return
- #-------------------------------------------------------------#
- def buildregister(connection, username, password):
- #with open("users.json", "r") as users:
- datos = open_data()
- for key, value in datos.items():
- if username == key:
- return connection.send_chat("Este username ya esta siendo usado.")
- elif MULTI_CLAIMS == False:
- if connection.address[0] == value["ip"]:
- return connection.send_chat("Usted actualmente ya cuenta con un sector.")
- datos[username] = {"ip": connection.address[0], "password": password, "coordx": False, "coordy": False, "shared": []}
- save_data(datos)
- print(datos)
- return connection.send_chat("%s acabas de registrarte correctamente, para logearte escribe /buildlogin <username> <password>" % connection.name)
- def buildlogin(connection, username, password):
- if not connection.login:
- datos = open_data()
- for key, value in datos.items():
- if username == key:
- if password == value["password"]:
- connection.login = True
- connection.username = username
- return connection.send_chat("Acabas de logearte como %s" % username)
- else:
- return connection.send_chat("La contrasenia es incorrecta.")
- return connection.send_chat("La cuenta con el username %s no existe." % username)
- return connection.send_chat("Usted actualmente ya esta logeado como %s" % connection.username)
- def claim(connection):
- if connection.login:
- datos = open_data()
- posx = datos[connection.username]["coordx"]
- posy = datos[connection.username]["coordy"]
- if posx != False and posy != False:
- return connection.send_chat("Usted actualmente ya tiene un sector.")
- connection.claimed = True
- return connection.send_chat("Destruye un bloque en el lugar donde reclamaras tu sector.")
- return connection.send_chat("Usted necesita logearse antes de poder reclamar un sector.")
- def share(connection, username):
- if connection.login:
- if username == connection.username:
- return connection.send_chat("Ingresa un usuario valido.")
- datos = open_data()
- users = datos[connection.username]["shared"]
- for key, value in datos.items():
- if username == key:
- if username in users:
- return connection.send_chat("%s ya se encuentra en tu lista de usuarios que pueden construir y destruir en tu sector." % username)
- else:
- users.append(username)
- save_data(datos)
- edit_claims(connection, connection.username, username, "add")
- print(connection.protocol.claim_coords)
- return connection.send_chat("%s ahora podra construir y destruir en tu sector." % username)
- return connection.send_chat("El usuario %s no existe o no esta registrado." % username)
- return connection.send_chat("Usted necesita logearse antes de compartir su sector.")
- def unshare(connection, username):
- if connection.login:
- datos = open_data()
- users = datos[connection.username]["shared"]
- if len(users) <= 0:
- return connection.send_chat("Actualmente no hay usuarios en tu lista.")
- elif username == connection.username:
- return connection.send_chat("Ingresa un usuario valido.")
- for key, value in datos.items():
- if username == key:
- if username in users:
- users.remove(username)
- save_data(datos)
- edit_claims(connection, connection.username, username, "remove")
- print(connection.protocol.claim_coords)
- return connection.send_chat("%s ya no podra construir ni destruir en tu sector." % username)
- else:
- return connection.send_chat("%s actualmente no se encuentra en tu lista de usuarios que pueden destruir ni destruir en tu sector." % username)
- return connection.send_chat("%s no existe o no esta registrado." % username)
- return connection.send_chat("Usted necesita logearse antes de compartir su sector.")
- add(claim)
- add(buildlogin)
- add(buildregister)
- add(share)
- add(unshare)
- def apply_script(protocol,connection,config):
- class BuildConnection(connection):
- claimed = False
- login = False
- username = None
- def is_in_range(self, x, y):
- if len(self.protocol.claim_coords) > 0:
- for values in self.protocol.claim_coords:
- pos_x = values[1]
- pos_y = values[2]
- for xx in range(pos_x, pos_x + (RANGE_CLAIM * 2)):
- for yy in range(pos_y - (RANGE_CLAIM * 2), pos_y):
- for new_x in range(x-RANGE_CLAIM,x+RANGE_CLAIM):
- for new_y in range(y-RANGE_CLAIM,y+RANGE_CLAIM):
- if xx == new_x and yy == new_y:
- return True
- return False
- def on_block_build_attempt(self,x,y,z):
- if len(self.protocol.claim_coords) > 0:
- check = self.protocol.is_claimed(x, y, z)
- if check:
- if self.login and self.username != check[0] and self.username not in check[3] or not self.login:
- self.send_chat("Este lugar le pertenece al usuario %s" % check[0])
- return False
- return connection.on_block_build_attempt(self,x,y,z)
- def on_line_build_attempt(self, points):
- if len(self.protocol.claim_coords) > 0:
- for xyz in points:
- check = self.protocol.is_claimed(xyz[0], xyz[1], xyz[2])
- if check:
- if self.login and self.username != check[0] and self.username not in check[3] or not self.login:
- self.send_chat("Este lugar le pertenece al usuario %s" % check[0])
- return False
- return connection.on_line_build_attempt(self, points)
- def on_block_destroy(self,x,y,z,mode):
- print(self.protocol.claim_coords)
- if self.login and self.claimed:
- if len(self.protocol.claim_coords) > 0:
- if self.is_in_range(x, y):
- self.send_chat("Escoge otro lugar.")
- return False
- for new_x in range(x-RANGE_CLAIM,x+RANGE_CLAIM):
- for new_y in range(y-RANGE_CLAIM,y+RANGE_CLAIM):
- if new_x < 0 or new_x >= 512 or new_y < 0 or new_y >= 512 or z < 0 or z >= 62:
- return self.send_chat("Usted no puede reclamar un sector aca.")
- removeblock(self, (new_x, new_y, z))
- fastblock(self,(new_x, new_y, z),self.color)
- self.send_chat("Tu sector acaba de ser guardado!")
- self.claimed = False
- datos = open_data()
- datos[self.username]["coordx"] = x - RANGE_CLAIM
- datos[self.username]["coordy"] = y + RANGE_CLAIM
- save_data(datos)
- self.protocol.claim_coords.append([self.username, x - RANGE_CLAIM, y + RANGE_CLAIM, []])
- return False
- if len(self.protocol.claim_coords) > 0:
- check = self.protocol.is_claimed(x, y, z)
- if check:
- if self.login and self.username != check[0] and self.username not in check[3] or not self.login:
- self.send_chat("Este lugar le pertenece al usuario %s" % check[0])
- return False
- return connection.on_block_destroy(self,x,y,z,mode)
- class BuildProtocol(protocol):
- claim_coords = []
- def on_map_change(self, map):
- datos = open_data()
- if len(datos) > 0:
- for key, value in datos.items():
- if value["coordx"] != False and value["coordx"] != False:
- self.claim_coords.append( [key, value["coordx"], value["coordy"], value["shared"]])
- print(self.claim_coords)
- protocol.on_map_change(self, map)
- def is_claimed(self, x, y, z):
- for pr in self.claim_coords:
- for xx in range(pr[1], pr[1] + (RANGE_CLAIM * 2)):
- for yy in range(pr[2] - (RANGE_CLAIM * 2), pr[2]):
- if x == xx and y == yy:
- return pr
- return False
- return BuildProtocol, BuildConnection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement