Moved things over to ButtonLib

This commit is contained in:
Norbi Peti 2016-10-24 19:12:03 +02:00
parent e892d60564
commit a31c353de6
7 changed files with 0 additions and 402 deletions

View file

@ -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<String> 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<String> GetPluginNames() {
List<String> 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;
}
}

View file

@ -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;
/**
* <p>
* The class for holding data common to all TBMC plugins
* </p>
* <p>
* Listen to the load and save events from this package to load and save plugin-specific data
* </p>
*
* @author Norbi
*
*/
public class TBMCPlayer {
private static final String TBMC_PLAYERS_DIR = "TBMC/players";
public String PlayerName;
public UUID UUID;
public <T extends TBMCPlayer> T AsPluginPlayer(Class<T> cl) {
T obj = null;
try {
obj = cl.newInstance();
obj.UUID = UUID;
obj.PlayerName = PlayerName;
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static HashMap<UUID, TBMCPlayer> 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 extends TBMCPlayer> T GetAs(Class<T> cl) { // TODO: Serialize player classes
return null;
}
}

View file

@ -1,36 +0,0 @@
package buttondevteam.core;
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

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}