mirror of
https://github.com/exituser/Brewery.git
synced 2026-09-17 05:28:59 +00:00
Added Support for UUID
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user