Various fixes

This commit is contained in:
Norbi Peti 2016-07-17 02:27:19 +02:00
parent cc45238034
commit 0b9aab4c9c
5 changed files with 60 additions and 25 deletions

4
plugin.yml Normal file
View file

@ -0,0 +1,4 @@
name: ButtonPluginBucket
main: buttondevteam.MainPlugin
version: 1.0
author: TBMCPlugins

View file

@ -1,5 +1,8 @@
package buttondevteam.core.player;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
public class CoreSubPlugin {
@ -10,6 +13,7 @@ public class CoreSubPlugin {
}
public void register() {
System.out.println("Players subplugin registered!");
Bukkit.getPluginManager().registerEvents(new PlayerListener(), plugin);
plugin.getLogger().log(Level.INFO, "Core subplugin registered!");
}
}

View file

@ -1,17 +1,21 @@
package buttondevteam.core.player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
public class PlayerListener implements Listener {
@EventHandler
@EventHandler(priority = EventPriority.HIGHEST)
public void OnPlayerJoin(PlayerJoinEvent event) {
try {
TBMCPlayer.LoadPlayer(event.getPlayer().getUniqueId());
} catch (Exception e) {
e.printStackTrace();
if (TBMCPlayer.LoadPlayer(event.getPlayer()) == null)
event.getPlayer().sendMessage("§c[TBMC] Failed to load player data! Please contact a mod.");
}
@EventHandler(priority = EventPriority.HIGHEST)
public void OnPlayerLeave(PlayerQuitEvent event) {
TBMCPlayer.SavePlayer(TBMCPlayer.GetPlayer(event.getPlayer()));
}
}

View file

@ -9,6 +9,11 @@ import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.ResidentModes;
import com.palmergames.bukkit.towny.object.TownyUniverse;
/**
* <p>
* The class for holding data common to all TBMC plugins
@ -22,6 +27,8 @@ import org.bukkit.entity.Player;
*
*/
public class TBMCPlayer {
private static final String TBMC_PLAYERS_DIR = "TBMC/players";
public String PlayerName;
public UUID UUID;
@ -51,20 +58,31 @@ public class TBMCPlayer {
return TBMCPlayer.OnlinePlayers.get(p.getUniqueId());
}
static TBMCPlayer LoadPlayer(UUID uuid) throws Exception {
if (OnlinePlayers.containsKey(uuid))
return OnlinePlayers.get(uuid);
File file = new File("TBMC/players");
static TBMCPlayer LoadPlayer(Player p) {
if (OnlinePlayers.containsKey(p.getUniqueId()))
return OnlinePlayers.get(p.getUniqueId());
File file = new File(TBMC_PLAYERS_DIR);
file.mkdirs();
file = new File("TBMC/players", uuid.toString() + ".yml");
file = new File(TBMC_PLAYERS_DIR, p.getUniqueId().toString() + ".yml");
if (!file.exists())
return AddPlayer(uuid);
return AddPlayer(p);
else {
final YamlConfiguration yc = new YamlConfiguration();
try {
yc.load(file);
} catch (Exception e) {
new Exception("Failed to load player data for " + p.getUniqueId(), e).printStackTrace();
return null;
}
TBMCPlayer player = new TBMCPlayer();
player.UUID = uuid;
player.UUID = p.getUniqueId();
player.PlayerName = yc.getString("playername");
if (!p.getName().equals(player.PlayerName)) {
System.out.println("Renaming " + player.PlayerName + " to " + p.getName());
TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse();
tu.getResidentMap().get(player.PlayerName).setName(p.getName());
System.out.println("Renaming done.");
}
// Load in other plugins
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(yc, player));
@ -72,15 +90,11 @@ public class TBMCPlayer {
}
}
static TBMCPlayer AddPlayer(UUID uuid) {
if (OnlinePlayers.containsKey(uuid))
return OnlinePlayers.get(uuid);
static TBMCPlayer AddPlayer(Player p) {
TBMCPlayer player = new TBMCPlayer();
player.UUID = uuid;
Player p = Bukkit.getPlayer(uuid);
if (p != null)
player.UUID = p.getUniqueId();
player.PlayerName = p.getName();
OnlinePlayers.put(uuid, player);
OnlinePlayers.put(p.getUniqueId(), player);
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerAddEvent(player));
SavePlayer(player);
return player;
@ -91,7 +105,7 @@ public class TBMCPlayer {
yc.set("playername", player.PlayerName);
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(yc, player));
try {
yc.save("tbmcplayers/" + player.UUID + ".yml");
yc.save(TBMC_PLAYERS_DIR + "/" + player.UUID + ".yml");
} catch (IOException e) {
new Exception("Failed to save player data for " + player.PlayerName, e).printStackTrace();
}

View file

@ -3,13 +3,22 @@ package buttondevteam.core.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) {
//TODO: Convert player configs
// TODO: Convert player configs
this.player = player;
}