This commit is contained in:
PanFritz
2015-06-15 22:27:17 +02:00
parent 5f0d845ec0
commit 3a02a4cffc
+154 -88
View File
@@ -3,8 +3,8 @@ from helpers import *
import time import time
import thread import thread
import copy import copy
import org.bukkit.Material as Material
import org.bukkit.inventory.ItemStack as ItemStack import org.bukkit.inventory.ItemStack as ItemStack
import org.bukkit.Material as Material
from java.util.UUID import fromString as juuid from java.util.UUID import fromString as juuid
#file names #file names
@@ -31,12 +31,12 @@ info_command = "info"
arenas = open_json_file(arena_file, []) arenas = open_json_file(arena_file, [])
signs = open_json_file(sign_file, []) signs = open_json_file(sign_file, [])
matches = [] matches = []
### TODO queue, more testing ### queue = []
def get_best(players): def get_best(players):
highest = 2147483647 highest = 2147483647
player_out = players[0] player_out = players[0]
for i in range(len(players)): for player in players:
player = players[i]
if player["deaths"] < highest: if player["deaths"] < highest:
highest = player["deaths"] highest = player["deaths"]
player_out = player player_out = player
@@ -44,8 +44,7 @@ def get_best(players):
def add_match(name): def add_match(name):
for i in range(len(arenas)): for arena in arenas:
arena = arenas[i]
if arena["name"] == name: if arena["name"] == name:
match = { match = {
"end_time": (time.time() + int(arena["match_time"])), "end_time": (time.time() + int(arena["match_time"])),
@@ -63,8 +62,7 @@ def end_match(name):
for i in range(len(matches)): for i in range(len(matches)):
match = matches[i] match = matches[i]
if match["arena"] == name: if match["arena"] == name:
for j in range(len(arenas)): for arena in arenas:
arena = arenas[j]
if arena["name"] == name: if arena["name"] == name:
players = match["players"] players = match["players"]
players_copy = copy.deepcopy(players) players_copy = copy.deepcopy(players)
@@ -102,12 +100,11 @@ def end_match(name):
safetp(player, server.getWorld(arena["spawn_world"]), pos.x, pos.y, pos.z, pos.yaw, pos.pitch) safetp(player, server.getWorld(arena["spawn_world"]), pos.x, pos.y, pos.z, pos.yaw, pos.pitch)
matches.pop(i) matches.pop(i)
update_queue(name) update_queue(name)
break return
def join_match(sender, name): def join_match(sender, name):
for i in range(len(matches)): for match in matches:
match = matches[i]
if match["arena"] == name: if match["arena"] == name:
if len(match["players"]) >= int(match["limit"]): if len(match["players"]) >= int(match["limit"]):
add_to_queue(sender, name) add_to_queue(sender, name)
@@ -126,11 +123,9 @@ def join_match(sender, name):
def start_match(name): def start_match(name):
for i in range(len(matches)): for match in matches:
match = matches[i]
if match["arena"] == name: if match["arena"] == name:
for k in range(len(arenas)): for arena in arenas:
arena = arenas[k]
if arena["name"] == name: if arena["name"] == name:
players = match["players"] players = match["players"]
for j in range(len(players)): for j in range(len(players)):
@@ -147,16 +142,33 @@ def start_match(name):
match["players"].pop(j) match["players"].pop(j)
j -= 1; j -= 1;
match["started"] = True match["started"] = True
break return
def add_to_queue(sender, name): def add_to_queue(sender, name):
queue_player = {
msg(sender, "&aMatch is currently in progress, you will be automatically teleported once it is over") "sender": sender,
"name": name
}
queue.append(queue_player)
msg(sender, "&a-&eMatch is currently in progress, you will be automatically teleported once it is over")
def update_queue(name): def update_queue(name):
print("UPDATING QUEUE") queue_copy = []
for queue_thing in queue:
queue_player_copy = {
"sender": queue_thing["sender"],
"name": queue_thing["name"]
}
queue_copy.append(queue_player_copy)
queue[:] = []
for queue_player in queue_copy:
if queue_player["name"] == name:
join_match(queue_player["sender"], name)
else:
queue.append(queue_player)
def save_snowbrawl(): def save_snowbrawl():
@@ -164,6 +176,13 @@ def save_snowbrawl():
save_json_file(sign_file, signs) save_json_file(sign_file, signs)
def teleport_to_arena(sender, name):
for arena in arenas:
if arena["name"] == name:
safetp(sender, server.getWorld(arena["tp_world"]), arena["tp_pos_x"], arena["tp_pos_y"], arena["tp_pos_z"], arena["tp_yaw"], arena["tp_pitch"])
break
def set_arena_sign(sender, name, sign): def set_arena_sign(sender, name, sign):
sign.setLine(0, "") sign.setLine(0, "")
sign.setLine(1, name) sign.setLine(1, name)
@@ -193,14 +212,20 @@ def delete_arena(sender, name):
def rename_arena(sender, name, newName): def rename_arena(sender, name, newName):
for i in range(len(arenas)): for arena in arenas:
arena = arenas[i]
if arena["name"] == name: if arena["name"] == name:
for j in range(len(matches)): for match in matches:
match = matches[j]
if match["arena"] == name: if match["arena"] == name:
match["arena"] = newName match["arena"] = newName
msg(sender, "&aRunning match appended to arena&6 %s" % newName) msg(sender, "&a-&eRunning match appended to arena&6 %s" % newName)
for queue_player in queue:
if queue_player["name"] == name:
queue_player["name"] = newName
msg(sender, "&a-&e Player&6 %s &ein queue relocated into&6 %s &equeue" % (queue_player["sender"].getName(), newName))
for sign in signs:
if sign["arena"] == name:
sign["arena"] = newName
msg(sender, "&a-&e Sign at&6 %s&e,&6 %s&e,&6 %s &erelinked to arena&6 %s" % (sign["x"], sign["y"], sign["z"], newName))
arena["name"] = newName arena["name"] = newName
save_snowbrawl() save_snowbrawl()
msg(sender, "&aArena renamed to&6 %s" % newName) msg(sender, "&aArena renamed to&6 %s" % newName)
@@ -208,13 +233,12 @@ def rename_arena(sender, name, newName):
def respawn_arena(sender, name): def respawn_arena(sender, name):
for i in range(len(arenas)): for arena in arenas:
arena = arenas[i]
if arena["name"] == name: if arena["name"] == name:
loc = sender.getLocation() loc = sender.getLocation()
arena["respawn_pos_x"] = int(loc.x) arena["respawn_pos_x"] = float(loc.x)
arena["respawn_pos_y"] = int(loc.y) arena["respawn_pos_y"] = float(loc.y)
arena["respawn_pos_z"] = int(loc.z) arena["respawn_pos_z"] = float(loc.z)
arena["respawn_yaw"] = int(loc.yaw) arena["respawn_yaw"] = int(loc.yaw)
arena["respawn_pitch"] = int(loc.pitch) arena["respawn_pitch"] = int(loc.pitch)
arena["respawn_world"] = loc.getWorld().name arena["respawn_world"] = loc.getWorld().name
@@ -225,13 +249,12 @@ def respawn_arena(sender, name):
def spawn_arena(sender, name): def spawn_arena(sender, name):
for i in range(len(arenas)): for arena in arenas:
arena = arenas[i]
if arena["name"] == name: if arena["name"] == name:
loc = sender.getLocation() loc = sender.getLocation()
arena["spawn_pos_x"] = int(loc.x) arena["spawn_pos_x"] = float(loc.x)
arena["spawn_pos_y"] = int(loc.y) arena["spawn_pos_y"] = float(loc.y)
arena["spawn_pos_z"] = int(loc.z) arena["spawn_pos_z"] = float(loc.z)
arena["spawn_yaw"] = int(loc.yaw) arena["spawn_yaw"] = int(loc.yaw)
arena["spawn_pitch"] = int(loc.pitch) arena["spawn_pitch"] = int(loc.pitch)
arena["spawn_world"] = loc.getWorld().name arena["spawn_world"] = loc.getWorld().name
@@ -241,7 +264,23 @@ def spawn_arena(sender, name):
break break
def tp_arena(sender, name):
for arena in arenas:
if arena["name"] == name:
loc = sender.getLocation()
arena["tp_pos_x"] = float(loc.x)
arena["tp_pos_y"] = float(loc.y)
arena["tp_pos_z"] = float(loc.z)
arena["tp_yaw"] = int(loc.yaw)
arena["tp_pitch"] = int(loc.pitch)
arena["tp_world"] = loc.getWorld().name
save_snowbrawl()
msg(sender, "&aArena teleport position set")
break
def create_arena(sender, name, limit, time): def create_arena(sender, name, limit, time):
loc = sender.getLocation()
arena = { arena = {
"name": name, "name": name,
"player_limit": limit, "player_limit": limit,
@@ -249,11 +288,23 @@ def create_arena(sender, name, limit, time):
"respawn_pos_x": 0, "respawn_pos_x": 0,
"respawn_pos_y": 0, "respawn_pos_y": 0,
"respawn_pos_z": 0, "respawn_pos_z": 0,
"respawn_pitch": 0,
"respawn_yaw": 0,
"respawn_world": "null",
"spawn_pos_x": 0, "spawn_pos_x": 0,
"spawn_pos_y": 0, "spawn_pos_y": 0,
"spawn_pos_z": 0, "spawn_pos_z": 0,
"spawn_pitch": 0,
"spawn_yaw": 0,
"spawn_world": "null",
"spawn_set": False, "spawn_set": False,
"respawn_set": False "respawn_set": False,
"tp_pos_x": float(loc.x),
"tp_pos_y": float(loc.y),
"tp_pos_z": float(loc.z),
"tp_yaw": 0,
"tp_pitch": 0,
"tp_world": loc.getWorld().name
} }
arenas.append(arena) arenas.append(arena)
save_snowbrawl() save_snowbrawl()
@@ -262,33 +313,28 @@ def create_arena(sender, name, limit, time):
def print_help(sender): def print_help(sender):
plugin_header(sender, "Snowbrawl") plugin_header(sender, "Snowbrawl")
msg(sender, "&aAlias: &6/sb") msg(sender, "&a- &eAlias: &6/sb")
if sender.hasPermission(teleport_perm):
msg(sender, "&a/snowbrawl <name> &eTeleport to a certain arena")
if sender.hasPermission(list_perm): if sender.hasPermission(list_perm):
msg(sender, "&a/snowbrawl %s &eDisplay the list of arenas" % list_command) msg(sender, "&a/snowbrawl %s &eDisplay the list of arenas" % list_command)
if sender.hasPermission(info_perm): if sender.hasPermission(info_perm):
msg(sender, "&a/snowbrawl %s &eDisplay info about an arena" % info_command) msg(sender, "&a/snowbrawl %s &eDisplay info about an arena" % info_command)
if sender.hasPermission(teleport_perm):
msg(sender, "&a/snowbrawl <name> &eTeleport to a certain arena")
if sender.hasPermission(modify_perm): if sender.hasPermission(modify_perm):
msg(sender, "&a/snowbrawl %s &eSet a snowbrawl arena sign" % set_command) msg(sender, "&a/snowbrawl %s &eSet a snowbrawl arena sign" % set_command)
msg(sender, "&a/snowbrawl %s &eDelete a snowbrawl arena sign" % del_command) msg(sender, "&a/snowbrawl %s &eDelete a snowbrawl arena sign" % del_command)
msg(sender, "&a/snowbrawl %s &eChange an arena sign name" % name_command) msg(sender, "&a/snowbrawl %s &eChange an arena sign name" % name_command)
msg(sender, "&a/snowbrawl %s &eSet the tp position for the arena" % pos_command) msg(sender, "&a/snowbrawl %s &eSet the tp position for the arena" % pos_command)
if sender.hasPermission(create_perm): if sender.hasPermission(create_perm):
msg(sender, "&a/snowbrawl %s &eCreate an arena" % create_command) msg(sender, "&a/snowbrawl %s &eCreate an arena" % create_command)
def check_valid_name(name): def check_valid_name(name):
if name in [list_command, del_command, set_command, name_command, pos_command]: if name in [list_command, del_command, set_command, name_command, pos_command, info_command]:
return False return False
exists = False for arena in arenas:
for i in range(len(arenas)):
arena = arenas[i]
if name == arena["name"]: if name == arena["name"]:
exists = True return False
break
if exists:
return False
return True return True
@@ -301,12 +347,12 @@ def onRespawn(event):
match = matches[i] match = matches[i]
players = match["players"] players = match["players"]
for j in range(len(players)): for j in range(len(players)):
if uid(player) == players[j]: if uid(player) == players[j]["uuid"]:
for k in range(len(arenas)): for k in range(len(arenas)):
arena = arenas[k] arena = arenas[k]
if arena["name"] == match["arena"]: if arena["name"] == match["arena"]:
event.setRespawnLocation(Location(server.getWorld(arena["respawn_world"]), arena["respawn_pos_x"], arena["respawn_pos_y"], arena["respawn_pos_z"], arena["respawn_yaw"], arena["respawn_pitch"])) event.setRespawnLocation(Location(server.getWorld(arena["respawn_world"]), arena["respawn_pos_x"], arena["respawn_pos_y"], arena["respawn_pos_z"], arena["respawn_yaw"], arena["respawn_pitch"]))
break return
@hook.event("entity.PlayerDeathEvent", "high") @hook.event("entity.PlayerDeathEvent", "high")
@@ -323,7 +369,7 @@ def onDeath(event):
arena = arenas[k] arena = arenas[k]
if arena["name"] == match["arena"]: if arena["name"] == match["arena"]:
players[j]["deaths"] += 1 players[j]["deaths"] += 1
break return
@hook.event("entity.ProjectileHitEvent", "high") @hook.event("entity.ProjectileHitEvent", "high")
@@ -334,22 +380,22 @@ def onHit(event):
return return
entity = event.getEntity() entity = event.getEntity()
location = entity.getLocation() location = entity.getLocation()
entity.getWorld().createExplosion(location.getX(), location.getY(), location.getZ(), float(5), False, False) entity.getWorld().createExplosion(location.getX(), location.getY(), location.getZ(), float(0.5), False, False)
@hook.event("player.PlayerInteractEvent", "high") # add snowblock click to refill snowballs @hook.event("player.PlayerInteractEvent", "high")
def onClick(event): def onClick(event):
if str(event.getAction()) != "RIGHT_CLICK_BLOCK": if str(event.getAction()) != "RIGHT_CLICK_BLOCK":
return return
block = event.getClickedBlock().getState() block = event.getClickedBlock().getState()
if event.getClickedBlock().getType() == Material.SNOW_BLOCK:
if event.getClickedBlock().getMaterial() == Material.SNOW_BLOCK:
inv = event.getPlayer().getInventory() inv = event.getPlayer().getInventory()
for slot, stack in dict(inv.all(Material.SNOW_BALL)).iteritems(): inv.remove(Material.SNOW_BALL)
stack.setAmount(0) inv.setItemInHand(ItemStack(Material.SNOW_BALL, 16))
inv.setItemInHand(ItemStack(Material.SNOW_BALL, 4)) event.getPlayer().updateInventory()
elif not isinstance(block, bukkit.block.Sign):
elif isinstance(block, bukkit.block.Sign) and not event.isCancelled(): return
elif not event.isCancelled():
line = block.getLine(1) line = block.getLine(1)
for j in range(len(arenas)): for j in range(len(arenas)):
arena = arenas[j] arena = arenas[j]
@@ -361,10 +407,22 @@ def onClick(event):
if sign["x"] == pos.x and sign["y"] == pos.y and sign["z"] == pos.z: if sign["x"] == pos.x and sign["y"] == pos.y and sign["z"] == pos.z:
if arena["spawn_set"] and arena["respawn_set"]: if arena["spawn_set"] and arena["respawn_set"]:
if event.getPlayer().hasPermission(join_perm): if event.getPlayer().hasPermission(join_perm):
for queue_player in queue:
if queue_player["sender"].getName() == event.getPlayer().getName():
msg(event.getPlayer(), "&a-&e You are already in a queue for a match")
return
for match in matches:
for match_player in match["players"]:
if match_player["uuid"] == uid(event.getPlayer()):
if line == match["arena"]:
msg(event.getPlayer(), "&a-&e You are already in this match")
else:
msg(event.getPlayer(), "&a-&e You are already in a different match")
return
join_match(event.getPlayer(), line) join_match(event.getPlayer(), line)
else: else:
msg(event.getPlayer(), "&a-&e You don't have permission to join snowbrawl matches") msg(event.getPlayer(), "&a-&e You don't have permission to join snowbrawl matches")
break return
@hook.command("sb") @hook.command("sb")
@@ -376,9 +434,9 @@ def on_snowbrawl_command_short(sender, command, label, args):
def on_snowbrawl_command(sender, command, label, args): def on_snowbrawl_command(sender, command, label, args):
cmd = args[0] if len(args) > 0 else None cmd = args[0] if len(args) > 0 else None
if cmd == None: #No arguments, print help if cmd == None: # No arguments, print help
print_help(sender) print_help(sender)
elif cmd == list_command: #print the list of arenas elif cmd == list_command: # Print the list of arenas
if sender.hasPermission(list_perm): if sender.hasPermission(list_perm):
if len(arenas) > 0: if len(arenas) > 0:
for i in range(len(arenas)): for i in range(len(arenas)):
@@ -387,30 +445,37 @@ def on_snowbrawl_command(sender, command, label, args):
msg(sender, "&a- &eNo snowbrawl arenas exist") msg(sender, "&a- &eNo snowbrawl arenas exist")
else: else:
noperm(sender) noperm(sender)
elif cmd == set_command: #set an arena tp sign elif cmd == set_command: # Set an arena tp sign
if not is_player(sender): if not is_player(sender):
msg(sender, "&cOnly players can do this") msg(sender, "&cOnly players can do this")
return True return True
if sender.hasPermission(modify_perm): if sender.hasPermission(modify_perm):
if len(args) > 1: if len(args) > 1:
mats = set() exists = False
mats = None for arena in arenas:
block = sender.getTargetBlock(mats, 3).getState() if str(args[1]) == arena["name"]:
if isinstance(block, bukkit.block.Sign): exists = True
set_arena_sign(sender, str(args[1]), block) break
if exists:
mats = set()
mats = None
block = sender.getTargetBlock(mats, 3).getState()
if isinstance(block, bukkit.block.Sign):
set_arena_sign(sender, str(args[1]), block)
else:
msg(sender, "&cYou are not looking at a sign")
else: else:
msg(sender, "&cYou are not looking at a sign") msg(sender, "&cArena&e %s &cdoes not exist" % str(args[1]))
else: else:
msg(sender, "&cArena to create is not specified") msg(sender, "&cArena to create is not specified")
msg(sender, "&e/snowbrawl %s <name>" % set_command) msg(sender, "&e/snowbrawl %s <name>" % set_command)
else: else:
noperm(sender) noperm(sender)
elif cmd == del_command: #delete an arena elif cmd == del_command: # Delete an arena
if sender.hasPermission(modify_perm): if sender.hasPermission(modify_perm):
if len(args) > 1: if len(args) > 1:
exists = False exists = False
for i in range(len(arenas)): for arena in arenas:
arena = arenas[i]
if str(args[1]) == arena["name"]: if str(args[1]) == arena["name"]:
exists = True exists = True
break break
@@ -423,18 +488,17 @@ def on_snowbrawl_command(sender, command, label, args):
msg(sender, "&a/snowbrawl %s <name>" % del_command) msg(sender, "&a/snowbrawl %s <name>" % del_command)
else: else:
noperm(sender) noperm(sender)
elif cmd == name_command: #rename an arena elif cmd == name_command: # Rename an arena
if sender.hasPermission(modify_perm): if sender.hasPermission(modify_perm):
if len(args) > 1: if len(args) > 1:
if len(args) > 2: if len(args) > 2:
exists = False exists = False
for i in range(len(arenas)): for arena in arenas:
arena = arenas[i]
if str(args[1]) == arena["name"]: if str(args[1]) == arena["name"]:
exists = True exists = True
break break
if exists: if exists:
if check_valid_name(str(args[1])): if check_valid_name(str(args[2])):
rename_arena(sender, str(args[1]), str(args[2])) rename_arena(sender, str(args[1]), str(args[2]))
else: else:
msg(sender, "&cArena name is invalid") msg(sender, "&cArena name is invalid")
@@ -448,7 +512,7 @@ def on_snowbrawl_command(sender, command, label, args):
msg(sender, "&a/snowbrawl %s <name> <newName>" % name_command) msg(sender, "&a/snowbrawl %s <name> <newName>" % name_command)
else: else:
noperm(sender) noperm(sender)
elif cmd == pos_command: #set a tp position for an arena elif cmd == pos_command: # Set a tp position for an arena
if not is_player(sender): if not is_player(sender):
msg(sender, "&cOnly players can do this") msg(sender, "&cOnly players can do this")
return True return True
@@ -456,8 +520,7 @@ def on_snowbrawl_command(sender, command, label, args):
if len(args) > 1: if len(args) > 1:
if len(args) > 2: if len(args) > 2:
exists = False exists = False
for i in range(len(arenas)): for arena in arenas:
arena = arenas[i]
if str(args[2]) == arena["name"]: if str(args[2]) == arena["name"]:
exists = True exists = True
break break
@@ -466,9 +529,11 @@ def on_snowbrawl_command(sender, command, label, args):
spawn_arena(sender, str(args[2])) spawn_arena(sender, str(args[2]))
elif str(args[1]) == "respawn": elif str(args[1]) == "respawn":
respawn_arena(sender, str(args[2])) respawn_arena(sender, str(args[2]))
elif str(args[1]) == "teleport":
tp_arena(sender, str(args[1]))
else: else:
msg(sender, "&cInvalid mode") msg(sender, "&cInvalid mode")
msg(sender, "&aValid modes: &6spawn&a,&6 respawn") msg(sender, "&aValid modes: &6spawn&a,&6 respawn&a,&6 teleport")
else: else:
msg(sender, "&cArena&e %s &cdoes not exist" % str(args[2])) msg(sender, "&cArena&e %s &cdoes not exist" % str(args[2]))
else: else:
@@ -479,7 +544,7 @@ def on_snowbrawl_command(sender, command, label, args):
msg(sender, "&a/snowbrawl %s re/spawn <name>" % pos_command) msg(sender, "&a/snowbrawl %s re/spawn <name>" % pos_command)
else: else:
noperm(sender) noperm(sender)
elif cmd == create_command: #Create an arena elif cmd == create_command: # Create an arena
if sender.hasPermission(create_perm): if sender.hasPermission(create_perm):
if len(args) > 1: if len(args) > 1:
if len(args) > 2: if len(args) > 2:
@@ -507,7 +572,7 @@ def on_snowbrawl_command(sender, command, label, args):
msg(sender, "&a/snowbrawl %s <name> <playerLimit> <matchTime>" % create_command) msg(sender, "&a/snowbrawl %s <name> <playerLimit> <matchTime>" % create_command)
else: else:
noperm(sender) noperm(sender)
elif cmd == info_command: #print info about an arena elif cmd == info_command: # Print info about an arena
if sender.hasPermission(info_perm): if sender.hasPermission(info_perm):
if len(args) > 1: if len(args) > 1:
exists = False exists = False
@@ -521,13 +586,13 @@ def on_snowbrawl_command(sender, command, label, args):
if exists: if exists:
msg(sender, "&a- &e%s" % arenas[arenaId]) msg(sender, "&a- &e%s" % arenas[arenaId])
else: else:
msg(sender, "&a- &eArena&6 %s &edoes not exist" % cmd) msg(sender, "&cArena&e %s &cdoes not exist" % cmd)
else: else:
msg(sender, "&cArena name is not specified") msg(sender, "&cArena name is not specified")
msg(sender, "&a/snowbrawl %s <name>" % info_command) msg(sender, "&a/snowbrawl %s <name>" % info_command)
else: else:
noperm(sender) noperm(sender)
else: #arguments dont match, teleport to an arena else: # Arguments dont match, teleport to an arena
if not is_player(sender): if not is_player(sender):
msg(sender, "&cOnly players can do this") msg(sender, "&cOnly players can do this")
return True return True
@@ -540,6 +605,7 @@ def on_snowbrawl_command(sender, command, label, args):
break break
if exists: if exists:
msg(sender, "&a-&e Teleporting to arena&6 %s" % cmd) msg(sender, "&a-&e Teleporting to arena&6 %s" % cmd)
teleport_to_arena(sender, cmd)
else: else:
msg(sender, "&a- &eArena&6 %s &edoes not exist" % cmd) msg(sender, "&a- &eArena&6 %s &edoes not exist" % cmd)
else: else:
@@ -562,7 +628,7 @@ def countdown_timer():
def stop_match_end_thread(): def stop_match_end_thread():
global isThreadRunning global isThreadRunning
print("Stopping snowbrawl match end check thread") print("Stopping snowbrawl match check thread")
isThreadRunning = False isThreadRunning = False