Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
v0.0.5 - Beta
Browse files Browse the repository at this point in the history
  • Loading branch information
Flibio committed Jul 17, 2015
1 parent 8fcf38a commit 245f715
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 23 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<modelVersion>4.0.0</modelVersion>
<groupId>EconomyLite</groupId>
<artifactId>EconomyLite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.5</version>
<packaging>jar</packaging>
<name>EconomyLite</name>
<description>An economy plugin for Sponge</description>
<url>https://github.com/Flibio/EconomyLite</url>

<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
Expand Down Expand Up @@ -34,7 +39,7 @@
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>spongeapi</artifactId>
<version>2.0</version>
<version>2.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
29 changes: 29 additions & 0 deletions src/me/Flibio/EconomyLite/BalanceChangeEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
5 changes: 3 additions & 2 deletions src/me/Flibio/EconomyLite/BalanceCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions src/me/Flibio/EconomyLite/ConfigurationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import org.slf4j.Logger;

import com.typesafe.config.ConfigException;

public class ConfigurationManager {


Expand All @@ -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;
}
Expand Down
12 changes: 10 additions & 2 deletions src/me/Flibio/EconomyLite/DataEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
31 changes: 26 additions & 5 deletions src/me/Flibio/EconomyLite/EconCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -95,7 +96,14 @@ public Optional<CommandResult> 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());
Expand Down Expand Up @@ -132,7 +140,14 @@ public Optional<CommandResult> 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());
Expand Down Expand Up @@ -164,7 +179,13 @@ public Optional<CommandResult> 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());
Expand Down
36 changes: 27 additions & 9 deletions src/me/Flibio/EconomyLite/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.io.IOException;

import ninja.leaping.configurate.ConfigurationNode;

import org.slf4j.Logger;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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<SqlService> sqlServiceOptional = event.getGame().getServiceManager().provide(SqlService.class);

Expand All @@ -60,6 +61,7 @@ public void onServerStart(ServerStartedEvent event){
type = "MySQL";
}
logger.info("Storage type: "+type);
logger.info("Scoreboard: "+scoreboardEnabled());
}

private void createFiles(){
Expand Down Expand Up @@ -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);
}
Expand All @@ -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");
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -178,6 +184,9 @@ private void loadOptions(){
password = root.getNode("MySQL").getNode("password").getString();
}
}
if(root.getChildrenMap().containsKey("Scoreboard")){
scoreboard = root.getNode("Scoreboard").getString();
}
}
}

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

}
6 changes: 4 additions & 2 deletions src/me/Flibio/EconomyLite/MySQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
51 changes: 51 additions & 0 deletions src/me/Flibio/EconomyLite/PlayerBalanceChange.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
}
}
Loading

0 comments on commit 245f715

Please sign in to comment.