You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.0 KiB
89 lines
3.0 KiB
10 years ago
|
package ru.simsonic.rscPermissions.API;
|
||
11 years ago
|
import java.util.Set;
|
||
|
import java.util.regex.Matcher;
|
||
|
import java.util.regex.Pattern;
|
||
11 years ago
|
import ru.simsonic.rscUtilityLibrary.TextProcessing.GenericChatCodes;
|
||
11 years ago
|
|
||
|
public class Destination
|
||
|
{
|
||
|
private final String region;
|
||
|
private final String world;
|
||
|
private final String serverId;
|
||
|
private Destination()
|
||
|
{
|
||
|
this.region = null;
|
||
|
this.world = null;
|
||
|
this.serverId = null;
|
||
|
}
|
||
|
private Destination(String region, String world, String serverId)
|
||
|
{
|
||
|
this.region = region;
|
||
|
this.world = world;
|
||
|
this.serverId = serverId;
|
||
|
}
|
||
11 years ago
|
public boolean isServerIdApplicable(String serverId)
|
||
11 years ago
|
{
|
||
11 years ago
|
return wildcardTest(serverId, this.serverId);
|
||
11 years ago
|
}
|
||
11 years ago
|
public boolean isWorldApplicable(String world, String instantiator)
|
||
|
{
|
||
|
if(this.world == null || this.world.isEmpty() || "*".equals(this.world))
|
||
|
return true;
|
||
|
final String instantiated = (instantiator != null && !instantiator.isEmpty())
|
||
|
? this.world.replace(Settings.instantiator, instantiator)
|
||
|
: this.world;
|
||
|
return wildcardTest(world, instantiated);
|
||
|
}
|
||
|
public boolean isRegionApplicable(Set<String> regions, String instantiator)
|
||
|
{
|
||
|
if(this.region == null || "".equals(this.region) || "*".equals(this.region))
|
||
|
return true;
|
||
|
final String instantiated = (instantiator != null && !"".equals(instantiator))
|
||
|
? this.region.replace(Settings.instantiator, instantiator)
|
||
|
: this.region;
|
||
|
for(String regionId : regions)
|
||
|
if(wildcardTest(regionId, instantiated))
|
||
|
return true;
|
||
|
return false;
|
||
|
}
|
||
|
public boolean isRegionApplicable(String[] regions, String instantiator)
|
||
11 years ago
|
{
|
||
|
if(this.region == null || "".equals(this.region) || "*".equals(this.region))
|
||
|
return true;
|
||
11 years ago
|
final String instantiated = (instantiator != null && !"".equals(instantiator))
|
||
11 years ago
|
? this.region.replace(Settings.instantiator, instantiator)
|
||
11 years ago
|
: this.region;
|
||
11 years ago
|
for(String regionId : regions)
|
||
11 years ago
|
if(wildcardTest(regionId, instantiated))
|
||
11 years ago
|
return true;
|
||
|
return false;
|
||
|
}
|
||
11 years ago
|
private static boolean wildcardTest(String testing, String pattern)
|
||
11 years ago
|
{
|
||
|
if(pattern == null || "".equals(pattern))
|
||
|
return true;
|
||
|
if(testing == null || "".equals(testing))
|
||
|
return false;
|
||
11 years ago
|
return GenericChatCodes.wildcardMatch(
|
||
11 years ago
|
"<wildcard>" + testing.toLowerCase() + "</wildcard>",
|
||
|
"<wildcard>" + pattern.toLowerCase() + "</wildcard>");
|
||
|
}
|
||
11 years ago
|
private static final Pattern destinationPattern = Pattern.compile(
|
||
10 years ago
|
"(?:((?:\\w|\\*|\\?)*):)?((?:\\w|\\*|\\?)*)?(?:@((?:\\w|\\*|\\?)*))?");
|
||
|
public static Destination parseDestination(String destination)
|
||
11 years ago
|
{
|
||
11 years ago
|
final Matcher match = destinationPattern.matcher(destination);
|
||
11 years ago
|
if(match.find())
|
||
|
{
|
||
11 years ago
|
final String groupR = match.group(1);
|
||
|
final String groupW = match.group(2);
|
||
|
final String groupS = match.group(3);
|
||
|
final String region = (groupR == null || "".equals(groupR)) ? "*" : groupR;
|
||
|
final String world = (groupW == null || "".equals(groupW)) ? "*" : groupW;
|
||
|
final String serverId = (groupS == null || "".equals(groupS)) ? "*" : groupS;
|
||
11 years ago
|
return new Destination(region, world, serverId);
|
||
|
}
|
||
|
return new Destination();
|
||
|
}
|
||
11 years ago
|
}
|