Skip to content

Commit

Permalink
allow adding groups on servers and worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
weaondara committed Mar 22, 2020
1 parent 561cc4a commit baff13a
Show file tree
Hide file tree
Showing 13 changed files with 1,396 additions and 349 deletions.
5 changes: 5 additions & 0 deletions src/main/java/net/alpenblock/bungeeperms/BPPermission.java
Expand Up @@ -47,4 +47,9 @@ public int compareTo(BPPermission o)
ret = timedDuration == null && o.timedDuration == null ? 0 : (timedDuration == null ? -1 : (o.timedDuration == null ? 1 :Integer.compare(timedDuration, o.timedDuration)));
return ret;
}

public BPPermission clone()
{
return new BPPermission(permission, origin, isGroup, server, world, timedStart, timedDuration);
}
}
227 changes: 191 additions & 36 deletions src/main/java/net/alpenblock/bungeeperms/CommandHandler.java

Large diffs are not rendered by default.

233 changes: 193 additions & 40 deletions src/main/java/net/alpenblock/bungeeperms/Group.java
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -82,7 +83,7 @@ public Server getServer(String name)
Server s = servers.get(name);
if (s == null)
{
s = new Server(name, new ArrayList(), new ArrayList(), new HashMap(), null, null, null);
s = new Server(name, new ArrayList(), new ArrayList(), new ArrayList(), new ArrayList(), new HashMap(), null, null, null);
servers.put(name, s);
}

Expand Down Expand Up @@ -146,6 +147,34 @@ public List<TimedValue<String>> getTimedInheritancesString()
return timedInheritances;
}

@Override
@Deprecated
public List<String> getGroupsString()
{
return getInheritancesString();
}

@Override
@Deprecated
public List<TimedValue<String>> getTimedGroupsString()
{
return getTimedInheritancesString();
}

@Override
@Deprecated
public void setGroups(List<String> groups)
{
setInheritances(groups);
}

@Override
@Deprecated
public void setTimedGroups(List<TimedValue<String>> groups)
{
setTimedInheritances(groups);
}

@Deprecated
public boolean has(String perm)
{
Expand Down Expand Up @@ -228,6 +257,16 @@ public int compare(TimedValue<Group> o1, TimedValue<Group> o2)
for (TimedValue<Group> tg : tinherit)
{
List<BPPermission> gperms = tg.getValue().getEffectivePerms(server, world);
for (int i = 0; i < gperms.size(); i++)
{
BPPermission p = gperms.get(i).clone();
if (p.getTimedStart() == null || tg.getStart().getTime() + tg.getDuration() > p.getTimedStart().getTime() + p.getTimedDuration())
{
p.setTimedStart(tg.getStart());
p.setTimedDuration(tg.getDuration());
}
gperms.set(i, p);
}
ret.addAll(gperms);
}

Expand All @@ -239,12 +278,96 @@ public int compare(TimedValue<Group> o1, TimedValue<Group> o2)
Server srv = servers.get(server);
if (srv != null)
{
//inheritances
inherit = new ArrayList(srv.getGroups());
inherit.sort(new Comparator<Group>()
{
@Override
public int compare(Group o1, Group o2)
{
return -Integer.compare(o1.getWeight(), o2.getWeight());
}
});
for (Group g : inherit)
{
List<BPPermission> gperms = g.getEffectivePerms(server, world);
ret.addAll(gperms);
}

//timed inheritances
tinherit = new ArrayList(srv.getTimedGroups());
tinherit.sort(new Comparator<TimedValue<Group>>()
{
@Override
public int compare(TimedValue<Group> o1, TimedValue<Group> o2)
{
return -Integer.compare(o1.getValue().getWeight(), o2.getValue().getWeight());
}
});
for (TimedValue<Group> tg : tinherit)
{
List<BPPermission> gperms = tg.getValue().getEffectivePerms(server, world);
for (int i = 0; i < gperms.size(); i++)
{
BPPermission p = gperms.get(i).clone();
if (p.getTimedStart() == null || tg.getStart().getTime() + tg.getDuration() > p.getTimedStart().getTime() + p.getTimedDuration())
{
p.setTimedStart(tg.getStart());
p.setTimedDuration(tg.getDuration());
}
gperms.set(i, p);
}
ret.addAll(gperms);
}

ret.addAll(Statics.makeBPPerms(srv.getPerms(), server, null, this));
ret.addAll(Statics.makeBPPermsTimed(srv.getTimedPerms(), server, null, this));

World w = srv.getWorld(world);
if (w != null)
{
//inheritances
inherit = new ArrayList(w.getGroups());
inherit.sort(new Comparator<Group>()
{
@Override
public int compare(Group o1, Group o2)
{
return -Integer.compare(o1.getWeight(), o2.getWeight());
}
});
for (Group g : inherit)
{
List<BPPermission> gperms = g.getEffectivePerms(server, world);
ret.addAll(gperms);
}

//timed inheritances
tinherit = new ArrayList(w.getTimedGroups());
tinherit.sort(new Comparator<TimedValue<Group>>()
{
@Override
public int compare(TimedValue<Group> o1, TimedValue<Group> o2)
{
return -Integer.compare(o1.getValue().getWeight(), o2.getValue().getWeight());
}
});
for (TimedValue<Group> tg : tinherit)
{
List<BPPermission> gperms = tg.getValue().getEffectivePerms(server, world);
for (int i = 0; i < gperms.size(); i++)
{
BPPermission p = gperms.get(i).clone();
if (p.getTimedStart() == null || tg.getStart().getTime() + tg.getDuration() > p.getTimedStart().getTime() + p.getTimedDuration())
{
p.setTimedStart(tg.getStart());
p.setTimedDuration(tg.getDuration());
}
gperms.set(i, p);
}
ret.addAll(gperms);
}

ret.addAll(Statics.makeBPPerms(w.getPerms(), server, world, this));
ret.addAll(Statics.makeBPPermsTimed(w.getTimedPerms(), server, world, this));
}
Expand Down Expand Up @@ -299,13 +422,28 @@ public int getPermissionsCount(String server, String world)
{
int count = getOwnPermissionsCount(server, world);

for (String group : inheritances)
{
Group g = BungeePerms.getInstance().getPermissionsManager().getGroup(group);
if (g == null)
continue;
for (Group g : getInheritances())
count += g.getPermissionsCount(server, world);
}
for (TimedValue<Group> g : getTimedInheritances())
count += g.getValue().getPermissionsCount(server, world);

Server s = getServer(server);
if (s == null)
return count;

for (Group g : s.getGroups())
count += g.getPermissionsCount(server, world);
for (TimedValue<Group> g : s.getTimedGroups())
count += g.getValue().getPermissionsCount(server, world);

World w = s.getWorld(world);
if (world == null)
return count;

for (Group g : w.getGroups())
count += g.getPermissionsCount(server, world);
for (TimedValue<Group> g : w.getTimedGroups())
count += g.getValue().getPermissionsCount(server, world);

return count;
}
Expand Down Expand Up @@ -354,38 +492,48 @@ public String buildSuffix(String server, String world)
return Statics.isEmpty(suffix) ? "" : suffix.substring(0, suffix.length() - 1) + ChatColor.RESET;
}

Long getNextTimedPermission(Set<Group> checkedgroups)
Long getNextTimedEntry(Set<Group> checkedgroups)
{
if (checkedgroups.contains(this))
return null;
checkedgroups.add(this);

boolean dosavegroups = false;
boolean dosaveperms = false;
boolean dosave = false;

Long next = null;

//timed inheritances
for (int i = 0; i < timedInheritances.size(); i++)
List<List<TimedValue<String>>> ll = new ArrayList();
ll.add(timedInheritances);
for (Server s : servers.values())
{
TimedValue g = timedInheritances.get(i);
long end = g.getStart().getTime() + g.getDuration() * 1000;
if (end < System.currentTimeMillis())
{
timedInheritances.remove(g);
i--;
dosavegroups = true;
}
else
ll.add(s.getTimedGroupsString());
for (World w : s.getWorlds().values())
ll.add(w.getTimedGroupsString());
}

for (List<TimedValue<String>> l : ll)
{
for (int i = 0; i < l.size(); i++)
{
next = next == null ? end : Math.min(next, end);
TimedValue g = l.get(i);
long end = g.getStart().getTime() + g.getDuration() * 1000;
if (end < System.currentTimeMillis())
{
//remove timed group
l.remove(g);
i--;
dosave = true;
}
else
{
next = next == null ? end : Math.min(next, end);
}
}
}
if (dosavegroups)
BungeePerms.getInstance().getPermissionsManager().getBackEnd().saveGroupTimedInheritances(this);

//timed perms
List<List<TimedValue<String>>> ll = new ArrayList();
ll = new ArrayList();
ll.add(timedPerms);
for (Server s : servers.values())
{
Expand All @@ -405,41 +553,46 @@ Long getNextTimedPermission(Set<Group> checkedgroups)
//remove timed perm
l.remove(p);
i--;
dosaveperms = true;
dosave = true;
}
else
{
next = next == null ? end : Math.min(next, end);
}
}
}
if (dosaveperms)
BungeePerms.getInstance().getPermissionsManager().getBackEnd().saveGroup(this, true);

if (dosavegroups || dosaveperms)
if (dosave)
{
BungeePerms.getInstance().getPermissionsManager().getBackEnd().saveGroup(this, true);
flushCache();
BungeePerms.getInstance().getNetworkNotifier().reloadGroup(this, null);
BungeePerms.getInstance().getEventDispatcher().dispatchGroupChangeEvent(this);
}

//inheritances
for (String s : inheritances)
//inheritances recursive
List<String> allgroups = new ArrayList();
allgroups.addAll(inheritances);
for (TimedValue<String> tgroup : timedInheritances)
allgroups.add(tgroup.getValue());
for (Server srv : servers.values())
{
Group g = BungeePerms.getInstance().getPermissionsManager().getGroup(s);
if (g == null)
continue;
Long end = g.getNextTimedPermission(checkedgroups);
if (end == null)
continue;
next = next == null ? end : Math.min(next, end);
allgroups.addAll(srv.getGroupsString());
for (TimedValue<String> tgroup : srv.getTimedGroupsString())
allgroups.add(tgroup.getValue());
for (World w : srv.getWorlds().values())
{
allgroups.addAll(w.getGroupsString());
for (TimedValue<String> tgroup : w.getTimedGroupsString())
allgroups.add(tgroup.getValue());
}
}
for (TimedValue<String> s : timedInheritances)
for (String s : allgroups)
{
Group g = BungeePerms.getInstance().getPermissionsManager().getGroup(s.getValue());
Group g = BungeePerms.getInstance().getPermissionsManager().getGroup(s);
if (g == null)
continue;
Long end = g.getNextTimedPermission(checkedgroups);
Long end = g.getNextTimedEntry(checkedgroups);
if (end == null)
continue;
next = next == null ? end : Math.min(next, end);
Expand Down

0 comments on commit baff13a

Please sign in to comment.