Saber-Factions/src/main/java/com/massivecraft/factions/util/MiscUtil.java

135 lines
4.3 KiB
Java
Raw Normal View History

2011-07-18 22:06:02 +02:00
package com.massivecraft.factions.util;
2011-04-08 21:01:46 +02:00
2014-11-06 01:36:47 +01:00
import com.massivecraft.factions.Conf;
2014-12-07 04:44:00 +01:00
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.SavageFactions;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.zcore.util.TL;
import org.bukkit.ChatColor;
import org.bukkit.entity.Creature;
import org.bukkit.entity.Entity;
2014-04-04 20:55:21 +02:00
import org.bukkit.entity.EntityType;
import java.util.ArrayList;
2014-04-04 20:55:21 +02:00
import java.util.Arrays;
import java.util.HashSet;
2014-12-07 04:44:00 +01:00
import java.util.List;
import java.util.logging.Level;
2014-04-04 20:55:21 +02:00
public class MiscUtil {
2018-07-12 18:11:07 +02:00
/// TODO create tag whitelist!!
public static HashSet<String> substanceChars =
new HashSet<>(Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "plugin", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"));
2018-07-12 18:11:07 +02:00
2014-04-04 20:55:21 +02:00
public static EntityType creatureTypeFromEntity(Entity entity) {
if (!(entity instanceof Creature)) {
return null;
}
2014-07-01 22:10:18 +02:00
String name = entity.getClass().getSimpleName();
name = name.substring(5); // Remove "Craft"
2014-04-04 20:55:21 +02:00
return EntityType.fromName(name);
}
// Inclusive range
public static long[] range(long start, long end) {
long[] values = new long[(int) Math.abs(end - start) + 1];
if (end < start) {
2014-07-01 22:10:18 +02:00
long oldstart = start;
start = end;
end = oldstart;
2014-04-04 20:55:21 +02:00
}
for (long i = start; i <= end; i++) {
values[(int) (i - start)] = i;
}
return values;
}
public static String getComparisonString(String str) {
2017-12-19 11:18:13 +01:00
StringBuilder ret = new StringBuilder();
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
str = ChatColor.stripColor(str);
str = str.toLowerCase();
2014-04-04 20:55:21 +02:00
for (char c : str.toCharArray()) {
if (substanceChars.contains(String.valueOf(c))) {
2017-12-19 11:18:13 +01:00
ret.append(c);
2014-04-04 20:55:21 +02:00
}
2014-07-01 22:10:18 +02:00
}
2017-12-19 11:18:13 +01:00
return ret.toString().toLowerCase();
2014-04-04 20:55:21 +02:00
}
public static ArrayList<String> validateTag(String str) {
2017-12-19 11:18:13 +01:00
ArrayList<String> errors = new ArrayList<>();
if (getComparisonString(str).length() < Conf.factionTagLengthMin) {
errors.add(SavageFactions.plugin.txt.parse(TL.GENERIC_FACTIONTAG_TOOSHORT.toString(), Conf.factionTagLengthMin));
}
if (str.length() > Conf.factionTagLengthMax) {
errors.add(SavageFactions.plugin.txt.parse(TL.GENERIC_FACTIONTAG_TOOLONG.toString(), Conf.factionTagLengthMax));
}
for (char c : str.toCharArray()) {
if (!substanceChars.contains(String.valueOf(c))) {
errors.add(SavageFactions.plugin.txt.parse(TL.GENERIC_FACTIONTAG_ALPHANUMERIC.toString(), c));
}
}
return errors;
}
2014-12-07 04:44:00 +01:00
public static Iterable<FPlayer> rankOrder(Iterable<FPlayer> players) {
2017-12-19 11:18:13 +01:00
List<FPlayer> admins = new ArrayList<>();
2018-03-26 23:43:15 +02:00
List<FPlayer> coleaders = new ArrayList<>();
2017-12-19 11:18:13 +01:00
List<FPlayer> moderators = new ArrayList<>();
List<FPlayer> normal = new ArrayList<>();
List<FPlayer> recruit = new ArrayList<>();
2014-12-07 04:44:00 +01:00
for (FPlayer player : players) {
// Fix for some data being broken when we added the recruit rank.
if (player.getRole() == null) {
player.setRole(Role.NORMAL);
SavageFactions.plugin.log(Level.WARNING, String.format("Player %s had null role. Setting them to normal. This isn't good D:", player.getName()));
}
2014-12-07 04:44:00 +01:00
switch (player.getRole()) {
case LEADER:
2014-12-07 04:44:00 +01:00
admins.add(player);
break;
2018-03-26 23:43:15 +02:00
case COLEADER:
admins.add(player);
break;
2014-12-07 04:44:00 +01:00
case MODERATOR:
moderators.add(player);
break;
case NORMAL:
normal.add(player);
break;
case RECRUIT:
recruit.add(player);
break;
2014-12-07 04:44:00 +01:00
}
}
2017-12-19 11:18:13 +01:00
List<FPlayer> ret = new ArrayList<>();
2014-12-07 04:44:00 +01:00
ret.addAll(admins);
2018-03-26 23:43:15 +02:00
ret.addAll(coleaders);
2014-12-07 04:44:00 +01:00
ret.addAll(moderators);
ret.addAll(normal);
ret.addAll(recruit);
2014-12-07 04:44:00 +01:00
return ret;
}
2011-04-08 21:01:46 +02:00
}