/* * Copyright (c) PikaMug and contributors * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package me.pikamug.quests.util; import io.github.znetworkw.znpcservers.npc.NPC; import lol.pyr.znpcsplus.api.entity.EntityProperty; import lol.pyr.znpcsplus.api.npc.Npc; import lol.pyr.znpcsplus.api.npc.NpcEntry; import me.pikamug.quests.dependencies.BukkitDependencies; import me.pikamug.quests.quests.Quest; import me.clip.placeholderapi.PlaceholderAPI; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; public class BukkitConfigUtil { private static final Pattern hexPattern = Pattern.compile("(?i)%#([0-9A-F]{6})%"); /** * Checks whether items in a list are instances of a class
*
* Does NOT check whether list objects are null
*
* @param list The list to check objects of
* @param clazz The class to compare against
* @return false if arg is null or list object does not match
*/
public static boolean checkList(final List> list, final Class> clazz) {
if (list == null || clazz == null) {
return false;
}
for (final Object o : list) {
if (o == null) {
Bukkit.getLogger().severe(clazz.getSimpleName() + " type in Quests file was \"null\"");
return false;
}
if (!clazz.isAssignableFrom(o.getClass())) {
return false;
}
}
return true;
}
public static Location getLocation(final String arg) {
final String[] info = arg.split(" ");
if (info.length < 4) {
return null;
}
final StringBuilder sb = new StringBuilder();
int index = 0;
final int xIndex = info.length -3;
final int yIndex = info.length -2;
final int zIndex = info.length -1;
while (index < xIndex) {
final String s = info[index];
if (index == 0) {
sb.append(s);
} else {
sb.append(" ").append(s);
}
index++;
}
final String world = sb.toString();
final double x;
final double y;
final double z;
try {
x = Double.parseDouble(info[xIndex]);
y = Double.parseDouble(info[yIndex]);
z = Double.parseDouble(info[zIndex]);
} catch (final Exception e) {
Bukkit.getLogger().severe("Please inform Quests developer location was wrong for "
+ world + " " + info[xIndex] + " " + info[yIndex] + " " + info[zIndex] + " ");
return null;
}
if (Bukkit.getServer().getWorld(world) == null) {
Bukkit.getLogger().severe("Quests could not locate world " + world + ", is it loaded?");
return null;
}
return new Location(Bukkit.getServer().getWorld(world), x, y, z);
}
public static String getLocationInfo(final Location loc) {
return Objects.requireNonNull(loc.getWorld()).getName() + " " + loc.getX() + " " + loc.getY() + " "
+ loc.getZ();
}
public static String[] parseStringWithPossibleLineBreaks(final String s, final Quest quest, final Player player) {
String parsed = parseString(s);
if (parsed.contains("", ChatColor.STRIKETHROUGH.toString());
parsed = parsed.replace("
", "\n");
parsed = ChatColor.translateAlternateColorCodes('&', parsed);
final Matcher matcher = hexPattern.matcher(parsed);
while (matcher.find()) {
final StringBuilder hex = new StringBuilder();
hex.append(ChatColor.COLOR_CHAR + "x");
final char[] chars = matcher.group(1).toCharArray();
for (final char aChar : chars) {
hex.append(ChatColor.COLOR_CHAR).append(Character.toLowerCase(aChar));
}
parsed = parsed.replace(matcher.group(), hex.toString());
}
return parsed;
}
}