This commit is contained in:
Lemon 2017-10-13 23:38:05 +05:00 committed by GitHub
parent ed2f15cc54
commit d878fd2458
5 changed files with 238 additions and 35 deletions

View File

@ -1,6 +1,7 @@
package me.totalfreedom.totalfreedommod.util;
import java.util.HashSet;
import java.util.UUID;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
@ -13,7 +14,7 @@ import org.bukkit.material.MaterialData;
public class DepreciationAggregator
{
public static Block getTargetBlock(LivingEntity entity, HashSet<Byte> transparent, int maxDistance)
public static Block getTargetBlock(LivingEntity entity, HashSet<Material> transparent, int maxDistance)
{
return entity.getTargetBlock(transparent, maxDistance);
}

View File

@ -24,6 +24,21 @@ public class FSync
}.runTask(plugin);
}
public static void playerMsg(final CommandSender sender, final String message)
{
final TotalFreedomMod plugin = TotalFreedomMod.plugin();
new BukkitRunnable()
{
@Override
public void run()
{
sender.sendMessage(message);
}
}.runTask(plugin);
}
public static void playerKick(final Player player, final String reason)
{
final TotalFreedomMod plugin = TotalFreedomMod.plugin();

View File

@ -11,6 +11,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -19,6 +20,8 @@ import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import static me.totalfreedom.totalfreedommod.util.FUtil.CHAT_RAINBOW;
import static me.totalfreedom.totalfreedommod.util.FUtil.FOUNDER;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
@ -31,37 +34,39 @@ import org.bukkit.scheduler.BukkitTask;
public class FUtil
{
private static final Random RANDOM = new Random();
//
private static final Random RANDOM;
public static final String SAVED_FLAGS_FILENAME = "savedflags.dat";
// See https://github.com/TotalFreedom/License - None of the listed names may be removed.
public static final List<String> DEVELOPERS = Arrays.asList("Madgeek1450", "Prozza", "Wild1145", "WickedGamingUK", "aggelosQQ");
public static String DATE_STORAGE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
public static final Map<String, ChatColor> CHAT_COLOR_NAMES = new HashMap<>();
public static final List<ChatColor> CHAT_COLOR_POOL = Arrays.asList(
ChatColor.DARK_BLUE,
ChatColor.DARK_GREEN,
ChatColor.DARK_AQUA,
ChatColor.DARK_RED,
ChatColor.DARK_PURPLE,
ChatColor.GOLD,
ChatColor.BLUE,
ChatColor.GREEN,
ChatColor.AQUA,
ChatColor.RED,
ChatColor.LIGHT_PURPLE,
ChatColor.YELLOW);
static
{
for (ChatColor chatColor : CHAT_COLOR_POOL)
{
CHAT_COLOR_NAMES.put(chatColor.name().toLowerCase().replace("_", ""), chatColor);
public static final List<String> DEVELOPERS;
public static final List<String> FOUNDER;
public static String DATE_STORAGE_FORMAT;
public static final Map<String, ChatColor> CHAT_COLOR_NAMES;
public static final Map<String, ChatColor> CHAT_RAINBOW_NAMES;
public static List<String> BLOCKED_CODES;
public static final List<ChatColor> CHAT_COLOR_POOL;
public static final List<ChatColor> CHAT_RAINBOW;
private static Iterator<ChatColor> color;
static {
RANDOM = new Random();
DEVELOPERS = Arrays.asList("Madgeek1450", "Prozza", "WickedGamingUK", "aggelosQQ", "OxLemonxO", "Commodore64x", "Wild1145");
FOUNDER = Arrays.asList("markbyron");
FUtil.DATE_STORAGE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
CHAT_COLOR_NAMES = new HashMap<String, ChatColor>();
CHAT_RAINBOW_NAMES = new HashMap<String, ChatColor>();
FUtil.BLOCKED_CODES = new ArrayList<String>();
CHAT_COLOR_POOL = Arrays.asList(ChatColor.DARK_BLUE, ChatColor.DARK_GREEN, ChatColor.DARK_AQUA, ChatColor.DARK_RED, ChatColor.DARK_PURPLE, ChatColor.GOLD, ChatColor.BLUE, ChatColor.GREEN, ChatColor.AQUA, ChatColor.RED, ChatColor.LIGHT_PURPLE, ChatColor.YELLOW);
CHAT_RAINBOW = Arrays.asList(ChatColor.DARK_RED, ChatColor.RED, ChatColor.GOLD, ChatColor.YELLOW, ChatColor.GREEN, ChatColor.DARK_GREEN, ChatColor.AQUA, ChatColor.DARK_AQUA, ChatColor.BLUE, ChatColor.DARK_BLUE, ChatColor.DARK_PURPLE, ChatColor.LIGHT_PURPLE);
for (final ChatColor chatColor : FUtil.CHAT_COLOR_POOL) {
FUtil.CHAT_COLOR_NAMES.put(chatColor.name().toLowerCase().replace("_", ""), chatColor);
}
for (final ChatColor chatColor : FUtil.CHAT_RAINBOW) {
FUtil.CHAT_RAINBOW_NAMES.put(chatColor.name().toLowerCase().replace("_", ""), chatColor);
}
FUtil.color = FUtil.CHAT_RAINBOW.iterator();
}
private FUtil()
private FUtil()
{
}
@ -142,7 +147,7 @@ public class FUtil
final File[] coreDumps = new File(".").listFiles(new FileFilter()
{
@Override
public boolean accept(File file)
public boolean accept(File file)
{
return file.getName().startsWith("java.core");
}
@ -343,7 +348,7 @@ public class FUtil
//getField: Borrowed from WorldEdit
@SuppressWarnings("unchecked")
public static <T> T getField(Object from, String name)
public static <T> T getField(Object from, String name)
{
Class<?> checkClass = from.getClass();
do
@ -354,19 +359,29 @@ public class FUtil
field.setAccessible(true);
return (T) field.get(from);
}
}
catch (NoSuchFieldException | IllegalAccessException ex)
{
}
} while (checkClass.getSuperclass() != Object.class
&& ((checkClass = checkClass.getSuperclass()) != null));
&& ((checkClass = checkClass.getSuperclass()) != null));
return null;
}
public static ChatColor randomChatColor()
{
return CHAT_COLOR_POOL.get(RANDOM.nextInt(CHAT_COLOR_POOL.size()));
public static ChatColor randomChatColor() {
return FUtil.CHAT_COLOR_POOL.get(FUtil.RANDOM.nextInt(FUtil.CHAT_COLOR_POOL.size()));
}
public static ChatColor rainbowChatColor() {
if (FUtil.color.hasNext()) {
return FUtil.color.next();
}
FUtil.color = FUtil.CHAT_RAINBOW.iterator();
return FUtil.color.next();
}
public static String colorize(String string)

View File

@ -0,0 +1,104 @@
package me.totalfreedom.totalfreedommod.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
public class History
{
public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void reportHistory(final CommandSender sender, final String username)
{
new BukkitRunnable()
{
@Override
public void run()
{
UUID uuid = UUIDFetcher.fetch(username);
if (uuid != null)
{
Gson gson = new GsonBuilder().create();
String compactUuid = uuid.toString().replace("-", "");
try
{
URL url = new URL("https://api.mojang.com/user/profiles/" + compactUuid + "/names");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
FName[] oldNames = gson.fromJson(reader, FName[].class);
if (oldNames == null)
{
FSync.playerMsg(sender, ChatColor.RED + "Player not found!");
return;
}
reader.close();
conn.disconnect();
Arrays.sort(oldNames);
printHistory(sender, oldNames);
}
catch (Exception ex)
{
FSync.playerMsg(sender, ChatColor.RED + "Error, see logs for more details.");
FLog.severe(ex);
}
}
else
{
FSync.playerMsg(sender, ChatColor.RED + "Player not found!");
}
}
}.runTaskAsynchronously(TotalFreedomMod.plugin());
}
private static void printHistory(CommandSender sender, FName[] oldNames)
{
if (oldNames.length == 1)
{
FSync.playerMsg(sender, ChatColor.GREEN + oldNames[0].getName() + ChatColor.GOLD + " has never changed their name.");
return;
}
FSync.playerMsg(sender, ChatColor.GOLD + "Original name: " + ChatColor.GREEN + oldNames[0].getName());
for (int i = 1; i < oldNames.length; i++)
{
Date date = new Date(oldNames[i].getChangedToAt());
String formattedDate = df.format(date);
FSync.playerMsg(sender, ChatColor.BLUE + formattedDate + ChatColor.GOLD + " changed to " + ChatColor.GREEN + oldNames[i].getName());
}
}
private static class FName implements Comparable<FName>
{
private String name;
private long changedToAt;
@Override
public int compareTo(FName other)
{
return Long.compare(this.changedToAt, other.changedToAt);
}
public String getName()
{
return name;
}
public long getChangedToAt()
{
return changedToAt;
}
}
}

View File

@ -0,0 +1,68 @@
package me.totalfreedom.totalfreedommod.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
// UUIDFetcher retrieves UUIDs from usernames via web requests to Mojang.
public class UUIDFetcher
{
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
public static UUID fetch(String name)
{
try
{
Gson gson = new GsonBuilder().create();
UUID uuid;
String body = gson.toJson(name);
URL url = new URL(PROFILE_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream stream = connection.getOutputStream();
stream.write(body.getBytes());
stream.flush();
stream.close();
FetchedUuid[] id = gson.fromJson(
new InputStreamReader(connection.getInputStream()),
FetchedUuid[].class);
if (id.length == 0 || id[0].getID() == null)
{
return null;
}
String idd = id[0].getID();
uuid = UUID.fromString(idd.substring(0, 8) + "-" + idd.substring(8, 12)
+ "-" + idd.substring(12, 16) + "-" + idd.substring(16, 20) + "-"
+ idd.substring(20, 32));
return uuid;
}
catch (IOException ex)
{
FLog.severe(ex);
}
return null;
}
private class FetchedUuid
{
private String id;
public String getID()
{
return id;
}
}
}