From 245f715b91e34f8c8b95120aa42cc3a6fa51d17e Mon Sep 17 00:00:00 2001 From: Flibio Date: Fri, 17 Jul 2015 13:20:20 -0600 Subject: [PATCH] v0.0.5 - Beta --- pom.xml | 9 +++- .../EconomyLite/BalanceChangeEvent.java | 29 +++++++++++ src/me/Flibio/EconomyLite/BalanceCommand.java | 5 +- .../EconomyLite/ConfigurationManager.java | 7 +++ src/me/Flibio/EconomyLite/DataEditor.java | 12 ++++- src/me/Flibio/EconomyLite/EconCommand.java | 31 +++++++++-- src/me/Flibio/EconomyLite/Main.java | 36 +++++++++---- src/me/Flibio/EconomyLite/MySQL.java | 6 ++- .../EconomyLite/PlayerBalanceChange.java | 51 +++++++++++++++++++ src/me/Flibio/EconomyLite/PlayerJoin.java | 26 +++++++++- 10 files changed, 189 insertions(+), 23 deletions(-) create mode 100644 src/me/Flibio/EconomyLite/BalanceChangeEvent.java create mode 100644 src/me/Flibio/EconomyLite/PlayerBalanceChange.java diff --git a/pom.xml b/pom.xml index 63ffbaa..ab62d44 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,12 @@ 4.0.0 EconomyLite EconomyLite - 0.0.1-SNAPSHOT + 0.0.5 + jar + EconomyLite + An economy plugin for Sponge + https://github.com/Flibio/EconomyLite + src @@ -34,7 +39,7 @@ org.spongepowered spongeapi - 2.0 + 2.1-SNAPSHOT provided diff --git a/src/me/Flibio/EconomyLite/BalanceChangeEvent.java b/src/me/Flibio/EconomyLite/BalanceChangeEvent.java new file mode 100644 index 0000000..871add7 --- /dev/null +++ b/src/me/Flibio/EconomyLite/BalanceChangeEvent.java @@ -0,0 +1,29 @@ +package me.Flibio.EconomyLite; + +import org.spongepowered.api.event.AbstractEvent; +import org.spongepowered.api.event.Cancellable; + +public class BalanceChangeEvent extends AbstractEvent implements Cancellable { + + private boolean cancelled = false; + private String player; + + public BalanceChangeEvent(String player){ + this.player = player; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + public String getPlayerName(){ + return player; + } + +} diff --git a/src/me/Flibio/EconomyLite/BalanceCommand.java b/src/me/Flibio/EconomyLite/BalanceCommand.java index 1ef5cd2..ef1c93e 100644 --- a/src/me/Flibio/EconomyLite/BalanceCommand.java +++ b/src/me/Flibio/EconomyLite/BalanceCommand.java @@ -4,6 +4,7 @@ import java.util.List; import org.slf4j.Logger; +import org.spongepowered.api.Game; import org.spongepowered.api.entity.player.Player; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.Texts; @@ -21,8 +22,8 @@ public class BalanceCommand implements CommandCallable { private String plural; private String singular; - public BalanceCommand(Logger log){ - dataEditor = new DataEditor(log); + public BalanceCommand(Logger log,Game game){ + dataEditor = new DataEditor(log,game); plural = Main.getCurrencyPlural(); singular = Main.getCurrencySingular(); diff --git a/src/me/Flibio/EconomyLite/ConfigurationManager.java b/src/me/Flibio/EconomyLite/ConfigurationManager.java index 447f500..fb8af8c 100644 --- a/src/me/Flibio/EconomyLite/ConfigurationManager.java +++ b/src/me/Flibio/EconomyLite/ConfigurationManager.java @@ -9,6 +9,8 @@ import org.slf4j.Logger; +import com.typesafe.config.ConfigException; + public class ConfigurationManager { @@ -27,6 +29,11 @@ protected ConfigurationNode getFile(){ logger.error("Error loading configuation file!"); logger.error(e.getMessage()); return null; + } catch (ConfigException e) { + logger.error("Error loading configuation file!"); + logger.error("To use a blank value in configuration, just use double quotes \"\" !"); + logger.error(e.getMessage()); + return null; } return root; } diff --git a/src/me/Flibio/EconomyLite/DataEditor.java b/src/me/Flibio/EconomyLite/DataEditor.java index 98217c2..658b817 100644 --- a/src/me/Flibio/EconomyLite/DataEditor.java +++ b/src/me/Flibio/EconomyLite/DataEditor.java @@ -10,22 +10,29 @@ import ninja.leaping.configurate.loader.ConfigurationLoader; import org.slf4j.Logger; +import org.spongepowered.api.Game; public class DataEditor { private Logger logger; private boolean mySQL; private MySQL sql; + private Game game; - public DataEditor(Logger log){ + public DataEditor(Logger log, Game game){ this.logger = log; mySQL = Main.sqlEnabled(); + this.game = game; } protected boolean setBalance(String name, int balance) { if(mySQL){ sql = Main.getSQL(); - return sql.setCurrency(name, balance); + boolean success = sql.setCurrency(name, balance); + if(success){ + game.getEventManager().post(new BalanceChangeEvent(name)); + } + return success; } else { ConfigurationLoader manager = HoconConfigurationLoader.builder().setFile(new File("config/EconomyLite/data.conf")).build(); ConfigurationNode root; @@ -56,6 +63,7 @@ protected boolean setBalance(String name, int balance) { logger.error(e.getMessage()); return false; } + game.getEventManager().post(new BalanceChangeEvent(name)); return true; } } diff --git a/src/me/Flibio/EconomyLite/EconCommand.java b/src/me/Flibio/EconomyLite/EconCommand.java index 5e8d2c1..1ec6ed3 100644 --- a/src/me/Flibio/EconomyLite/EconCommand.java +++ b/src/me/Flibio/EconomyLite/EconCommand.java @@ -4,6 +4,7 @@ import java.util.List; import org.slf4j.Logger; +import org.spongepowered.api.Game; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.Texts; import org.spongepowered.api.text.format.TextColors; @@ -20,8 +21,8 @@ public class EconCommand implements CommandCallable { private String plural; private String singular; - public EconCommand(Logger log){ - dataEditor = new DataEditor(log); + public EconCommand(Logger log, Game game){ + dataEditor = new DataEditor(log,game); plural = Main.getCurrencyPlural(); singular = Main.getCurrencySingular(); @@ -95,7 +96,14 @@ public Optional process(CommandSource source, String arg_string) if(amnt == 1){ currencyName = singular; } - source.sendMessage(Texts.builder("Successfully added "+amnt+" "+currencyName+" to "+name+"'s balance!").color(TextColors.GREEN).build()); + Text removeCompleteText = Texts.builder("EconomyLite » ").color(TextColors.GOLD).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder("Added ").color(TextColors.YELLOW).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(amnt+" ").color(TextColors.DARK_GREEN).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(currencyName).color(TextColors.GREEN).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(" to ").color(TextColors.YELLOW).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(name+"'s").color(TextColors.GREEN).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(" balance!").color(TextColors.YELLOW).build()).build(); + source.sendMessage(removeCompleteText); return Optional.of(CommandResult.success()); } else { source.sendMessage(Texts.builder("Error: Internal plugin error occured while changing the balance").color(TextColors.RED).build()); @@ -132,7 +140,14 @@ public Optional process(CommandSource source, String arg_string) if(amnt == 1){ currencyName = singular; } - source.sendMessage(Texts.builder("Successfully removed "+amnt+" "+currencyName+" from "+name+"!").color(TextColors.GREEN).build()); + Text removeCompleteText = Texts.builder("EconomyLite » ").color(TextColors.GOLD).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder("Removed ").color(TextColors.YELLOW).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(amnt+" ").color(TextColors.DARK_GREEN).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(currencyName).color(TextColors.GREEN).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(" from ").color(TextColors.YELLOW).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(name).color(TextColors.GREEN).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder("!").color(TextColors.YELLOW).build()).build(); + source.sendMessage(removeCompleteText); return Optional.of(CommandResult.success()); } else { source.sendMessage(Texts.builder("Error: Internal plugin error occured while changing the balance").color(TextColors.RED).build()); @@ -164,7 +179,13 @@ public Optional process(CommandSource source, String arg_string) } if(dataEditor.setBalance(name, amnt)){ - source.sendMessage(Texts.builder("Successfully set the balance of "+name+" to "+amnt+"!").color(TextColors.GREEN).build()); + Text removeCompleteText = Texts.builder("EconomyLite » ").color(TextColors.GOLD).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder("Set the balance of ").color(TextColors.YELLOW).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(name).color(TextColors.GREEN).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(" to ").color(TextColors.YELLOW).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder(amnt+"").color(TextColors.DARK_GREEN).build()).build(); + removeCompleteText = removeCompleteText.builder().append(Texts.builder("!").color(TextColors.YELLOW).build()).build(); + source.sendMessage(removeCompleteText); return Optional.of(CommandResult.success()); } else { source.sendMessage(Texts.builder("Error: Internal plugin error occured while changing the balance").color(TextColors.RED).build()); diff --git a/src/me/Flibio/EconomyLite/Main.java b/src/me/Flibio/EconomyLite/Main.java index c986575..9cad06d 100644 --- a/src/me/Flibio/EconomyLite/Main.java +++ b/src/me/Flibio/EconomyLite/Main.java @@ -2,7 +2,6 @@ import java.io.File; import java.io.IOException; - import ninja.leaping.configurate.ConfigurationNode; import org.slf4j.Logger; @@ -15,12 +14,12 @@ import com.google.common.base.Optional; import com.google.inject.Inject; -@Plugin(id = "EconomyLite", name = "EconomyLite", version = "0.0.4") +@Plugin(id = "EconomyLite", name = "EconomyLite", version = "0.0.5") public class Main { @Inject private Logger logger; - + private static String currencyPlural = ""; private static String currencySingular = ""; private static boolean sql = false; @@ -29,6 +28,7 @@ public class Main { private static String database = ""; private static String username = ""; private static String password = ""; + private static String scoreboard = ""; private static MySQL mySQL; @Subscribe @@ -39,10 +39,11 @@ public void onServerInitialize(InitializationEvent event) { configurationDefaults(); loadOptions(); - event.getGame().getEventManager().register(this, new PlayerJoin(logger)); + event.getGame().getEventManager().register(this, new PlayerJoin(logger,event.getGame())); + event.getGame().getEventManager().register(this, new PlayerBalanceChange(logger,event.getGame())); - event.getGame().getCommandDispatcher().register(this, new EconCommand(logger), "econ"); - event.getGame().getCommandDispatcher().register(this, new BalanceCommand(logger), "balance"); + event.getGame().getCommandDispatcher().register(this, new EconCommand(logger,event.getGame()), "econ"); + event.getGame().getCommandDispatcher().register(this, new BalanceCommand(logger,event.getGame()), "balance"); Optional sqlServiceOptional = event.getGame().getServiceManager().provide(SqlService.class); @@ -60,6 +61,7 @@ public void onServerStart(ServerStartedEvent event){ type = "MySQL"; } logger.info("Storage type: "+type); + logger.info("Scoreboard: "+scoreboardEnabled()); } private void createFiles(){ @@ -121,7 +123,7 @@ private void configurationDefaults(){ if(!root.getChildrenMap().containsKey("Currency-Singular")){ root.getNode("Currency-Singular").setValue("Coin"); } - //check if MySQL Node exists + //Check if MySQL Node exists if(!root.getChildrenMap().containsKey("MySQL")){ root.getNode("MySQL").getNode("enabled").setValue(false); } @@ -130,7 +132,7 @@ private void configurationDefaults(){ root.getNode("MySQL").getNode("hostname").setValue("hostname"); } if(!root.getNode("MySQL").getChildrenMap().containsKey("port")){ - root.getNode("MySQL").getNode("port").setValue("3306"); + root.getNode("MySQL").getNode("port").setValue(3306); } if(!root.getNode("MySQL").getChildrenMap().containsKey("database")){ root.getNode("MySQL").getNode("database").setValue("database"); @@ -141,6 +143,10 @@ private void configurationDefaults(){ if(!root.getNode("MySQL").getChildrenMap().containsKey("password")){ root.getNode("MySQL").getNode("password").setValue("password"); } + //end mysql + if(!root.getChildrenMap().containsKey("Scoreboard")){ + root.getNode("Scoreboard").setValue("disabled"); + } manager.saveFile(root); } } @@ -178,6 +184,9 @@ private void loadOptions(){ password = root.getNode("MySQL").getNode("password").getString(); } } + if(root.getChildrenMap().containsKey("Scoreboard")){ + scoreboard = root.getNode("Scoreboard").getString(); + } } } @@ -193,7 +202,16 @@ public static boolean sqlEnabled(){ return sql; } - public static MySQL getSQL(){ + public static boolean scoreboardEnabled(){ + if(scoreboard.equalsIgnoreCase("enabled")){ + return true; + } else { + return false; + } + } + + protected static MySQL getSQL(){ return mySQL; } + } \ No newline at end of file diff --git a/src/me/Flibio/EconomyLite/MySQL.java b/src/me/Flibio/EconomyLite/MySQL.java index 077eb62..19c46ab 100644 --- a/src/me/Flibio/EconomyLite/MySQL.java +++ b/src/me/Flibio/EconomyLite/MySQL.java @@ -146,6 +146,9 @@ protected boolean playerExists(UUID uuid){ } protected int getCurrency(String name) { + /* + * create custom event that is fired + */ reconnect(); int amount; ResultSet res; @@ -156,8 +159,7 @@ protected int getCurrency(String name) { res.next(); amount = res.getInt("currency"); } catch (SQLException e) { - logger.error("Error getting currency of player..."); - e.printStackTrace(); + logger.error("Error getting currency of player..."+e.getMessage()); amount = 0; } closeConnection(); diff --git a/src/me/Flibio/EconomyLite/PlayerBalanceChange.java b/src/me/Flibio/EconomyLite/PlayerBalanceChange.java new file mode 100644 index 0000000..92899b5 --- /dev/null +++ b/src/me/Flibio/EconomyLite/PlayerBalanceChange.java @@ -0,0 +1,51 @@ +package me.Flibio.EconomyLite; + +import org.slf4j.Logger; +import org.spongepowered.api.Game; +import org.spongepowered.api.entity.player.Player; +import org.spongepowered.api.event.Subscribe; +import org.spongepowered.api.scoreboard.Scoreboard; +import org.spongepowered.api.scoreboard.critieria.Criteria; +import org.spongepowered.api.scoreboard.displayslot.DisplaySlots; +import org.spongepowered.api.scoreboard.objective.Objective; +import org.spongepowered.api.text.Texts; +import org.spongepowered.api.text.format.TextColors; + +public class PlayerBalanceChange { + + private Logger logger; + private Game game; + + public PlayerBalanceChange(Logger logger, Game game){ + this.logger = logger; + this.game = game; + } + + @Subscribe + public void onBalanceChange(BalanceChangeEvent event){ + + //Update the scoreboard + if(Main.scoreboardEnabled()) { + int playerBalance = (new DataEditor(logger,game)).getBalance(event.getPlayerName()); + + Player player = null; + + for(Player p : game.getServer().getOnlinePlayers()){ + if(p.getName().equalsIgnoreCase(event.getPlayerName())){ + player = p; + } + } + + if(player!=null){ + Scoreboard board = game.getRegistry().getScoreboardBuilder().build(); + Objective obj = game.getRegistry().getObjectiveBuilder().name("EconomyLite").criterion(Criteria.DUMMY).displayName(Texts.builder("Economy").color(TextColors.YELLOW).build()).build(); + obj.getScore(Texts.builder("Balance: ").color(TextColors.GREEN).build()).setScore(playerBalance); + board.addObjective(obj); + board.addObjective(obj, DisplaySlots.SIDEBAR); + + player.setScoreboard(board); + } + + } + } +} diff --git a/src/me/Flibio/EconomyLite/PlayerJoin.java b/src/me/Flibio/EconomyLite/PlayerJoin.java index 2d717a3..5cc0ff7 100644 --- a/src/me/Flibio/EconomyLite/PlayerJoin.java +++ b/src/me/Flibio/EconomyLite/PlayerJoin.java @@ -9,18 +9,27 @@ import ninja.leaping.configurate.loader.ConfigurationLoader; import org.slf4j.Logger; +import org.spongepowered.api.Game; import org.spongepowered.api.entity.player.Player; import org.spongepowered.api.event.Subscribe; import org.spongepowered.api.event.entity.player.PlayerJoinEvent; +import org.spongepowered.api.scoreboard.Scoreboard; +import org.spongepowered.api.scoreboard.critieria.Criteria; +import org.spongepowered.api.scoreboard.displayslot.DisplaySlots; +import org.spongepowered.api.scoreboard.objective.Objective; +import org.spongepowered.api.text.Texts; +import org.spongepowered.api.text.format.TextColors; public class PlayerJoin { private Logger logger; private boolean mySQL; private MySQL sql; + private Game game; - public PlayerJoin(Logger logger){ + public PlayerJoin(Logger logger, Game game){ this.logger = logger; + this.game = game; mySQL = Main.sqlEnabled(); } @@ -31,6 +40,7 @@ public void onPlayerJoin(PlayerJoinEvent event) { String uuid = player.getUniqueId().toString(); UUID UUID = player.getUniqueId(); + //Economy setup if(mySQL){ sql = Main.getSQL(); sql.reconnect(); @@ -71,5 +81,19 @@ public void onPlayerJoin(PlayerJoinEvent event) { return; } } + + if(Main.scoreboardEnabled()){ + int playerBalance = (new DataEditor(logger,game)).getBalance(player.getName()); + + if(player!=null){ + Scoreboard board = game.getRegistry().getScoreboardBuilder().build(); + Objective obj = game.getRegistry().getObjectiveBuilder().name("EconomyLite").criterion(Criteria.DUMMY).displayName(Texts.builder("Economy").color(TextColors.YELLOW).build()).build(); + obj.getScore(Texts.builder("Balance: ").color(TextColors.GREEN).build()).setScore(playerBalance); + board.addObjective(obj); + board.addObjective(obj, DisplaySlots.SIDEBAR); + + player.setScoreboard(board); + } + } } }