12 changed files with 63 additions and 461 deletions
@ -1,97 +0,0 @@
|
||||
package ru.simsonic.rscPermissions.InternalCache; |
||||
import java.util.Set; |
||||
import java.util.UUID; |
||||
import org.bukkit.Location; |
||||
import org.bukkit.entity.Player; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowInheritance; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowPermission; |
||||
import ru.simsonic.rscPermissions.InternalCache.LocalCacheTree.ResolutionLeaf; |
||||
|
||||
public class AsyncPlayerInfo |
||||
{ |
||||
public Player player; |
||||
public String name; |
||||
public UUID uuid; |
||||
public int expirience; |
||||
public Location location; |
||||
public Set<String> regions; |
||||
public AsyncPlayerInfo() |
||||
{ |
||||
} |
||||
public AsyncPlayerInfo(String playerName) |
||||
{ |
||||
this.name = playerName; |
||||
} |
||||
public AsyncPlayerInfo(UUID playerUniqueId) |
||||
{ |
||||
this.uuid = playerUniqueId; |
||||
} |
||||
public AsyncPlayerInfo(Player player, Set<String> regions) |
||||
{ |
||||
if(player != null) |
||||
{ |
||||
this.player = player; |
||||
try |
||||
{ |
||||
// minecraft <= 1.7
|
||||
this.name = player.getName(); |
||||
} catch(RuntimeException | NoSuchMethodError ex) { |
||||
// minecraft >= 1.8
|
||||
} |
||||
try |
||||
{ |
||||
// minecraft >= 1.8
|
||||
this.uuid = player.getUniqueId(); |
||||
} catch(RuntimeException | NoSuchMethodError ex) { |
||||
// minecraft <= 1.7
|
||||
} |
||||
this.expirience = player.getLevel(); |
||||
this.location = player.getLocation(); |
||||
this.regions = regions; |
||||
} |
||||
} |
||||
public boolean isPlayerEntityApplicable(String entity) |
||||
{ |
||||
// Test by UUID (minecraft >= 1.8)
|
||||
try |
||||
{ |
||||
if(this.uuid.compareTo(UUID.fromString(entity)) == 0) |
||||
return true; |
||||
} catch(RuntimeException ex) { |
||||
// Server doesn't support this yet
|
||||
} |
||||
// Test by name (minecraft <= 1.7)
|
||||
try |
||||
{ |
||||
if(this.name.equalsIgnoreCase(entity)) |
||||
return true; |
||||
} catch(RuntimeException ex) { |
||||
// Server already doesn't support this
|
||||
} |
||||
return false; |
||||
} |
||||
public boolean isPlayerPermissionApplicable(RowPermission row) |
||||
{ |
||||
if(isPlayerEntityApplicable(row.entity) || "".equals(row.entity)) |
||||
return (row.destination.isLocationApplicable(location, regions, null) && row.expirience <= expirience); |
||||
return false; |
||||
} |
||||
public boolean isGroupPermissionApplicable(RowPermission row, ResolutionLeaf leaf) |
||||
{ |
||||
if(row.entity.equalsIgnoreCase(leaf.group) || "".equals(row.entity)) |
||||
return (row.destination.isLocationApplicable(location, regions, leaf.instance) && row.expirience <= expirience); |
||||
return false; |
||||
} |
||||
public boolean isPlayerInheritanceApplicable(RowInheritance row) |
||||
{ |
||||
if(isPlayerEntityApplicable(row.entity)) |
||||
return (row.destination.isLocationApplicable(location, regions, row.instance) && row.expirience <= expirience); |
||||
return false; |
||||
} |
||||
public boolean isGroupInheritanceApplicable(RowInheritance row, ResolutionLeaf leaf) |
||||
{ |
||||
if(row.entity.equalsIgnoreCase(leaf.group)) |
||||
return (row.destination.isLocationApplicable(location, regions, leaf.instance) && row.expirience <= expirience); |
||||
return false; |
||||
} |
||||
} |
@ -1,71 +0,0 @@
|
||||
package ru.simsonic.rscPermissions.InternalCache; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
import ru.simsonic.rscPermissions.DataTypes.EntityType; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowEntity; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowInheritance; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowPermission; |
||||
import ru.simsonic.rscPermissions.BukkitPluginMain; |
||||
|
||||
public class LocalCacheData |
||||
{ |
||||
protected final BukkitPluginMain plugin; |
||||
protected final HashMap<String, RowEntity> entities_g = new HashMap<>(); |
||||
protected final HashMap<String, RowEntity> entities_u = new HashMap<>(); |
||||
protected final ConcurrentHashMap<String, String> prefixes_u = new ConcurrentHashMap<>(); |
||||
protected final ConcurrentHashMap<String, String> suffixes_u = new ConcurrentHashMap<>(); |
||||
protected final ArrayList<RowPermission> permissions_p2g = new ArrayList<>(); |
||||
protected final ArrayList<RowPermission> permissions_p2u = new ArrayList<>(); |
||||
protected final ArrayList<RowInheritance> inheritance_g2g = new ArrayList<>(); |
||||
protected final ArrayList<RowInheritance> inheritance_g2u = new ArrayList<>(); |
||||
protected LocalCacheData(BukkitPluginMain rscp) |
||||
{ |
||||
this.plugin = rscp; |
||||
} |
||||
public synchronized int ImportEntities(RowEntity[] rows) |
||||
{ |
||||
entities_g.clear(); |
||||
entities_u.clear(); |
||||
if(rows == null) |
||||
return 0; |
||||
for(RowEntity row : rows) |
||||
{ |
||||
if(row.entityType == EntityType.group) |
||||
entities_g.put(row.entity.toLowerCase(), row); |
||||
else |
||||
entities_u.put(row.entity.toLowerCase(), row); |
||||
} |
||||
return entities_g.size() + entities_u.size(); |
||||
} |
||||
public synchronized int ImportPermissions(RowPermission[] rows) |
||||
{ |
||||
permissions_p2g.clear(); |
||||
permissions_p2u.clear(); |
||||
if(rows == null) |
||||
return 0; |
||||
for(RowPermission row : rows) |
||||
{ |
||||
if(row.entityType == EntityType.group) |
||||
permissions_p2g.add(row); |
||||
else |
||||
permissions_p2u.add(row); |
||||
} |
||||
return permissions_p2g.size() + permissions_p2u.size(); |
||||
} |
||||
public synchronized int ImportInheritance(RowInheritance[] rows) |
||||
{ |
||||
inheritance_g2g.clear(); |
||||
inheritance_g2u.clear(); |
||||
if(rows == null) |
||||
return 0; |
||||
for(RowInheritance row : rows) |
||||
{ |
||||
if(row.childType == EntityType.group) |
||||
inheritance_g2g.add(row); |
||||
else |
||||
inheritance_g2u.add(row); |
||||
} |
||||
return inheritance_g2g.size() + inheritance_g2u.size(); |
||||
} |
||||
} |
@ -1,65 +0,0 @@
|
||||
package ru.simsonic.rscPermissions.InternalCache; |
||||
import java.util.ArrayList; |
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowEntity; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowInheritance; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowPermission; |
||||
import ru.simsonic.rscPermissions.BukkitPluginMain; |
||||
import ru.simsonic.rscPermissions.API.Settings; |
||||
|
||||
public class LocalCacheFunctions extends LocalCacheTree |
||||
{ |
||||
public LocalCacheFunctions(BukkitPluginMain rscp) |
||||
{ |
||||
super(rscp); |
||||
} |
||||
public String userGetPrefix(String user) |
||||
{ |
||||
return prefixes_u.get(user); |
||||
} |
||||
public String userGetSuffix(String user) |
||||
{ |
||||
return suffixes_u.get(user); |
||||
} |
||||
public synchronized String groupGetPrefix(String group) |
||||
{ |
||||
if(group == null || "".equals(group)) |
||||
return null; |
||||
RowEntity entity = entities_g.get(group.toLowerCase()); |
||||
return (entity != null) ? entity.prefix : null; |
||||
} |
||||
public synchronized String groupGetSuffix(String group) |
||||
{ |
||||
if(group == null || "".equals(group)) |
||||
return null; |
||||
RowEntity entity = entities_g.get(group.toLowerCase()); |
||||
return (entity != null) ? entity.suffix : null; |
||||
} |
||||
public synchronized ArrayList<String> getUserGroups(String player) |
||||
{ |
||||
final ArrayList<ResolutionLeaf> tree = mapTrees.get(player.toLowerCase()); |
||||
if(tree == null) |
||||
return null; |
||||
final ArrayList<String> result = new ArrayList<>(); |
||||
for(ResolutionLeaf leaf : tree) |
||||
result.add(leaf.instance != null ? leaf.group + Settings.separator + leaf.instance : leaf.group); |
||||
return result; |
||||
} |
||||
public synchronized Set<String> getAllPossibleGroups() |
||||
{ |
||||
Set<String> result = new HashSet<>(); |
||||
for(RowEntity row : entities_g.values()) |
||||
result.add(row.entity.toLowerCase()); |
||||
for(RowPermission row : permissions_p2g) |
||||
result.add(row.entity.toLowerCase()); |
||||
for(RowInheritance row : inheritance_g2g) |
||||
{ |
||||
result.add(row.entity.toLowerCase()); |
||||
result.add(row.parent.toLowerCase()); |
||||
} |
||||
for(RowInheritance row : inheritance_g2u) |
||||
result.add(row.parent.toLowerCase()); |
||||
return result; |
||||
} |
||||
} |
@ -1,171 +0,0 @@
|
||||
package ru.simsonic.rscPermissions.InternalCache; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.UUID; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
import org.bukkit.entity.Player; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowEntity; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowInheritance; |
||||
import ru.simsonic.rscPermissions.DataTypes.RowPermission; |
||||
import ru.simsonic.rscPermissions.BukkitPluginMain; |
||||
import ru.simsonic.rscPermissions.API.Settings; |
||||
import ru.simsonic.rscUtilityLibrary.TextProcessing.GenericChatCodes; |
||||
|
||||
public class LocalCacheTree extends LocalCacheData |
||||
{ |
||||
protected static class ResolutionLeaf |
||||
{ |
||||
public String group; |
||||
public String instance; |
||||
public RowInheritance row; |
||||
} |
||||
protected LocalCacheTree(BukkitPluginMain rscp) |
||||
{ |
||||
super(rscp); |
||||
} |
||||
public final ConcurrentHashMap<String, ArrayList<ResolutionLeaf>> mapTrees = new ConcurrentHashMap<>(); |
||||
public final ConcurrentHashMap<String, HashMap<String, Boolean>> mapPermissions = new ConcurrentHashMap<>(); |
||||
protected final RowInheritance defaultInheritance = new RowInheritance(); |
||||
public void setDefaultGroup(String defaultGroup) |
||||
{ |
||||
defaultInheritance.parent = defaultGroup; |
||||
defaultInheritance.deriveInstance(); |
||||
} |
||||
public synchronized void clear() |
||||
{ |
||||
mapTrees.clear(); |
||||
mapPermissions.clear(); |
||||
prefixes_u.clear(); |
||||
suffixes_u.clear(); |
||||
entities_g.clear(); |
||||
entities_u.clear(); |
||||
permissions_p2g.clear(); |
||||
permissions_p2u.clear(); |
||||
inheritance_g2g.clear(); |
||||
inheritance_g2u.clear(); |
||||
} |
||||
public synchronized void calculateStartupPermissions() |
||||
{ |
||||
final HashSet<String> playerEntities = new HashSet<>(); |
||||
// Undefined player
|
||||
playerEntities.add(""); // Зачем я его тут добавил?!?
|
||||
// Defined players (in any table)
|
||||
playerEntities.addAll(entities_u.keySet()); |
||||
for(RowPermission row : permissions_p2u) |
||||
playerEntities.add(row.entity); |
||||
for(RowInheritance row : inheritance_g2u) |
||||
playerEntities.add(row.entity); |
||||
// Recalculate
|
||||
for(String entityNameOrUUID : playerEntities) |
||||
calculateBasePermissions(entityNameOrUUID); |
||||
} |
||||
public synchronized void calculateBasePermissions(String playerName) |
||||
{ |
||||
AsyncPlayerInfo p2rc = new AsyncPlayerInfo(playerName); |
||||
HashMap<String, Boolean> list = treeToPermissions(p2rc); |
||||
mapPermissions.put(playerName, list); |
||||
} |
||||
public synchronized void calculateBasePermissions(UUID playerUniqueId) |
||||
{ |
||||
AsyncPlayerInfo p2rc = new AsyncPlayerInfo(playerUniqueId); |
||||
HashMap<String, Boolean> list = treeToPermissions(p2rc); |
||||
mapPermissions.put(playerUniqueId.toString().replace("-", "").toLowerCase(), list); |
||||
} |
||||
public void calculatePlayerPermissions(Player player) |
||||
{ |
||||
final AsyncPlayerInfo api = new AsyncPlayerInfo(player, plugin.regionListProvider.getPlayerRegions(player)); |
||||
// plugin.recalculatingPlayers.offer(api);
|
||||
} |
||||
public synchronized HashMap<String, Boolean> treeToPermissions(AsyncPlayerInfo p2rc) |
||||
{ |
||||
final HashMap<String, Boolean> permissions = new HashMap<>(); |
||||
String prefix = ""; |
||||
String suffix = ""; |
||||
ArrayList<ResolutionLeaf> tree = buildUserTree(p2rc); |
||||
if(p2rc.name != null) |
||||
mapTrees.put(p2rc.name.toLowerCase(), tree); |
||||
// Group permissions
|
||||
for(ResolutionLeaf leaf : tree) |
||||
{ |
||||
for(RowPermission row : permissions_p2g) |
||||
if(p2rc.isGroupPermissionApplicable(row, leaf)) |
||||
{ |
||||
String permission = row.permission; |
||||
// Additional processing
|
||||
if(permission.contains(Settings.instantiator) && (leaf.instance != null)) |
||||
permission = permission.replace(Settings.instantiator, leaf.instance); |
||||
permissions.put(permission, row.value); |
||||
} |
||||
RowEntity entity = entities_g.get(leaf.group.toLowerCase()); |
||||
if(entity != null) |
||||
{ |
||||
if(entity.prefix != null && !"".equals(entity.prefix)) |
||||
prefix = entity.prefix.replace("%", prefix); |
||||
if(entity.suffix != null && !"".equals(entity.suffix)) |
||||
suffix = entity.suffix.replace("%", suffix); |
||||
prefix = prefix.replace(Settings.instantiator, leaf.instance); |
||||
suffix = suffix.replace(Settings.instantiator, leaf.instance); |
||||
} |
||||
} |
||||
// User permissions
|
||||
for(RowPermission row : permissions_p2u) |
||||
if(p2rc.isPlayerPermissionApplicable(row)) |
||||
permissions.put(row.permission, row.value); |
||||
if(p2rc.name != null) |
||||
{ |
||||
RowEntity entity = entities_u.get(p2rc.name.toLowerCase()); |
||||
if(entity != null) |
||||
{ |
||||
if(entity.prefix != null && !"".equals(entity.prefix)) |
||||
prefix = entity.prefix.replace("%", prefix); |
||||
if(entity.suffix != null && !"".equals(entity.suffix)) |
||||
suffix = entity.suffix.replace("%", suffix); |
||||
} |
||||
prefixes_u.put(p2rc.name, GenericChatCodes.processStringStatic(prefix)); |
||||
suffixes_u.put(p2rc.name, GenericChatCodes.processStringStatic(suffix)); |
||||
} |
||||
return permissions; |
||||
} |
||||
private ArrayList<ResolutionLeaf> buildUserTree(AsyncPlayerInfo p2rc) |
||||
{ |
||||
// User's direct inheritance
|
||||
ArrayList<RowInheritance> parentRows = new ArrayList<>(); |
||||
for(RowInheritance row : inheritance_g2u) |
||||
if(p2rc.isPlayerInheritanceApplicable(row)) |
||||
parentRows.add(row); |
||||
Collections.sort(parentRows); |
||||
// Indirect default group
|
||||
if(parentRows.isEmpty() || plugin.settings.isDefaultForever()) |
||||
parentRows.add(0, defaultInheritance); |
||||
ArrayList<ResolutionLeaf> resultTree = new ArrayList<>(); |
||||
// Parent deep inheritances
|
||||
for(RowInheritance row : parentRows) |
||||
{ |
||||
ResolutionLeaf newleaf = new ResolutionLeaf(); |
||||
newleaf.group = row.parent; |
||||
newleaf.instance = row.instance; |
||||
newleaf.row = row; |
||||
buildGroupTree(p2rc, newleaf, resultTree); |
||||
} |
||||
return resultTree; |
||||
} |
||||
private void buildGroupTree(AsyncPlayerInfo p2rc, ResolutionLeaf findAndOpen, ArrayList<ResolutionLeaf> result) |
||||
{ |
||||
ArrayList<RowInheritance> parentRows = new ArrayList<>(inheritance_g2g.size() >> 2); |
||||
for(RowInheritance row : inheritance_g2g) |
||||
if(p2rc.isGroupInheritanceApplicable(row, findAndOpen)) |
||||
parentRows.add(row); |
||||
Collections.sort(parentRows); |
||||
for(RowInheritance row : parentRows) |
||||
{ |
||||
ResolutionLeaf newleaf = new ResolutionLeaf(); |
||||
newleaf.group = row.parent; |
||||
newleaf.instance = (row.instance != null) ? row.instance : findAndOpen.instance; |
||||
newleaf.row = row; |
||||
buildGroupTree(p2rc, newleaf, result); |
||||
} |
||||
result.add(findAndOpen); |
||||
} |
||||
} |
Loading…
Reference in new issue