Added Support for UUID

This commit is contained in:
Sn0wStorm 2014-06-06 01:27:27 +02:00
parent 28dcfbe5d2
commit e8c905e19f
7 changed files with 169 additions and 68 deletions

View File

@ -66,6 +66,8 @@ permissions:
description: Copy Potions
brewery.cmd.delete:
description: Delete Potions
brewery.cmd.persist:
description: Make Potions Persistent
brewery.cmd.reload:
description: Reload config

View File

@ -3,7 +3,9 @@ package com.dre.brewery;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.entity.Player;
import org.bukkit.entity.Entity;
@ -17,7 +19,7 @@ import org.bukkit.potion.PotionEffectType;
import org.bukkit.configuration.ConfigurationSection;
public class BPlayer {
public static Map<String, BPlayer> players = new HashMap<String, BPlayer>();// Players name and BPlayer
private static Map<String, BPlayer> players = new HashMap<String, BPlayer>();// Players name/uuid and BPlayer
private static Map<Player, Integer> pTasks = new HashMap<Player, Integer>();// Player and count
private static int taskId;
@ -50,31 +52,81 @@ public class BPlayer {
players.put(name, this);
}
public static BPlayer get(String name) {
public static BPlayer get(Player player) {
if (!players.isEmpty()) {
if (players.containsKey(name)) {
return players.get(name);
}
return players.get(P.playerString(player));
}
return null;
}
/*public String getPlayerName() {
for (Map.Entry<String,BPlayer> entry : players.entrySet()) {
// This method may be slow and should not be used if not needed
public static BPlayer getByName(String playerName) {
if (P.useUUID) {
for (Map.Entry<String, BPlayer> entry : players.entrySet()) {
OfflinePlayer p = P.p.getServer().getOfflinePlayer(UUID.fromString(entry.getKey()));
if (p != null) {
String name = p.getName();
if (name != null) {
if (name.equalsIgnoreCase(playerName)) {
return entry.getValue();
}
}
}
}
return null;
}
return players.get(playerName);
}
// This method may be slow and should not be used if not needed
public static boolean hasPlayerbyName(String playerName) {
if (P.useUUID) {
for (Map.Entry<String, BPlayer> entry : players.entrySet()) {
OfflinePlayer p = P.p.getServer().getOfflinePlayer(UUID.fromString(entry.getKey()));
if (p != null) {
String name = p.getName();
if (name != null) {
if (name.equalsIgnoreCase(playerName)) {
return true;
}
}
}
}
return false;
}
return players.containsKey(playerName);
}
public static boolean isEmpty() {
return players.isEmpty();
}
public static boolean hasPlayer(Player player) {
return players.containsKey(P.playerString(player));
}
// Create a new BPlayer and add it to the list
public static BPlayer addPlayer(Player player) {
BPlayer bPlayer = new BPlayer();
players.put(P.playerString(player), bPlayer);
return bPlayer;
}
public static void remove(Player player) {
players.remove(P.playerString(player));
}
public void remove() {
for (Map.Entry<String, BPlayer> entry : players.entrySet()) {
if (entry.getValue() == this) {
return entry.getKey();
players.remove(entry.getKey());
return;
}
}
return null;
}
public Player getPlayer() {
return org.bukkit.Bukkit.getPlayer(getPlayerName());
}*/
// returns the Player if online
public static Player getPlayer(String name) {
return org.bukkit.Bukkit.getPlayerExact(name);
public static void clear() {
players.clear();
}
// Drink a brew and apply effects, etc.
@ -85,10 +137,9 @@ public class BPlayer {
addBrewEffects(brew, player);
return;
}
BPlayer bPlayer = get(player.getName());
BPlayer bPlayer = get(player);
if (bPlayer == null) {
bPlayer = new BPlayer();
players.put(player.getName(), bPlayer);
bPlayer = addPlayer(player);
}
bPlayer.drunkeness += brewAlc;
if (brew.getQuality() > 0) {
@ -121,29 +172,29 @@ public class BPlayer {
// push the player around if he moves
public static void playerMove(PlayerMoveEvent event) {
BPlayer bPlayer = get(event.getPlayer().getName());
BPlayer bPlayer = get(event.getPlayer());
if (bPlayer != null) {
bPlayer.move(event);
}
}
// Eat something to drain the drunkeness
public void drainByItem(String name, Material mat) {
public void drainByItem(Player player, Material mat) {
int strength = drainItems.get(mat);
if (drain(name, strength)) {
players.remove(name);
if (drain(player, strength)) {
remove(player);
}
}
// drain the drunkeness by amount, returns true when player has to be removed
public boolean drain(String name, int amount) {
public boolean drain(Player player, int amount) {
if (drunkeness > 0) {
quality -= getQuality() * amount;
}
drunkeness -= amount;
if (drunkeness > 0) {
if (offlineDrunk == 0) {
if (getPlayer(name) == null) {
if (player == null) {
offlineDrunk = drunkeness;
}
}
@ -154,7 +205,7 @@ public class BPlayer {
quality = getQuality();
if (drunkeness <= -offlineDrunk) {
if (drunkeness <= -hangoverTime) {
return true;
return true;
}
}
}
@ -263,7 +314,7 @@ public class BPlayer {
}
hangoverEffects(player);
// wird der spieler noch gebraucht?
players.remove(player.getName());
players.remove(P.playerString(player));
} else if (offlineDrunk - drunkeness >= 30) {
Location randomLoc = Wakeup.getRandom(player.getLocation());
@ -448,7 +499,7 @@ public class BPlayer {
if (bplayer.drunkeness > 30) {
if (bplayer.offlineDrunk == 0) {
Player player = getPlayer(name);
Player player = P.getPlayerfromString(name);
if (player != null) {
bplayer.drunkEffects(player);
@ -476,7 +527,7 @@ public class BPlayer {
// Prevent 0 drunkeness
soberPerMin++;
}
if (bplayer.drain(name, soberPerMin)) {
if (bplayer.drain(P.getPlayerfromString(name), soberPerMin)) {
iter.remove();
}
}
@ -485,14 +536,15 @@ public class BPlayer {
// save all data
public static void save(ConfigurationSection config) {
for (String name : players.keySet()) {
ConfigurationSection section = config.createSection(name);
section.set("quality", players.get(name).quality);
section.set("drunk", players.get(name).drunkeness);
if (players.get(name).offlineDrunk != 0) {
section.set("offDrunk", players.get(name).offlineDrunk);
for (Map.Entry<String, BPlayer> entry : players.entrySet()) {
ConfigurationSection section = config.createSection(entry.getKey());
BPlayer bPlayer = entry.getValue();
section.set("quality", bPlayer.quality);
section.set("drunk", bPlayer.drunkeness);
if (bPlayer.offlineDrunk != 0) {
section.set("offDrunk", bPlayer.offlineDrunk);
}
if (players.get(name).passedOut) {
if (bPlayer.passedOut) {
section.set("passedOut", true);
}
}

View File

@ -7,6 +7,7 @@ import java.util.ListIterator;
import java.util.HashMap;
import java.io.IOException;
import java.io.File;
import java.util.UUID;
import java.util.logging.Level;
import org.bukkit.entity.Player;
@ -35,6 +36,7 @@ public class P extends JavaPlugin {
public static int lastBackup = 0;
public static int lastSave = 1;
public static int autosave = 3;
public static boolean useUUID;
// Third Party Enabled
public boolean useWG; //WorldGuard
@ -57,6 +59,11 @@ public class P extends JavaPlugin {
public void onEnable() {
p = this;
// Version check
String v = Bukkit.getBukkitVersion();
useUUID = !v.matches(".*1\\.[1-6].*") && !v.matches(".*1\\.7\\.[0-5].*");
P.p.log("Bukkit Version: " + v + "uuid: " + useUUID);
readConfig();
readData();
@ -105,7 +112,7 @@ public class P extends JavaPlugin {
BIngredients.possibleIngredients.clear();
BIngredients.recipes.clear();
BIngredients.cookedNames.clear();
BPlayer.players.clear();
BPlayer.clear();
Brew.potions.clear();
Wakeup.wakeups.clear();
Words.words.clear();
@ -319,6 +326,17 @@ public class P extends JavaPlugin {
if (section != null) {
// keys have players name
for (String name : section.getKeys(false)) {
try {
UUID.fromString(name);
if (!useUUID) {
continue;
}
} catch (IllegalArgumentException e) {
if (useUUID) {
continue;
}
}
int quality = section.getInt(name + ".quality");
int drunk = section.getInt(name + ".drunk");
int offDrunk = section.getInt(name + ".offDrunk", 0);
@ -493,7 +511,7 @@ public class P extends JavaPlugin {
Barrel.save(configFile.createSection("Barrel"), oldData.getConfigurationSection("Barrel"));
}
if (!BPlayer.players.isEmpty()) {
if (!BPlayer.isEmpty()) {
BPlayer.save(configFile.createSection("Player"));
}
@ -651,6 +669,27 @@ public class P extends JavaPlugin {
return msg;
}
// Returns either uuid or Name of player, depending on bukkit version
public static String playerString(Player player) {
if (useUUID) {
return player.getUniqueId().toString();
} else {
return player.getName();
}
}
// returns the Player if online
public static Player getPlayerfromString(String name) {
if (useUUID) {
try {
return Bukkit.getPlayer(UUID.fromString(name));
} catch (IllegalArgumentException e) {
return Bukkit.getPlayerExact(name);
}
}
return Bukkit.getPlayerExact(name);
}
// Runnables
public class DrunkRunnable implements Runnable {
@ -660,7 +699,7 @@ public class P extends JavaPlugin {
@Override
public void run() {
if (!BPlayer.players.isEmpty()) {
if (!BPlayer.isEmpty()) {
BPlayer.drunkeness();
}
}

View File

@ -76,10 +76,10 @@ public class Words {
// Distort players words when he uses a command
public static void playerCommand(PlayerCommandPreprocessEvent event) {
String name = event.getPlayer().getName();
BPlayer bPlayer = BPlayer.get(name);
BPlayer bPlayer = BPlayer.get(event.getPlayer());
if (bPlayer != null) {
if (!commands.isEmpty() && loadWords()) {
String name = event.getPlayer().getName();
if (!waitPlayers.containsKey(name) || waitPlayers.get(name) + 500 < System.currentTimeMillis()) {
String chat = event.getMessage();
for (String command : commands) {
@ -106,7 +106,7 @@ public class Words {
// Distort players words when he uses a command
public static void signWrite(SignChangeEvent event) {
BPlayer bPlayer = BPlayer.get(event.getPlayer().getName());
BPlayer bPlayer = BPlayer.get(event.getPlayer());
if (bPlayer != null) {
if (loadWords()) {
int index = 0;
@ -127,7 +127,7 @@ public class Words {
// Distort players words when he talks
public static void playerChat(AsyncPlayerChatEvent event) {
BPlayer bPlayer = BPlayer.get(event.getPlayer().getName());
BPlayer bPlayer = BPlayer.get(event.getPlayer());
if (bPlayer != null) {
if (loadWords()) {
String message = event.getMessage();

View File

@ -37,7 +37,7 @@ public class BlockListener implements Listener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onSignChangeLow(SignChangeEvent event) {
if (Words.doSigns) {
if (BPlayer.players.containsKey(event.getPlayer().getName())) {
if (BPlayer.hasPlayer(event.getPlayer())) {
Words.signWrite(event);
}
}

View File

@ -78,7 +78,7 @@ public class CommandListener implements CommandExecutor {
p.msg(sender, p.languageReader.get("Error_NoPermissions"));
}
} else if (cmd.equalsIgnoreCase("delete") || cmd.equalsIgnoreCase("rm")) {
} else if (cmd.equalsIgnoreCase("delete") || cmd.equalsIgnoreCase("rm") || cmd.equalsIgnoreCase("remove")) {
if (sender.hasPermission("brewery.cmd.delete")) {
cmdDelete(sender);
@ -104,7 +104,7 @@ public class CommandListener implements CommandExecutor {
} else {
if (p.getServer().getPlayerExact(cmd) != null || BPlayer.players.containsKey(cmd)) {
if (p.getServer().getPlayerExact(cmd) != null || BPlayer.hasPlayerbyName(cmd)) {
if (args.length == 1) {
if (sender.hasPermission("brewery.cmd.infoOther")) {
@ -267,23 +267,30 @@ public class CommandListener implements CommandExecutor {
}
String playerName = args[0];
BPlayer bPlayer = BPlayer.get(playerName);
if (bPlayer == null) {
Player player = P.p.getServer().getPlayerExact(playerName);
BPlayer bPlayer;
if (player == null) {
bPlayer = BPlayer.getByName(playerName);
} else {
bPlayer = BPlayer.get(player);
}
if (bPlayer == null && player != null) {
if (drunkeness == 0) {
return;
}
bPlayer = new BPlayer();
BPlayer.players.put(playerName, bPlayer);
bPlayer = BPlayer.addPlayer(player);
}
if (bPlayer == null) {
return;
}
if (drunkeness == 0) {
BPlayer.players.remove(playerName);
bPlayer.remove();
} else {
bPlayer.setData(drunkeness, quality);
}
if (drunkeness > 100) {
Player player = p.getServer().getPlayer(playerName);
if (player != null) {
bPlayer.drinkCap(player);
} else {
@ -308,7 +315,13 @@ public class CommandListener implements CommandExecutor {
}
}
BPlayer bPlayer = BPlayer.get(playerName);
Player player = P.p.getServer().getPlayerExact(playerName);
BPlayer bPlayer;
if (player == null) {
bPlayer = BPlayer.getByName(playerName);
} else {
bPlayer = BPlayer.get(player);
}
if (bPlayer == null) {
p.msg(sender, p.languageReader.get("CMD_Info_NotDrunk", playerName));
} else {

View File

@ -163,9 +163,9 @@ public class PlayerListener implements Listener {
}
}
} else if (BPlayer.drainItems.containsKey(item.getType())) {
BPlayer bplayer = BPlayer.get(player.getName());
BPlayer bplayer = BPlayer.get(player);
if (bplayer != null) {
bplayer.drainByItem(player.getName(), item.getType());
bplayer.drainByItem(player, item.getType());
}
}
}
@ -174,13 +174,12 @@ public class PlayerListener implements Listener {
// Player has died! Decrease Drunkeness by 20
@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event) {
String playerName = event.getPlayer().getName();
BPlayer bPlayer = BPlayer.get(playerName);
BPlayer bPlayer = BPlayer.get(event.getPlayer());
if (bPlayer != null) {
if (bPlayer.getDrunkeness() > 20) {
bPlayer.setData(bPlayer.getDrunkeness() - 20, 0);
} else {
BPlayer.players.remove(playerName);
BPlayer.remove(event.getPlayer());
}
}
}
@ -188,7 +187,7 @@ public class PlayerListener implements Listener {
// player walks while drunk, push him around!
@EventHandler(priority = EventPriority.LOW)
public void onPlayerMove(PlayerMoveEvent event) {
if (BPlayer.players.containsKey(event.getPlayer().getName())) {
if (BPlayer.hasPlayer(event.getPlayer())) {
BPlayer.playerMove(event);
}
}
@ -196,17 +195,13 @@ public class PlayerListener implements Listener {
// player talks while drunk, but he cant speak very well
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerChat(AsyncPlayerChatEvent event) {
if (BPlayer.players.containsKey(event.getPlayer().getName())) {
Words.playerChat(event);
}
Words.playerChat(event);
}
// player commands while drunk, distort chat commands
@EventHandler(priority = EventPriority.LOWEST)
public void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
if (BPlayer.players.containsKey(event.getPlayer().getName())) {
Words.playerCommand(event);
}
Words.playerCommand(event);
}
// player joins while passed out
@ -214,7 +209,7 @@ public class PlayerListener implements Listener {
public void onPlayerLogin(PlayerLoginEvent event) {
if (event.getResult() == PlayerLoginEvent.Result.ALLOWED) {
final Player player = event.getPlayer();
BPlayer bplayer = BPlayer.get(player.getName());
BPlayer bplayer = BPlayer.get(player);
if (bplayer != null) {
if (player.hasPermission("brewery.bypass.logindeny")) {
if (bplayer.getDrunkeness() > 100) {
@ -239,7 +234,7 @@ public class PlayerListener implements Listener {
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
BPlayer bplayer = BPlayer.get(event.getPlayer().getName());
BPlayer bplayer = BPlayer.get(event.getPlayer());
if (bplayer != null) {
bplayer.disconnecting();
}
@ -247,7 +242,7 @@ public class PlayerListener implements Listener {
@EventHandler
public void onPlayerKick(PlayerKickEvent event) {
BPlayer bplayer = BPlayer.get(event.getPlayer().getName());
BPlayer bplayer = BPlayer.get(event.getPlayer());
if (bplayer != null) {
bplayer.disconnecting();
}