Added some code and some TODOs
This commit is contained in:
parent
5e4128afe7
commit
5f62df97de
5 changed files with 78 additions and 39 deletions
|
@ -7,11 +7,10 @@ import org.bukkit.event.player.PlayerJoinEvent;
|
|||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.player.TBMCPlayer;
|
||||
|
||||
public class PlayerListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
/*@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void OnPlayerJoin(PlayerJoinEvent event) {
|
||||
TBMCPlayer player = TBMCPlayer.loadPlayer(event.getPlayer());
|
||||
if (player == null) {
|
||||
|
@ -26,5 +25,5 @@ public class PlayerListener implements Listener {
|
|||
TBMCPlayer player = TBMCPlayer.getPlayer(event.getPlayer());
|
||||
TBMCPlayer.savePlayer(player);
|
||||
TBMCPlayer.quitPlayer(player);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
|
|
@ -41,7 +41,8 @@ public abstract class ChromaGamerBase implements AutoCloseable {
|
|||
}
|
||||
|
||||
/**
|
||||
* This method returns the filename for this player data. For example, for Minecraft-related data, use MC UUIDs, for Discord data, use Discord IDs, etc.
|
||||
* This method returns the filename for this player data. For example, for Minecraft-related data, use MC UUIDs, for Discord data, use Discord IDs, etc.<br>
|
||||
* <b>Does not include .yml</b>
|
||||
*/
|
||||
public abstract String getFileName();
|
||||
|
||||
|
|
12
src/main/java/buttondevteam/lib/player/PlayerClass.java
Normal file
12
src/main/java/buttondevteam/lib/player/PlayerClass.java
Normal file
|
@ -0,0 +1,12 @@
|
|||
package buttondevteam.lib.player;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface PlayerClass {
|
||||
String pluginname();
|
||||
}
|
6
src/main/java/buttondevteam/lib/player/TBMCPlayer.java
Normal file
6
src/main/java/buttondevteam/lib/player/TBMCPlayer.java
Normal file
|
@ -0,0 +1,6 @@
|
|||
package buttondevteam.lib.player;
|
||||
|
||||
@PlayerClass(pluginname = "ButtonCore")
|
||||
public class TBMCPlayer extends TBMCPlayerBase {
|
||||
|
||||
}
|
|
@ -1,10 +1,16 @@
|
|||
package buttondevteam.lib.player;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.palmergames.bukkit.towny.Towny;
|
||||
import com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
|
@ -29,8 +35,6 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
plugindata.set("playername", value);
|
||||
}
|
||||
|
||||
public abstract String getPluginName();
|
||||
|
||||
@Override
|
||||
public String getFileName() {
|
||||
return getUUID().toString();
|
||||
|
@ -60,9 +64,21 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
public static <T extends TBMCPlayerBase> T getPlayer(UUID uuid, Class<T> cl) {
|
||||
if (playermap.containsKey(uuid + "-" + cl.getSimpleName()))
|
||||
return (T) playermap.get(uuid + "-" + cl.getSimpleName());
|
||||
T obj = ChromaGamerBase.getUser(uuid.toString(), cl);
|
||||
obj.uuid = uuid;
|
||||
return obj;
|
||||
try {
|
||||
T player;
|
||||
if (playerfiles.containsKey(uuid)) {
|
||||
player = cl.newInstance();
|
||||
player.plugindata = playerfiles.get(uuid);
|
||||
playermap.put(player.uuid + "-" + player.getFolder(), player); // It will get removed on player quit
|
||||
} else
|
||||
player = ChromaGamerBase.getUser(uuid.toString(), cl);
|
||||
player.uuid = uuid;
|
||||
return player;
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException(
|
||||
"Failed to get player with UUID " + uuid + " and class " + cl.getSimpleName() + "!", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -70,6 +86,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
*/
|
||||
static final ConcurrentHashMap<String, TBMCPlayerBase> playermap = new ConcurrentHashMap<>();
|
||||
|
||||
private static final ConcurrentHashMap<UUID, YamlConfiguration> playerfiles = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Gets the TBMCPlayer object as a specific plugin player, keeping it's data<br>
|
||||
* Make sure to use try-with-resources with this to save the data, as it may need to load the file
|
||||
|
@ -84,9 +102,25 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
/**
|
||||
* Only intended to use from ButtonCore
|
||||
*/
|
||||
public static <T extends TBMCPlayerBase> T loadPlayer(OfflinePlayer p, Class<T> cl) { // TODO: Load player files and get player classes backed by the YAML
|
||||
T player = getPlayer(p.getUniqueId(), cl);
|
||||
Bukkit.getLogger().info("Loaded player: " + player.getPlayerName());
|
||||
public static void savePlayer(TBMCPlayerBase player) {
|
||||
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(player));
|
||||
try {
|
||||
player.close();
|
||||
} catch (Exception e) {
|
||||
new Exception("Failed to save player data for " + player.getPlayerName(), e).printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Only intended to use from ButtonCore
|
||||
*/
|
||||
public static void joinPlayer(UUID uuid) {
|
||||
YamlConfiguration yc;
|
||||
if (playerfiles.containsKey(uuid))
|
||||
yc = playerfiles.get(uuid);
|
||||
else
|
||||
playerfiles.put(uuid, yc = YamlConfiguration.loadConfiguration(new File("minecraft", uuid + ".yml")));
|
||||
/*Bukkit.getLogger().info("Loaded player: " + player.getPlayerName());
|
||||
if (player.getPlayerName() == null) {
|
||||
player.setPlayerName(p.getName());
|
||||
Bukkit.getLogger().info("Player name saved: " + player.getPlayerName());
|
||||
|
@ -112,40 +146,27 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
|
||||
// Load in other plugins
|
||||
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(player));
|
||||
return player;
|
||||
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player));*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Only intended to use from ButtonCore
|
||||
*/
|
||||
public static void savePlayer(TBMCPlayerBase player) {
|
||||
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(player));
|
||||
try {
|
||||
player.close();
|
||||
} catch (Exception e) {
|
||||
new Exception("Failed to save player data for " + player.getPlayerName(), e).printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Only intended to use from ButtonCore
|
||||
*/
|
||||
public static void joinPlayer(TBMCPlayerBase player) {
|
||||
playermap.put(player.uuid + "-" + player.getFolder(), player);
|
||||
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player));
|
||||
}
|
||||
|
||||
/**
|
||||
* Only intended to use from ButtonCore
|
||||
*/
|
||||
public static void quitPlayer(TBMCPlayerBase player) {
|
||||
public static void quitPlayer(Player p) {
|
||||
Iterator<Entry<String, TBMCPlayerBase>> it = playermap.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Entry<String, TBMCPlayerBase> entry = it.next();
|
||||
if (entry.getKey().startsWith(p.getUniqueId().toString())) { // Save every player data
|
||||
TBMCPlayerBase player = entry.getValue(); // TODO: Separate plugin data by plugin name (annotations?)
|
||||
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player));
|
||||
playermap.remove(player.uuid + "-" + player.getFolder());
|
||||
try {
|
||||
player.close();
|
||||
}
|
||||
}
|
||||
final YamlConfiguration playerfile = playerfiles.get(p.getUniqueId());
|
||||
try { // Only save to file once, not for each plugin
|
||||
playerfile.save(p.getUniqueId().toString() + ".yml"); // TODO: Bring this together with the close() method, like a common method or something
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException("Error while saving quitting player " + player.getPlayerName() + " ("
|
||||
+ player.getFolder() + "/" + player.getFileName() + ")!", e);
|
||||
TBMCCoreAPI.SendException("Error while saving quitting player " + playerfile.getString("playername") + " ("
|
||||
+ "minecraft/" + p.getUniqueId() + ".yml)!", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue