From a31c353de6cad3c285925b02547802cd9fb1db41 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 24 Oct 2016 19:12:03 +0200 Subject: [PATCH] Moved things over to ButtonLib --- .../java/buttondevteam/core/TBMCCoreAPI.java | 94 ----------- .../java/buttondevteam/core/TBMCPlayer.java | 150 ------------------ .../core/TBMCPlayerAddEvent.java | 36 ----- .../core/TBMCPlayerJoinEvent.java | 27 ---- .../core/TBMCPlayerLoadEvent.java | 34 ---- .../core/TBMCPlayerQuitEvent.java | 27 ---- .../core/TBMCPlayerSaveEvent.java | 34 ---- 7 files changed, 402 deletions(-) delete mode 100644 src/main/java/buttondevteam/core/TBMCCoreAPI.java delete mode 100644 src/main/java/buttondevteam/core/TBMCPlayer.java delete mode 100644 src/main/java/buttondevteam/core/TBMCPlayerAddEvent.java delete mode 100644 src/main/java/buttondevteam/core/TBMCPlayerJoinEvent.java delete mode 100644 src/main/java/buttondevteam/core/TBMCPlayerLoadEvent.java delete mode 100644 src/main/java/buttondevteam/core/TBMCPlayerQuitEvent.java delete mode 100644 src/main/java/buttondevteam/core/TBMCPlayerSaveEvent.java diff --git a/src/main/java/buttondevteam/core/TBMCCoreAPI.java b/src/main/java/buttondevteam/core/TBMCCoreAPI.java deleted file mode 100644 index 5794016..0000000 --- a/src/main/java/buttondevteam/core/TBMCCoreAPI.java +++ /dev/null @@ -1,94 +0,0 @@ -package buttondevteam.core; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - -import buttondevteam.core.MainPlugin; - -public final class TBMCCoreAPI { - /** - * Updates or installs the specified plugin. The plugin must use Maven. - * - * @param name - * The plugin's repository name. - * @return Error message or empty string - */ - public static String UpdatePlugin(String name) { - List plugins = GetPluginNames(); - String correctname = null; - for (String plugin : plugins) { - if (plugin.equalsIgnoreCase(name)) { - correctname = plugin; // Fixes capitalization - break; - } - } - if (correctname == null) { - MainPlugin.Instance.getLogger().warning("There was an error while updating TBMC plugin: " + name); - return "Can't find plugin: " + name; - } - MainPlugin.Instance.getLogger().info("Updating TBMC plugin: " + correctname); - String ret = ""; - URL url; - try { - url = new URL("https://jitpack.io/com/github/TBMCPlugins/" - + (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/master-SNAPSHOT/" - + correctname + "-master-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well - FileUtils.copyURLToFile(url, new File("plugins/" + correctname + ".jar")); - } catch (FileNotFoundException e) { - ret = "Can't find JAR, the build probably failed. Build log (scroll to bottom):\nhttps://jitpack.io/com/github/TBMCPlugins/" - + correctname + "/master-SNAPSHOT/build.log"; - } catch (IOException e) { - ret = "IO error!\n" + e.getMessage(); - } catch (Exception e) { - MainPlugin.Instance.getLogger().warning("Error!\n" + e); - ret = e.toString(); - } - return ret; - } - - /** - * Retrieves all the repository names from the GitHub organization. - * - * @return A list of names - */ - public static List GetPluginNames() { - List ret = new ArrayList<>(); - try { - String resp = DownloadString("https://api.github.com/orgs/TBMCPlugins/repos"); - JsonArray arr = new JsonParser().parse(resp).getAsJsonArray(); - for (JsonElement obj : arr) { - JsonObject jobj = obj.getAsJsonObject(); - ret.add(jobj.get("name").getAsString()); - } - } catch (Exception e) { - e.printStackTrace(); - } - return ret; - } - - public static String DownloadString(String urlstr) throws MalformedURLException, IOException { - URL url = new URL(urlstr); - URLConnection con = url.openConnection(); - con.setRequestProperty("User-Agent", "TBMCPlugins"); - InputStream in = con.getInputStream(); - String encoding = con.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - in.close(); - return body; - } -} diff --git a/src/main/java/buttondevteam/core/TBMCPlayer.java b/src/main/java/buttondevteam/core/TBMCPlayer.java deleted file mode 100644 index 5bfe230..0000000 --- a/src/main/java/buttondevteam/core/TBMCPlayer.java +++ /dev/null @@ -1,150 +0,0 @@ -package buttondevteam.core; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.configuration.file.YamlConfiguration; -import com.palmergames.bukkit.towny.Towny; -import com.palmergames.bukkit.towny.object.Resident; -import com.palmergames.bukkit.towny.object.TownyUniverse; - -/** - *

- * The class for holding data common to all TBMC plugins - *

- *

- * Listen to the load and save events from this package to load and save plugin-specific data - *

- * - * @author Norbi - * - */ -public class TBMCPlayer { - private static final String TBMC_PLAYERS_DIR = "TBMC/players"; - - public String PlayerName; - - public UUID UUID; - - public T AsPluginPlayer(Class cl) { - T obj = null; - try { - obj = cl.newInstance(); - obj.UUID = UUID; - obj.PlayerName = PlayerName; - } catch (Exception e) { - e.printStackTrace(); - } - return obj; - } - - public static HashMap OnlinePlayers = new HashMap<>(); - - /** - * @param name - * The player's name - * @return The {@link TBMCPlayer} object for the player - */ - public static TBMCPlayer GetFromName(String name) { - @SuppressWarnings("deprecation") - OfflinePlayer p = Bukkit.getOfflinePlayer(name); - if (p != null) - return GetPlayer(p); - else - return null; - } - - /** - * @param p - * The Player object - * @return The {@link TBMCPlayer} object for the player - */ - public static TBMCPlayer GetPlayer(OfflinePlayer p) { - if (TBMCPlayer.OnlinePlayers.containsKey(p.getUniqueId())) - return TBMCPlayer.OnlinePlayers.get(p.getUniqueId()); - else - return TBMCPlayer.LoadPlayer(p); - } - - protected static TBMCPlayer LoadPlayer(OfflinePlayer 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_DIR, p.getUniqueId().toString() + ".yml"); - if (!file.exists()) - 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 = p.getUniqueId(); - player.PlayerName = yc.getString("playername"); - System.out.println("Player name: " + player.PlayerName); - if (player.PlayerName == null) { - player.PlayerName = p.getName(); - System.out.println("Player name saved: " + player.PlayerName); - } else if (!p.getName().equals(player.PlayerName)) { - System.out.println("Renaming " + player.PlayerName + " to " + p.getName()); - TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse(); - Resident resident = tu.getResidentMap().get(player.PlayerName); - if (resident == null) - System.out.println("Resident not found - couldn't rename in Towny."); - else if (tu.getResidentMap().contains(p.getName())) - System.out.println("Target resident name is already in use."); // TODO: Handle - else - resident.setName(p.getName()); - player.PlayerName = p.getName(); - System.out.println("Renaming done."); - } - - // Load in other plugins - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(yc, player)); - return player; - } - } - - static TBMCPlayer AddPlayer(OfflinePlayer p) { - TBMCPlayer player = new TBMCPlayer(); - player.UUID = p.getUniqueId(); - player.PlayerName = p.getName(); - OnlinePlayers.put(p.getUniqueId(), player); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerAddEvent(player)); - SavePlayer(player); - return player; - } - - static void SavePlayer(TBMCPlayer player) { - YamlConfiguration yc = new YamlConfiguration(); - yc.set("playername", player.PlayerName); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(yc, player)); - try { - yc.save(TBMC_PLAYERS_DIR + "/" + player.UUID + ".yml"); - } catch (IOException e) { - new Exception("Failed to save player data for " + player.PlayerName, e).printStackTrace(); - } - } - - static void JoinPlayer(TBMCPlayer player) { - OnlinePlayers.put(player.UUID, player); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player)); - } - - static void QuitPlayer(TBMCPlayer player) { - OnlinePlayers.remove(player.UUID); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); - } - - T GetAs(Class cl) { // TODO: Serialize player classes - return null; - } -} diff --git a/src/main/java/buttondevteam/core/TBMCPlayerAddEvent.java b/src/main/java/buttondevteam/core/TBMCPlayerAddEvent.java deleted file mode 100644 index 7760384..0000000 --- a/src/main/java/buttondevteam/core/TBMCPlayerAddEvent.java +++ /dev/null @@ -1,36 +0,0 @@ -package buttondevteam.core; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -/** - *

- * This event gets called when a new player joins. After this event, the - * {@link TBMCPlayerSaveEvent} will be called. - *

- * - * @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; - } -} diff --git a/src/main/java/buttondevteam/core/TBMCPlayerJoinEvent.java b/src/main/java/buttondevteam/core/TBMCPlayerJoinEvent.java deleted file mode 100644 index 1427bb4..0000000 --- a/src/main/java/buttondevteam/core/TBMCPlayerJoinEvent.java +++ /dev/null @@ -1,27 +0,0 @@ -package buttondevteam.core; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class TBMCPlayerJoinEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private TBMCPlayer player; - - public TBMCPlayerJoinEvent(TBMCPlayer player) { - this.player = player; - } - - public TBMCPlayer GetPlayer() { - return player; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} diff --git a/src/main/java/buttondevteam/core/TBMCPlayerLoadEvent.java b/src/main/java/buttondevteam/core/TBMCPlayerLoadEvent.java deleted file mode 100644 index dea41b0..0000000 --- a/src/main/java/buttondevteam/core/TBMCPlayerLoadEvent.java +++ /dev/null @@ -1,34 +0,0 @@ -package buttondevteam.core; - -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class TBMCPlayerLoadEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private YamlConfiguration yaml; - private TBMCPlayer player; - - public TBMCPlayerLoadEvent(YamlConfiguration yaml, TBMCPlayer player) { - this.yaml = yaml; - this.player = player; - } - - public YamlConfiguration GetPlayerConfig() { - return yaml; - } - - public TBMCPlayer GetPlayer() { - return player; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} diff --git a/src/main/java/buttondevteam/core/TBMCPlayerQuitEvent.java b/src/main/java/buttondevteam/core/TBMCPlayerQuitEvent.java deleted file mode 100644 index 4b4bab8..0000000 --- a/src/main/java/buttondevteam/core/TBMCPlayerQuitEvent.java +++ /dev/null @@ -1,27 +0,0 @@ -package buttondevteam.core; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class TBMCPlayerQuitEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private TBMCPlayer player; - - public TBMCPlayerQuitEvent(TBMCPlayer player) { - this.player = player; - } - - public TBMCPlayer GetPlayer() { - return player; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} diff --git a/src/main/java/buttondevteam/core/TBMCPlayerSaveEvent.java b/src/main/java/buttondevteam/core/TBMCPlayerSaveEvent.java deleted file mode 100644 index 331de19..0000000 --- a/src/main/java/buttondevteam/core/TBMCPlayerSaveEvent.java +++ /dev/null @@ -1,34 +0,0 @@ -package buttondevteam.core; - -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class TBMCPlayerSaveEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private YamlConfiguration yaml; - private TBMCPlayer player; - - public TBMCPlayerSaveEvent(YamlConfiguration yaml, TBMCPlayer player) { - this.yaml = yaml; - this.player = player; - } - - public YamlConfiguration GetPlayerConfig() { - return yaml; - } - - public TBMCPlayer GetPlayer() { - return player; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -}