Skip to content
This repository has been archived by the owner on Jul 27, 2019. It is now read-only.

Commit

Permalink
Add UUID support.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnaude committed Dec 18, 2014
1 parent 7aa95a7 commit f18a339
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/cnaude/purpleirc/CommandHandlers.java
Expand Up @@ -89,6 +89,8 @@ public CommandHandlers(PurpleIRC plugin) {
commands.put("whois", new Whois(plugin));
commands.put("help", new Help(plugin));

commands.put("test", new Test(plugin));

for (String s : commands.keySet()) {
sortedCommands.add(s);
}
Expand Down Expand Up @@ -120,4 +122,4 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
commands.get("help").dispatch(sender, args);
return true;
}
}
}
86 changes: 86 additions & 0 deletions src/main/java/com/cnaude/purpleirc/Commands/Test.java
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2014 cnaude
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cnaude.purpleirc.Commands;

import com.cnaude.purpleirc.PurpleIRC;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

/**
*
* @author cnaude
*/
public class Test implements IRCCommandInterface {

private final PurpleIRC plugin;
private final String usage = "[player name]";
private final String desc = "Testing various Vault methods.";
private final String name = "test";
private final String fullUsage = ChatColor.WHITE + "Usage: " + ChatColor.GOLD + "/irc " + name + " " + usage;

/**
*
* @param plugin
*/
public Test(PurpleIRC plugin) {
this.plugin = plugin;
}

/**
*
* @param sender
* @param args
*/
@Override
public void dispatch(CommandSender sender, String[] args) {
//irc test <username>
if (plugin.vaultHelpers == null) {
sender.sendMessage(ChatColor.RED + "Vault is no enabled!");
return;
}
if (plugin.debugMode()) {
if (args.length >= 2) {
String playername = args[1];
sender.sendMessage(ChatColor.LIGHT_PURPLE + "Testing " + playername);
sender.sendMessage("getGroupPrefix : " + plugin.getGroupPrefix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getGroupSuffix : " + plugin.getGroupSuffix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerPrefix : " + plugin.getPlayerPrefix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerSuffix : " + plugin.getPlayerSuffix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerGroup : " + plugin.getPlayerGroup(plugin.defaultPlayerWorld, playername));
} else {
sender.sendMessage(fullUsage);
}
} else {
sender.sendMessage(ChatColor.RED + "Debug mode must enabled to use this feature.");
}
}

@Override
public String name() {
return name;
}

@Override
public String desc() {
return desc;
}

@Override
public String usage() {
return usage;
}
}
Expand Up @@ -61,7 +61,8 @@ public void run() {
plugin.netPackets.updateTabList(event.getPlayer());
}
}
plugin.updateDisplayNameCache(event.getPlayer());
plugin.updateDisplayNameCache(event.getPlayer());
plugin.updateUuidCache(event.getPlayer());
}
}, 20);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cnaude/purpleirc/PurpleBot.java
Expand Up @@ -133,7 +133,7 @@ public final class PurpleBot {
public CaseInsensitiveMap<Boolean> enableMessageFiltering;
private final CaseInsensitiveMap<Boolean> shortify;
public CaseInsensitiveMap<String> heroChannel;
public CaseInsensitiveMap<String> townyChannel;
public CaseInsensitiveMap<String> townyChannel;
public CaseInsensitiveMap<Collection<String>> opsList;
public CaseInsensitiveMap<Collection<String>> voicesList;
public CaseInsensitiveMap<Collection<String>> worldList;
Expand Down
90 changes: 84 additions & 6 deletions src/main/java/com/cnaude/purpleirc/PurpleIRC.java
Expand Up @@ -76,6 +76,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -127,6 +128,7 @@ public class PurpleIRC extends JavaPlugin {
ircNickPrefixHalfOp,
ircNickPrefixVoice;
private final CaseInsensitiveMap<String> displayNameCache;
public CaseInsensitiveMap<UUID> uuidCache;

public ArrayList<String> kickedPlayers = new ArrayList<>();

Expand Down Expand Up @@ -176,6 +178,7 @@ public class PurpleIRC extends JavaPlugin {
public VanishHook vanishHook;
private YamlConfiguration heroConfig;
private final File cacheFile;
private final File uuidCacheFile;

public PurpleIRC() {
this.MAINCONFIG = "MAIN-CONFIG";
Expand All @@ -188,8 +191,10 @@ public PurpleIRC() {
this.heroChannelMessages = new CaseInsensitiveMap<>();
this.heroActionChannelMessages = new CaseInsensitiveMap<>();
this.displayNameCache = new CaseInsensitiveMap<>();
this.uuidCache = new CaseInsensitiveMap<>();
this.hostCache = new HashMap<>();
this.cacheFile = new File("plugins/PurpleIRC/displayName.cache");
this.uuidCacheFile = new File("plugins/PurpleIRC/uuid.cache");
}

/**
Expand All @@ -209,6 +214,7 @@ public void onEnable() {
saveConfig();
loadConfig();
loadDisplayNameCache();
loadUuidCache();
if (identServerEnabled) {
logInfo("Starting Ident Server ...");
try {
Expand Down Expand Up @@ -414,6 +420,7 @@ public void onDisable() {
}
}
saveDisplayNameCache();
saveUuidCache();
}

/**
Expand Down Expand Up @@ -910,6 +917,18 @@ public String getPlayerGroup(Player player) {
}
return ChatColor.translateAlternateColorCodes('&', groupName);
}

/**
*
* @param player
* @return
*/
public UUID getPlayerUuid(String player) {
if (uuidCache.containsKey(player)) {
return uuidCache.get(player);
}
return null;
}

/**
*
Expand All @@ -919,9 +938,10 @@ public String getPlayerGroup(Player player) {
*/
public String getPlayerGroup(String worldName, String player) {
String groupName = "";
if (vaultHelpers != null) {
UUID uuid = getPlayerUuid(player);
if (vaultHelpers != null && uuid != null) {
if (vaultHelpers.permission != null) {
OfflinePlayer offlinePlayer = getServer().getOfflinePlayer(player);
OfflinePlayer offlinePlayer = getServer().getOfflinePlayer(uuid);
if (offlinePlayer != null) {
try {
groupName = vaultHelpers.permission.getPrimaryGroup(worldName, offlinePlayer);
Expand Down Expand Up @@ -963,9 +983,10 @@ public String getPlayerPrefix(Player player) {
*/
public String getPlayerPrefix(String worldName, String player) {
String prefix = "";
if (vaultHelpers != null) {
UUID uuid = getPlayerUuid(player);
if (vaultHelpers != null && uuid != null) {
if (vaultHelpers.chat != null) {
OfflinePlayer offlinePlayer = getServer().getOfflinePlayer(player);
OfflinePlayer offlinePlayer = getServer().getOfflinePlayer(uuid);
if (offlinePlayer != null) {
try {
prefix = vaultHelpers.chat.getPlayerPrefix(worldName, offlinePlayer);
Expand Down Expand Up @@ -1007,9 +1028,10 @@ public String getPlayerSuffix(Player player) {
*/
public String getPlayerSuffix(String worldName, String player) {
String suffix = "";
if (vaultHelpers != null) {
UUID uuid = getPlayerUuid(player);
if (vaultHelpers != null && uuid != null) {
if (vaultHelpers.chat != null) {
OfflinePlayer offlinePlayer = getServer().getOfflinePlayer(player);
OfflinePlayer offlinePlayer = getServer().getOfflinePlayer(uuid);
if (offlinePlayer != null) {
try {
suffix = vaultHelpers.chat.getPlayerSuffix(worldName, offlinePlayer);
Expand Down Expand Up @@ -1066,6 +1088,25 @@ public void updateDisplayNameCache(String player, String displayName) {
logDebug("Caching displayName for " + player + " = " + displayName);
displayNameCache.put(player, displayName);
}

/**
*
* @param player
*/
public void updateUuidCache(Player player) {
logDebug("Caching UUID for " + player.getName() + " = " + player.getUniqueId().toString());
uuidCache.put(player.getName(), player.getUniqueId());
}

/**
*
* @param player
* @param uuid
*/
public void updateUuidCache(String player, UUID uuid) {
logDebug("Caching UUID for " + player + " = " + uuid.toString());
uuidCache.put(player, uuid);
}

/**
*
Expand Down Expand Up @@ -1251,6 +1292,43 @@ public void loadDisplayNameCache() {
logError(e.getMessage());
}
}

public void saveUuidCache() {
BufferedWriter writer;
try {
writer = new BufferedWriter(new FileWriter(uuidCacheFile));
} catch (IOException ex) {
logError(ex.getMessage());
return;
}

try {
for (String s : uuidCache.keySet()) {
logDebug("Saving to uuid.cache: " + s + "\t" + uuidCache.get(s).toString());
writer.write(s + "\t" + uuidCache.get(s).toString() + "\n");
}
writer.close();
} catch (IOException ex) {
logError(ex.getMessage());
}
}

public void loadUuidCache() {
try {
try (BufferedReader in = new BufferedReader(new FileReader(uuidCacheFile))) {
String line;
while ((line = in.readLine()) != null) {
if (line.equals("\n")) {
continue;
}
String[] parts = line.split("\t", 2);
updateUuidCache(parts[0], UUID.fromString(parts[1]));
}
}
} catch (IOException | NumberFormatException e) {
logError(e.getMessage());
}
}

public String getPlayerHost(Player player) {
final String playerIP = player.getAddress().getAddress().getHostAddress();
Expand Down

0 comments on commit f18a339

Please sign in to comment.