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;
|
2018-10-23 01:42:57 +02:00
|
|
|
import com.massivecraft.factions.SavageFactions;
|
2018-03-02 07:15:44 +01:00
|
|
|
import com.massivecraft.factions.struct.Role;
|
2015-01-20 19:27:26 +01:00
|
|
|
import com.massivecraft.factions.zcore.util.TL;
|
2011-10-22 17:03:49 +02:00
|
|
|
import org.bukkit.ChatColor;
|
2011-10-08 22:03:44 +02:00
|
|
|
import org.bukkit.entity.Creature;
|
|
|
|
import org.bukkit.entity.Entity;
|
2014-04-04 20:55:21 +02:00
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
|
2014-10-19 07:37:25 +02:00
|
|
|
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;
|
2018-03-02 07:15:44 +01:00
|
|
|
import java.util.logging.Level;
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
public class MiscUtil {
|
2014-08-05 17:17:27 +02:00
|
|
|
|
2018-07-12 18:11:07 +02:00
|
|
|
/// TODO create tag whitelist!!
|
2018-11-02 20:08:40 +01:00
|
|
|
public static HashSet<String> substanceChars =
|
2018-12-09 03:13:18 +01:00
|
|
|
new HashSet<>(Arrays.asList("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("")));
|
2018-07-12 18:11:07 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
public static EntityType creatureTypeFromEntity(Entity entity) {
|
2018-11-07 17:14:42 +01:00
|
|
|
if (!(entity instanceof Creature)) {
|
2014-04-04 20:55:21 +02:00
|
|
|
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
|
|
|
}
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2014-10-19 07:37:25 +02:00
|
|
|
public static ArrayList<String> validateTag(String str) {
|
2017-12-19 11:18:13 +01:00
|
|
|
ArrayList<String> errors = new ArrayList<>();
|
2014-10-19 07:37:25 +02:00
|
|
|
|
|
|
|
if (getComparisonString(str).length() < Conf.factionTagLengthMin) {
|
2018-10-23 01:42:57 +02:00
|
|
|
errors.add(SavageFactions.plugin.txt.parse(TL.GENERIC_FACTIONTAG_TOOSHORT.toString(), Conf.factionTagLengthMin));
|
2014-10-19 07:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (str.length() > Conf.factionTagLengthMax) {
|
2018-10-23 01:42:57 +02:00
|
|
|
errors.add(SavageFactions.plugin.txt.parse(TL.GENERIC_FACTIONTAG_TOOLONG.toString(), Conf.factionTagLengthMax));
|
2014-10-19 07:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (char c : str.toCharArray()) {
|
2018-11-07 17:14:42 +01:00
|
|
|
if (!substanceChars.contains(String.valueOf(c))) {
|
2018-10-23 01:42:57 +02:00
|
|
|
errors.add(SavageFactions.plugin.txt.parse(TL.GENERIC_FACTIONTAG_ALPHANUMERIC.toString(), c));
|
2014-10-19 07:37:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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<>();
|
2018-01-05 03:10:49 +01:00
|
|
|
List<FPlayer> recruit = new ArrayList<>();
|
2014-12-07 04:44:00 +01:00
|
|
|
|
|
|
|
for (FPlayer player : players) {
|
2018-03-02 07:15:44 +01:00
|
|
|
|
|
|
|
// Fix for some data being broken when we added the recruit rank.
|
|
|
|
if (player.getRole() == null) {
|
|
|
|
player.setRole(Role.NORMAL);
|
2018-10-23 01:42:57 +02:00
|
|
|
SavageFactions.plugin.log(Level.WARNING, String.format("Player %s had null role. Setting them to normal. This isn't good D:", player.getName()));
|
2018-03-02 07:15:44 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 04:44:00 +01:00
|
|
|
switch (player.getRole()) {
|
2019-02-11 05:57:45 +01:00
|
|
|
case LEADER:
|
|
|
|
admins.add(player);
|
|
|
|
break;
|
|
|
|
case COLEADER:
|
|
|
|
admins.add(player);
|
|
|
|
break;
|
|
|
|
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);
|
2018-01-05 03:10:49 +01:00
|
|
|
ret.addAll(recruit);
|
2014-12-07 04:44:00 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2011-04-08 21:01:46 +02:00
|
|
|
}
|
|
|
|
|