summaryrefslogtreecommitdiff
path: root/snowbrawl.py
diff options
context:
space:
mode:
authorPanFritz <redstonenoobpan@gmail.com>2015-06-15 22:27:17 +0200
committerPanFritz <redstonenoobpan@gmail.com>2015-06-15 22:27:17 +0200
commit3a02a4cffcaac49e90b08fb259cb4aec93d6d51f (patch)
tree141e2731b2fe7cbacc2f909b8b2cbde13165cdd4 /snowbrawl.py
parent5f0d845ec0751a3f9c22abf842c8cb86ae55d87e (diff)
tweaks
Diffstat (limited to 'snowbrawl.py')
-rw-r--r--snowbrawl.py242
1 files changed, 154 insertions, 88 deletions
diff --git a/snowbrawl.py b/snowbrawl.py
index 20a73e1..c3cf500 100644
--- a/snowbrawl.py
+++ b/snowbrawl.py
@@ -3,8 +3,8 @@ from helpers import *
import time
import thread
import copy
-import org.bukkit.Material as Material
import org.bukkit.inventory.ItemStack as ItemStack
+import org.bukkit.Material as Material
from java.util.UUID import fromString as juuid
#file names
@@ -31,12 +31,12 @@ info_command = "info"
arenas = open_json_file(arena_file, [])
signs = open_json_file(sign_file, [])
matches = []
- ### TODO queue, more testing ###
+queue = []
+
def get_best(players):
highest = 2147483647
player_out = players[0]
- for i in range(len(players)):
- player = players[i]
+ for player in players:
if player["deaths"] < highest:
highest = player["deaths"]
player_out = player
@@ -44,8 +44,7 @@ def get_best(players):
def add_match(name):
- for i in range(len(arenas)):
- arena = arenas[i]
+ for arena in arenas:
if arena["name"] == name:
match = {
"end_time": (time.time() + int(arena["match_time"])),
@@ -63,8 +62,7 @@ def end_match(name):
for i in range(len(matches)):
match = matches[i]
if match["arena"] == name:
- for j in range(len(arenas)):
- arena = arenas[j]
+ for arena in arenas:
if arena["name"] == name:
players = match["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)
matches.pop(i)
update_queue(name)
- break
+ return
def join_match(sender, name):
- for i in range(len(matches)):
- match = matches[i]
+ for match in matches:
if match["arena"] == name:
if len(match["players"]) >= int(match["limit"]):
add_to_queue(sender, name)
@@ -126,11 +123,9 @@ def join_match(sender, name):
def start_match(name):
- for i in range(len(matches)):
- match = matches[i]
+ for match in matches:
if match["arena"] == name:
- for k in range(len(arenas)):
- arena = arenas[k]
+ for arena in arenas:
if arena["name"] == name:
players = match["players"]
for j in range(len(players)):
@@ -147,16 +142,33 @@ def start_match(name):
match["players"].pop(j)
j -= 1;
match["started"] = True
- break
+ return
-def add_to_queue(sender, name):
- msg(sender, "&aMatch is currently in progress, you will be automatically teleported once it is over")
+def add_to_queue(sender, name):
+ queue_player = {
+ "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):
- 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():
@@ -164,6 +176,13 @@ def save_snowbrawl():
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):
sign.setLine(0, "")
sign.setLine(1, name)
@@ -193,14 +212,20 @@ def delete_arena(sender, name):
def rename_arena(sender, name, newName):
- for i in range(len(arenas)):
- arena = arenas[i]
+ for arena in arenas:
if arena["name"] == name:
- for j in range(len(matches)):
- match = matches[j]
+ for match in matches:
if match["arena"] == name:
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
save_snowbrawl()
msg(sender, "&aArena renamed to&6 %s" % newName)
@@ -208,13 +233,12 @@ def rename_arena(sender, name, newName):
def respawn_arena(sender, name):
- for i in range(len(arenas)):
- arena = arenas[i]
+ for arena in arenas:
if arena["name"] == name:
loc = sender.getLocation()
- arena["respawn_pos_x"] = int(loc.x)
- arena["respawn_pos_y"] = int(loc.y)
- arena["respawn_pos_z"] = int(loc.z)
+ arena["respawn_pos_x"] = float(loc.x)
+ arena["respawn_pos_y"] = float(loc.y)
+ arena["respawn_pos_z"] = float(loc.z)
arena["respawn_yaw"] = int(loc.yaw)
arena["respawn_pitch"] = int(loc.pitch)
arena["respawn_world"] = loc.getWorld().name
@@ -225,13 +249,12 @@ def respawn_arena(sender, name):
def spawn_arena(sender, name):
- for i in range(len(arenas)):
- arena = arenas[i]
+ for arena in arenas:
if arena["name"] == name:
loc = sender.getLocation()
- arena["spawn_pos_x"] = int(loc.x)
- arena["spawn_pos_y"] = int(loc.y)
- arena["spawn_pos_z"] = int(loc.z)
+ arena["spawn_pos_x"] = float(loc.x)
+ arena["spawn_pos_y"] = float(loc.y)
+ arena["spawn_pos_z"] = float(loc.z)
arena["spawn_yaw"] = int(loc.yaw)
arena["spawn_pitch"] = int(loc.pitch)
arena["spawn_world"] = loc.getWorld().name
@@ -241,7 +264,23 @@ def spawn_arena(sender, name):
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):
+ loc = sender.getLocation()
arena = {
"name": name,
"player_limit": limit,
@@ -249,11 +288,23 @@ def create_arena(sender, name, limit, time):
"respawn_pos_x": 0,
"respawn_pos_y": 0,
"respawn_pos_z": 0,
+ "respawn_pitch": 0,
+ "respawn_yaw": 0,
+ "respawn_world": "null",
"spawn_pos_x": 0,
"spawn_pos_y": 0,
"spawn_pos_z": 0,
+ "spawn_pitch": 0,
+ "spawn_yaw": 0,
+ "spawn_world": "null",
"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)
save_snowbrawl()
@@ -262,33 +313,28 @@ def create_arena(sender, name, limit, time):
def print_help(sender):
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):
msg(sender, "&a/snowbrawl %s &eDisplay the list of arenas" % list_command)
if sender.hasPermission(info_perm):
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):
- 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 &eSet a snowbrawl arena sign" % set_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 &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):
- 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):
- 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
- exists = False
- for i in range(len(arenas)):
- arena = arenas[i]
+ for arena in arenas:
if name == arena["name"]:
- exists = True
- break
- if exists:
- return False
+ return False
return True
@@ -301,12 +347,12 @@ def onRespawn(event):
match = matches[i]
players = match["players"]
for j in range(len(players)):
- if uid(player) == players[j]:
+ if uid(player) == players[j]["uuid"]:
for k in range(len(arenas)):
arena = arenas[k]
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"]))
- break
+ return
@hook.event("entity.PlayerDeathEvent", "high")
@@ -323,7 +369,7 @@ def onDeath(event):
arena = arenas[k]
if arena["name"] == match["arena"]:
players[j]["deaths"] += 1
- break
+ return
@hook.event("entity.ProjectileHitEvent", "high")
@@ -334,22 +380,22 @@ def onHit(event):
return
entity = event.getEntity()
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):
if str(event.getAction()) != "RIGHT_CLICK_BLOCK":
return
block = event.getClickedBlock().getState()
-
- if event.getClickedBlock().getMaterial() == Material.SNOW_BLOCK:
+ if event.getClickedBlock().getType() == Material.SNOW_BLOCK:
inv = event.getPlayer().getInventory()
- for slot, stack in dict(inv.all(Material.SNOW_BALL)).iteritems():
- stack.setAmount(0)
- inv.setItemInHand(ItemStack(Material.SNOW_BALL, 4))
-
- elif isinstance(block, bukkit.block.Sign) and not event.isCancelled():
+ inv.remove(Material.SNOW_BALL)
+ inv.setItemInHand(ItemStack(Material.SNOW_BALL, 16))
+ event.getPlayer().updateInventory()
+ elif not isinstance(block, bukkit.block.Sign):
+ return
+ elif not event.isCancelled():
line = block.getLine(1)
for j in range(len(arenas)):
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 arena["spawn_set"] and arena["respawn_set"]:
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)
else:
msg(event.getPlayer(), "&a-&e You don't have permission to join snowbrawl matches")
- break
+ return
@hook.command("sb")
@@ -376,9 +434,9 @@ def on_snowbrawl_command_short(sender, command, label, args):
def on_snowbrawl_command(sender, command, label, args):
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)
- elif cmd == list_command: #print the list of arenas
+ elif cmd == list_command: # Print the list of arenas
if sender.hasPermission(list_perm):
if len(arenas) > 0:
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")
else:
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):
msg(sender, "&cOnly players can do this")
return True
if sender.hasPermission(modify_perm):
if len(args) > 1:
- mats = set()
- mats = None
- block = sender.getTargetBlock(mats, 3).getState()
- if isinstance(block, bukkit.block.Sign):
- set_arena_sign(sender, str(args[1]), block)
+ exists = False
+ for arena in arenas:
+ if str(args[1]) == arena["name"]:
+ exists = True
+ 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:
- msg(sender, "&cYou are not looking at a sign")
+ msg(sender, "&cArena&e %s &cdoes not exist" % str(args[1]))
else:
msg(sender, "&cArena to create is not specified")
msg(sender, "&e/snowbrawl %s <name>" % set_command)
else:
noperm(sender)
- elif cmd == del_command: #delete an arena
+ elif cmd == del_command: # Delete an arena
if sender.hasPermission(modify_perm):
if len(args) > 1:
exists = False
- for i in range(len(arenas)):
- arena = arenas[i]
+ for arena in arenas:
if str(args[1]) == arena["name"]:
exists = True
break
@@ -423,18 +488,17 @@ def on_snowbrawl_command(sender, command, label, args):
msg(sender, "&a/snowbrawl %s <name>" % del_command)
else:
noperm(sender)
- elif cmd == name_command: #rename an arena
+ elif cmd == name_command: # Rename an arena
if sender.hasPermission(modify_perm):
if len(args) > 1:
if len(args) > 2:
exists = False
- for i in range(len(arenas)):
- arena = arenas[i]
+ for arena in arenas:
if str(args[1]) == arena["name"]:
exists = True
break
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]))
else:
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)
else:
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):
msg(sender, "&cOnly players can do this")
return True
@@ -456,8 +520,7 @@ def on_snowbrawl_command(sender, command, label, args):
if len(args) > 1:
if len(args) > 2:
exists = False
- for i in range(len(arenas)):
- arena = arenas[i]
+ for arena in arenas:
if str(args[2]) == arena["name"]:
exists = True
break
@@ -466,9 +529,11 @@ def on_snowbrawl_command(sender, command, label, args):
spawn_arena(sender, str(args[2]))
elif str(args[1]) == "respawn":
respawn_arena(sender, str(args[2]))
+ elif str(args[1]) == "teleport":
+ tp_arena(sender, str(args[1]))
else:
msg(sender, "&cInvalid mode")
- msg(sender, "&aValid modes: &6spawn&a,&6 respawn")
+ msg(sender, "&aValid modes: &6spawn&a,&6 respawn&a,&6 teleport")
else:
msg(sender, "&cArena&e %s &cdoes not exist" % str(args[2]))
else:
@@ -479,7 +544,7 @@ def on_snowbrawl_command(sender, command, label, args):
msg(sender, "&a/snowbrawl %s re/spawn <name>" % pos_command)
else:
noperm(sender)
- elif cmd == create_command: #Create an arena
+ elif cmd == create_command: # Create an arena
if sender.hasPermission(create_perm):
if len(args) > 1:
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)
else:
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 len(args) > 1:
exists = False
@@ -521,13 +586,13 @@ def on_snowbrawl_command(sender, command, label, args):
if exists:
msg(sender, "&a- &e%s" % arenas[arenaId])
else:
- msg(sender, "&a- &eArena&6 %s &edoes not exist" % cmd)
+ msg(sender, "&cArena&e %s &cdoes not exist" % cmd)
else:
msg(sender, "&cArena name is not specified")
msg(sender, "&a/snowbrawl %s <name>" % info_command)
else:
noperm(sender)
- else: #arguments dont match, teleport to an arena
+ else: # Arguments dont match, teleport to an arena
if not is_player(sender):
msg(sender, "&cOnly players can do this")
return True
@@ -540,6 +605,7 @@ def on_snowbrawl_command(sender, command, label, args):
break
if exists:
msg(sender, "&a-&e Teleporting to arena&6 %s" % cmd)
+ teleport_to_arena(sender, cmd)
else:
msg(sender, "&a- &eArena&6 %s &edoes not exist" % cmd)
else:
@@ -562,7 +628,7 @@ def countdown_timer():
def stop_match_end_thread():
global isThreadRunning
- print("Stopping snowbrawl match end check thread")
+ print("Stopping snowbrawl match check thread")
isThreadRunning = False