Fixed rename method, player data and other stuff

This commit is contained in:
Norbi Peti 2017-01-21 23:01:58 +01:00
parent 28fcb2f8d8
commit 88f85d2b15
9 changed files with 119 additions and 74 deletions

View file

@ -6,6 +6,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayer;
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
@ -13,9 +14,10 @@ public class PlayerListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void OnPlayerJoin(PlayerJoinEvent event) { public void OnPlayerJoin(PlayerJoinEvent event) {
TBMCPlayer player = TBMCPlayer.loadPlayer(event.getPlayer()); TBMCPlayer player = TBMCPlayer.loadPlayer(event.getPlayer());
if (player == null) if (player == null) {
event.getPlayer().sendMessage("§c[TBMC] Failed to load player data! Please contact a mod."); TBMCCoreAPI.SendException("Error on player join!", new Exception("Player is null"));
else event.getPlayer().sendMessage("§c[TBMC] Failed to load player data! The error has been sent to the devs.");
} else
TBMCPlayer.joinPlayer(player); TBMCPlayer.joinPlayer(player);
} }

View file

@ -1,7 +1,5 @@
package buttondevteam.core; package buttondevteam.core;
import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor; import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag; import org.bukkit.boss.BarFlag;
@ -51,7 +49,7 @@ public class ScheduledRestartCommand extends TBMCCommandBase {
if (restartcounter % 200 == 0) if (restartcounter % 200 == 0)
Bukkit.broadcastMessage("§c-- The server is restarting in " + restartcounter / 20 + " seconds!"); Bukkit.broadcastMessage("§c-- The server is restarting in " + restartcounter / 20 + " seconds!");
restartbar.setProgress(restartcounter / (double) restarttime); restartbar.setProgress(restartcounter / (double) restarttime);
restartbar.setTitle("Server restart in " + restartcounter / 20f); restartbar.setTitle(String.format("Server restart in %f.2", restartcounter / 20f));
/* /*
* if (restartcounter % 20 == 0) System.out.println("Progress: " + restartbar.getProgress()); * if (restartcounter % 20 == 0) System.out.println("Progress: " + restartbar.getProgress());
*/ */

View file

@ -10,7 +10,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
public abstract class ChromaGamerBase implements AutoCloseable { public abstract class ChromaGamerBase implements AutoCloseable {
private static final String TBMC_PLAYERS_DIR = "TBMC/players/"; public static final String TBMC_PLAYERS_DIR = "TBMC/players/";
private static final HashMap<Class<?>, String> playerTypes = new HashMap<>(); private static final HashMap<Class<?>, String> playerTypes = new HashMap<>();
@ -57,8 +57,9 @@ public abstract class ChromaGamerBase implements AutoCloseable {
protected static <T extends ChromaGamerBase> T getUser(String fname, Class<T> cl) { protected static <T extends ChromaGamerBase> T getUser(String fname, Class<T> cl) {
try { try {
T obj = cl.newInstance(); T obj = cl.newInstance();
obj.plugindata = YamlConfiguration final File file = new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname + ".yml");
.loadConfiguration(new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname)); file.mkdirs();
obj.plugindata = YamlConfiguration.loadConfiguration(file);
return obj; return obj;
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while loading a " + cl.getSimpleName() + "!", e); TBMCCoreAPI.SendException("An error occured while loading a " + cl.getSimpleName() + "!", e);
@ -68,7 +69,7 @@ public abstract class ChromaGamerBase implements AutoCloseable {
@Override @Override
public void close() throws Exception { public void close() throws Exception {
plugindata.save(new File(TBMC_PLAYERS_DIR + getFolderForType(getClass()), getFileName())); plugindata.save(new File(TBMC_PLAYERS_DIR + getFolder(), getFileName() + ".yml"));
} }
/** /**
@ -79,11 +80,11 @@ public abstract class ChromaGamerBase implements AutoCloseable {
*/ */
public <T extends ChromaGamerBase> void connectWith(T user) { public <T extends ChromaGamerBase> void connectWith(T user) {
// Set the ID, go through all linked files and connect them as well // Set the ID, go through all linked files and connect them as well
plugindata.set(getFolderForType(user.getClass()) + "_id", user.plugindata.getString("id")); plugindata.set(user.getFolder() + "_id", user.plugindata.getString("id"));
final String ownFolder = getFolderForType(getClass()); final String ownFolder = user.getFolder();
user.plugindata.set(ownFolder + "_id", plugindata.getString("id")); user.plugindata.set(ownFolder + "_id", plugindata.getString("id"));
Consumer<YamlConfiguration> sync = sourcedata -> { Consumer<YamlConfiguration> sync = sourcedata -> {
final String sourcefolder = sourcedata == plugindata ? ownFolder : getFolderForType(user.getClass()); final String sourcefolder = sourcedata == plugindata ? ownFolder : user.getFolder();
final String id = sourcedata.getString("id"); final String id = sourcedata.getString("id");
for (Entry<Class<?>, String> entry : playerTypes.entrySet()) { // Set our ID in all files we can find, both from our connections and the new ones for (Entry<Class<?>, String> entry : playerTypes.entrySet()) { // Set our ID in all files we can find, both from our connections and the new ones
final String otherid = sourcedata.getString(entry.getValue() + "_id"); final String otherid = sourcedata.getString(entry.getValue() + "_id");
@ -121,4 +122,8 @@ public abstract class ChromaGamerBase implements AutoCloseable {
return null; return null;
return getUser(plugindata.getString(newfolder + "_id"), cl); return getUser(plugindata.getString(newfolder + "_id"), cl);
} }
public String getFolder() {
return getFolderForType(getClass());
}
} }

View file

@ -1,36 +0,0 @@
package buttondevteam.lib.player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* <p>
* This event gets called when a new player joins. After this event, the
* {@link TBMCPlayerSaveEvent} will be called.
* </p>
*
* @author Norbi
*
*/
public class TBMCPlayerAddEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private TBMCPlayer player;
public TBMCPlayerAddEvent(TBMCPlayer player) {
this.player = player;
}
public TBMCPlayer GetPlayer() {
return player;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View file

@ -3,6 +3,16 @@ package buttondevteam.lib.player;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import buttondevteam.lib.TBMCCoreAPI;
public abstract class TBMCPlayerBase extends ChromaGamerBase { public abstract class TBMCPlayerBase extends ChromaGamerBase {
private static final String FOLDER_NAME = "minecraft"; private static final String FOLDER_NAME = "minecraft";
protected UUID uuid; protected UUID uuid;
@ -11,6 +21,14 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
return uuid; return uuid;
} }
public String getPlayerName() {
return plugindata.getString("playername", "");
}
public void setPlayerName(String value) {
plugindata.set("playername", value);
}
public abstract String getPluginName(); public abstract String getPluginName();
@Override @Override
@ -29,6 +47,15 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
addPlayerType(TBMCPlayerBase.class, FOLDER_NAME); addPlayerType(TBMCPlayerBase.class, FOLDER_NAME);
} }
/**
* Get player as a plugin player
*
* @param uuid
* The UUID of the player to get
* @param cl
* The type of the player
* @return The requested player object
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T extends TBMCPlayerBase> T getPlayer(UUID uuid, Class<T> cl) { public static <T extends TBMCPlayerBase> T getPlayer(UUID uuid, Class<T> cl) {
if (playermap.containsKey(uuid + "-" + cl.getSimpleName())) if (playermap.containsKey(uuid + "-" + cl.getSimpleName()))
@ -54,4 +81,66 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
public <T extends TBMCPlayerBase> T asPluginPlayer(Class<T> cl) { public <T extends TBMCPlayerBase> T asPluginPlayer(Class<T> cl) {
return getPlayer(uuid, cl); return getPlayer(uuid, cl);
} }
/**
* Only intended to use from ButtonCore
*/
public static <T extends TBMCPlayerBase> T loadPlayer(OfflinePlayer p, Class<T> cl) {
T player = getPlayer(p.getUniqueId(), cl);
Bukkit.getLogger().info("Loaded player: " + player.getPlayerName());
if (player.getPlayerName() == null) {
player.setPlayerName(p.getName());
Bukkit.getLogger().info("Player name saved: " + player.getPlayerName());
} else if (!p.getName().equals(player.getPlayerName())) {
Bukkit.getLogger().info("Renaming " + player.getPlayerName() + " to " + p.getName());
TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse();
Resident resident = tu.getResidentMap().get(player.getPlayerName());
if (resident == null)
Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny.");
else if (tu.getResidentMap().contains(p.getName()))
Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle
else
try {
TownyUniverse.getDataSource().renamePlayer(resident, p.getName());
} catch (AlreadyRegisteredException e) {
TBMCCoreAPI.SendException("Failed to rename resident, there's already one with this name.", e);
} catch (NotRegisteredException e) {
TBMCCoreAPI.SendException("Failed to rename resident, the resident isn't registered.", e);
}
player.setPlayerName(p.getName());
Bukkit.getLogger().info("Renaming done.");
}
// Load in other plugins
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(player));
return 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) {
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player));
playermap.remove(player.uuid + "-" + player.getFolder());
}
} }

View file

@ -6,13 +6,13 @@ import org.bukkit.event.HandlerList;
public class TBMCPlayerJoinEvent extends Event { public class TBMCPlayerJoinEvent extends Event {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private TBMCPlayer player; private TBMCPlayerBase player;
public TBMCPlayerJoinEvent(TBMCPlayer player) { public TBMCPlayerJoinEvent(TBMCPlayerBase player) {
this.player = player; this.player = player;
} }
public TBMCPlayer GetPlayer() { public TBMCPlayerBase GetPlayer() {
return player; return player;
} }

View file

@ -1,25 +1,18 @@
package buttondevteam.lib.player; package buttondevteam.lib.player;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
public class TBMCPlayerLoadEvent extends Event { public class TBMCPlayerLoadEvent extends Event {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private YamlConfiguration yaml; private TBMCPlayerBase player;
private TBMCPlayer player;
public TBMCPlayerLoadEvent(YamlConfiguration yaml, TBMCPlayer player) { public TBMCPlayerLoadEvent(TBMCPlayerBase player) {
this.yaml = yaml;
this.player = player; this.player = player;
} }
public YamlConfiguration GetPlayerConfig() { public TBMCPlayerBase GetPlayer() {
return yaml;
}
public TBMCPlayer GetPlayer() {
return player; return player;
} }

View file

@ -6,13 +6,13 @@ import org.bukkit.event.HandlerList;
public class TBMCPlayerQuitEvent extends Event { public class TBMCPlayerQuitEvent extends Event {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private TBMCPlayer player; private TBMCPlayerBase player;
public TBMCPlayerQuitEvent(TBMCPlayer player) { public TBMCPlayerQuitEvent(TBMCPlayerBase player) {
this.player = player; this.player = player;
} }
public TBMCPlayer GetPlayer() { public TBMCPlayerBase GetPlayer() {
return player; return player;
} }

View file

@ -7,19 +7,13 @@ import org.bukkit.event.HandlerList;
public class TBMCPlayerSaveEvent extends Event { public class TBMCPlayerSaveEvent extends Event {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private YamlConfiguration yaml; private TBMCPlayerBase player;
private TBMCPlayer player;
public TBMCPlayerSaveEvent(YamlConfiguration yaml, TBMCPlayer player) { public TBMCPlayerSaveEvent(TBMCPlayerBase player) {
this.yaml = yaml;
this.player = player; this.player = player;
} }
public YamlConfiguration GetPlayerConfig() { public TBMCPlayerBase GetPlayer() {
return yaml;
}
public TBMCPlayer GetPlayer() {
return player; return player;
} }