Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added sync "checker" task to check that the permissions of players ar…

…e correctly set every 10 ticks.

Shouldn't be too much of a performance hit as we're just performing an .equals(String)

@TnT wanna test this one for me?
  • Loading branch information...
commit 54fec0d130bf0d16a630a9e2e6162b2d189717bb 1 parent c745c99
Ben authored
3  src/de/bananaco/bpermissions/imp/Permissions.java
@@ -33,6 +33,9 @@
33 33
34 34 @Override
35 35 public void onDisable() {
  36 + // Cancel tasks
  37 + getServer().getScheduler().cancelTasks(this);
  38 +
36 39 super.onDisable();
37 40 if(wm != null) {
38 41 for(World world : wm.getAllWorlds())
11 src/de/bananaco/bpermissions/imp/SuperPermissionHandler.java
@@ -29,6 +29,8 @@
29 29 private WorldManager wm = WorldManager.getInstance();
30 30 private Map<Player, PermissionAttachment> attachments = new HashMap<Player, PermissionAttachment>();
31 31 private Permissions plugin;
  32 +
  33 + private WorldChecker checker;
32 34
33 35 private static Field permissions;
34 36
@@ -63,6 +65,8 @@ public static void setPermissions(PermissionAttachment att, Map<String, Boolean>
63 65 att.getPermissible().recalculatePermissions();
64 66 }
65 67
  68 + // Main constructor
  69 +
66 70 protected SuperPermissionHandler(Permissions plugin) {
67 71 this.plugin = plugin;
68 72 // This next bit is simply to make bPermissions.* work with superperms, since I now have my bulk adding, I will concede to this
@@ -70,6 +74,10 @@ protected SuperPermissionHandler(Permissions plugin) {
70 74 children.put("bPermissions.admin", true);
71 75 Permission permission = new Permission("bPermissions.*", PermissionDefault.OP, children);
72 76 plugin.getServer().getPluginManager().addPermission(permission);
  77 +
  78 + checker = new WorldChecker(plugin.getServer(), this);
  79 +
  80 + plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, checker, 10, 10);
73 81 }
74 82
75 83 /**
@@ -127,6 +135,9 @@ public void setupPlayer(Player player, World world) {
127 135 // Then whack it onto the player
128 136 // TODO wait for the bukkit team to get their finger out, we'll use our reflection here!
129 137 try {
  138 + // The world
  139 + perms.put("world."+player.getWorld().getName(), true);
  140 + Debugger.log("world."+world.getName()+" set for "+player.getName());
130 141 setPermissions(att, perms);
131 142 } catch (IllegalArgumentException e) {
132 143 e.printStackTrace();
65 src/de/bananaco/bpermissions/imp/WorldChecker.java
... ... @@ -0,0 +1,65 @@
  1 +package de.bananaco.bpermissions.imp;
  2 +
  3 +import java.util.HashMap;
  4 +import java.util.Map;
  5 +
  6 +import org.bukkit.Server;
  7 +import org.bukkit.entity.Player;
  8 +
  9 +public class WorldChecker implements Runnable {
  10 +
  11 + private final Server server;
  12 + private final SuperPermissionHandler handler;
  13 +
  14 + private Map<Integer, String> worlds = new HashMap<Integer, String>();
  15 +
  16 + protected WorldChecker(Server server, SuperPermissionHandler handler) {
  17 + this.server = server;
  18 + this.handler = handler;
  19 + }
  20 +
  21 + private void update() {
  22 + Player[] players = server.getOnlinePlayers().clone();
  23 + for(Player player : players) {
  24 + if(needsUpdating(player)) {
  25 + updateEntry(player);
  26 + handler.setupPlayer(player.getName());
  27 + Debugger.log(player.getName()+" updated via the WorldChecker sync task");
  28 + }
  29 + }
  30 + }
  31 +
  32 + private boolean hasEntry(Player player) {
  33 + return worlds.containsKey(player.hashCode());
  34 + }
  35 +
  36 + private boolean needsUpdating(Player player) {
  37 + if(!player.isPermissionSet("world."+player.getWorld().getName())) {
  38 + Debugger.log("Wrong world set!");
  39 + return true;
  40 + }
  41 + if(!hasEntry(player)) {
  42 + Debugger.log("First entry created!");
  43 + return true;
  44 + }
  45 + if(getEntry(player).equals(player.getWorld().getName())) {
  46 + return false;
  47 + }
  48 + Debugger.log("Unknown error!");
  49 + return true;
  50 + }
  51 +
  52 + private String getEntry(Player player) {
  53 + return worlds.get(player.hashCode());
  54 + }
  55 +
  56 + private void updateEntry(Player player) {
  57 + worlds.put(player.hashCode(), player.getWorld().getName());
  58 + }
  59 +
  60 + @Override
  61 + public void run() {
  62 + update();
  63 + }
  64 +
  65 +}

0 comments on commit 54fec0d

Please sign in to comment.
Something went wrong with that request. Please try again.