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

Commit

Permalink
v0.0.4 - Alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
Flibio committed Jul 12, 2015
1 parent 821851c commit 2da2d91
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 108 deletions.
2 changes: 1 addition & 1 deletion src/me/Flibio/EconomyLite/BalanceCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Text getUsage(CommandSource source) {
public Optional<CommandResult> process(CommandSource source, String arg_string)
throws CommandException {
if(!(source instanceof Player)){
source.sendMessage(Texts.builder("Error: You must a player to use /balance").color(TextColors.RED).build());
source.sendMessage(Texts.builder("Error: You must be a player to use /balance").color(TextColors.RED).build());
return Optional.of(CommandResult.builder().successCount(0).build());
}

Expand Down
4 changes: 2 additions & 2 deletions src/me/Flibio/EconomyLite/ConfigurationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ConfigurationManager(Logger log){
}

protected ConfigurationNode getFile(){
ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(new File("EconomyLite/config.conf")).build();
ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(new File("config/EconomyLite/config.conf")).build();
ConfigurationNode root;
try {
root = manager.load();
Expand All @@ -32,7 +32,7 @@ protected ConfigurationNode getFile(){
}

protected void saveFile(ConfigurationNode root){
ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(new File("EconomyLite/config.conf")).build();
ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(new File("config/EconomyLite/config.conf")).build();

try {
manager.save(root);
Expand Down
154 changes: 86 additions & 68 deletions src/me/Flibio/EconomyLite/DataEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,96 +14,114 @@
public class DataEditor {

private Logger logger;
private boolean mySQL;
private MySQL sql;

public DataEditor(Logger log){
this.logger = log;
mySQL = Main.sqlEnabled();
}

protected boolean setBalance(String name, int balance) {
ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(new File("EconomyLite/data.conf")).build();
ConfigurationNode root;
try {
root = manager.load();
} catch (IOException e) {
logger.error("Error loading data file!");
logger.error(e.getMessage());
return false;
if(mySQL){
sql = Main.getSQL();
return sql.setCurrency(name, balance);
} else {
ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(new File("config/EconomyLite/data.conf")).build();
ConfigurationNode root;
try {
root = manager.load();
} catch (IOException e) {
logger.error("Error loading data file!");
logger.error(e.getMessage());
return false;
}

if(playerExists(name)){
Map<Object, ? extends ConfigurationNode> playerMap = root.getChildrenMap();
for(Entry<Object, ? extends ConfigurationNode> entry : playerMap.entrySet()){
String uuid = (String) entry.getKey();
if(root.getNode(uuid).getNode("name").getValue().equals(name)){
root.getNode(uuid).getNode("balance").setValue(""+balance);
}
}
} else {
return false;
}

try {
manager.save(root);
} catch (IOException e) {
logger.error("Error saving data file!");
logger.error(e.getMessage());
return false;
}
return true;
}

if(playerExists(name)){
}

protected boolean playerExists(String name) {
if(mySQL){
sql = Main.getSQL();
return sql.playerExists(name);
} else {
ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(new File("config/EconomyLite/data.conf")).build();
ConfigurationNode root;

try {
root = manager.load();
} catch (IOException e) {
logger.error("Error loading data file!");
logger.error(e.getMessage());
return false;
}

//Iterate and find the name
Map<Object, ? extends ConfigurationNode> playerMap = root.getChildrenMap();

for(Entry<Object, ? extends ConfigurationNode> entry : playerMap.entrySet()){
String uuid = (String) entry.getKey();
if(root.getNode(uuid).getNode("name").getValue().equals(name)){
root.getNode(uuid).getNode("balance").setValue(""+balance);
return true;
}
}
} else {
return false;
}

try {
manager.save(root);
} catch (IOException e) {
logger.error("Error saving data file!");
logger.error(e.getMessage());
return false;
}
return true;
}

protected boolean playerExists(String name) {
ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(new File("EconomyLite/data.conf")).build();
ConfigurationNode root;

try {
root = manager.load();
} catch (IOException e) {
logger.error("Error loading data file!");
logger.error(e.getMessage());
return false;
}

//Iterate and find the name
Map<Object, ? extends ConfigurationNode> playerMap = root.getChildrenMap();

for(Entry<Object, ? extends ConfigurationNode> entry : playerMap.entrySet()){
String uuid = (String) entry.getKey();
if(root.getNode(uuid).getNode("name").getValue().equals(name)){
return true;
protected int getBalance(String name) {
if(mySQL){
sql = Main.getSQL();
return sql.getCurrency(name);
} else {
ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(new File("config/EconomyLite/data.conf")).build();
ConfigurationNode root;

try {
root = manager.load();
} catch (IOException e) {
logger.error("Error loading data file!");
logger.error(e.getMessage());
return 0;
}
}
return false;
}

protected int getBalance(String name) {
ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(new File("EconomyLite/data.conf")).build();
ConfigurationNode root;

try {
root = manager.load();
} catch (IOException e) {
logger.error("Error loading data file!");
logger.error(e.getMessage());
return 0;
}

Map<Object, ? extends ConfigurationNode> playerMap = root.getChildrenMap();

for(Entry<Object, ? extends ConfigurationNode> entry : playerMap.entrySet()){
String uuid = (String) entry.getKey();
if(root.getNode(uuid).getNode("name").getValue().equals(name)){
String amnt = root.getNode(uuid).getNode("balance").getString();
try{
return Integer.parseInt(amnt);
} catch(NumberFormatException e){
logger.error("Invalid number read from data file!");
logger.error(e.getMessage());
return 0;
Map<Object, ? extends ConfigurationNode> playerMap = root.getChildrenMap();

for(Entry<Object, ? extends ConfigurationNode> entry : playerMap.entrySet()){
String uuid = (String) entry.getKey();
if(root.getNode(uuid).getNode("name").getValue().equals(name)){
String amnt = root.getNode(uuid).getNode("balance").getString();
try{
return Integer.parseInt(amnt);
} catch(NumberFormatException e){
logger.error("Invalid number read from data file!");
logger.error(e.getMessage());
return 0;
}
}
}
return 0;
}
return 0;
}

protected boolean addCurrency(String name, int amount){
Expand Down
93 changes: 83 additions & 10 deletions src/me/Flibio/EconomyLite/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,32 @@
import org.slf4j.Logger;
import org.spongepowered.api.event.Subscribe;
import org.spongepowered.api.event.state.InitializationEvent;
import org.spongepowered.api.event.state.ServerStartedEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.service.sql.SqlService;

import com.google.common.base.Optional;
import com.google.inject.Inject;

@Plugin(id = "EconomyLite", name = "EconomyLite", version = "0.0.3")
@Plugin(id = "EconomyLite", name = "EconomyLite", version = "0.0.4")
public class Main {

@Inject
private Logger logger;

private static String currencyPlural = "";
private static String currencySingular = "";
private static boolean sql = false;
private static String hostname = "";
private static String port = "";
private static String database = "";
private static String username = "";
private static String password = "";
private static MySQL mySQL;

@Subscribe
public void onServerStart(InitializationEvent event) {
logger.info("EconomyLite by Flibio enabling!");
public void onServerInitialize(InitializationEvent event) {
logger.info("EconomyLite by Flibio initializing!");

createFiles();
configurationDefaults();
Expand All @@ -34,11 +44,27 @@ public void onServerStart(InitializationEvent event) {
event.getGame().getCommandDispatcher().register(this, new EconCommand(logger), "econ");
event.getGame().getCommandDispatcher().register(this, new BalanceCommand(logger), "balance");

}
Optional<SqlService> sqlServiceOptional = event.getGame().getServiceManager().provide(SqlService.class);

if(sql&&sqlServiceOptional.isPresent()){
logger.info("MySQL Storage Enabled...");
mySQL = new MySQL(hostname,port,database,username,password,logger,sqlServiceOptional.get());
}
}

@Subscribe
public void onServerStart(ServerStartedEvent event){
logger.info("EconomyLite by Flibio enabled: ");
String type = "Local File";
if(sql){
type = "MySQL";
}
logger.info("Storage type: "+type);
}

private void createFiles(){
//Create EconomyLite folder if doesn't exist
File folder = new File("EconomyLite");
File folder = new File("config/EconomyLite");
try{
if(!folder.exists()){
logger.info("No EconomyLite folder found, attempting to create one");
Expand All @@ -50,13 +76,13 @@ private void createFiles(){

}
} catch(Exception e){
logger.warn("Error creating EconomyLite folder! DETAILED ERROR:");
logger.warn("Error creating EconomyLite folder! Detailed Error:");
logger.warn(e.getMessage());
logger.warn("END EconomyLite Error");
}

//EconomyLite File Generation
File data = new File("EconomyLite/data.conf");
File data = new File("config/EconomyLite/data.conf");
if(!data.exists()){
logger.info("No existing data file was found, attempting to create a new one!");
try {
Expand All @@ -68,7 +94,7 @@ private void createFiles(){
}
}
//Configuration file
File conf = new File("EconomyLite/config.conf");
File conf = new File("config/EconomyLite/config.conf");
if(!conf.exists()){
logger.info("No existing configuration file was found, attempting to create a new one!");
try {
Expand All @@ -91,12 +117,31 @@ private void configurationDefaults(){
if(root!=null){
if(!root.getChildrenMap().containsKey("Currency-Plural")){
root.getNode("Currency-Plural").setValue("Coins");
manager.saveFile(root);
}
if(!root.getChildrenMap().containsKey("Currency-Singular")){
root.getNode("Currency-Singular").setValue("Coin");
manager.saveFile(root);
}
//check if MySQL Node exists
if(!root.getChildrenMap().containsKey("MySQL")){
root.getNode("MySQL").getNode("enabled").setValue(false);
}
//Check all MySQL child nodes
if(!root.getNode("MySQL").getChildrenMap().containsKey("hostname")){
root.getNode("MySQL").getNode("hostname").setValue("hostname");
}
if(!root.getNode("MySQL").getChildrenMap().containsKey("port")){
root.getNode("MySQL").getNode("port").setValue("3306");
}
if(!root.getNode("MySQL").getChildrenMap().containsKey("database")){
root.getNode("MySQL").getNode("database").setValue("database");
}
if(!root.getNode("MySQL").getChildrenMap().containsKey("username")){
root.getNode("MySQL").getNode("username").setValue("username");
}
if(!root.getNode("MySQL").getChildrenMap().containsKey("password")){
root.getNode("MySQL").getNode("password").setValue("password");
}
manager.saveFile(root);
}
}

Expand All @@ -113,6 +158,26 @@ private void loadOptions(){
if(root.getChildrenMap().containsKey("Currency-Singular")){
currencySingular = root.getNode("Currency-Singular").getString();
}
if(root.getChildrenMap().containsKey("MySQL")){
if(root.getNode("MySQL").getChildrenMap().containsKey("enabled")){
sql = root.getNode("MySQL").getNode("enabled").getBoolean();
}
if(root.getNode("MySQL").getChildrenMap().containsKey("hostname")){
hostname = root.getNode("MySQL").getNode("hostname").getString();
}
if(root.getNode("MySQL").getChildrenMap().containsKey("port")){
port = root.getNode("MySQL").getNode("port").getString();
}
if(root.getNode("MySQL").getChildrenMap().containsKey("database")){
database = root.getNode("MySQL").getNode("database").getString();
}
if(root.getNode("MySQL").getChildrenMap().containsKey("username")){
username = root.getNode("MySQL").getNode("username").getString();
}
if(root.getNode("MySQL").getChildrenMap().containsKey("password")){
password = root.getNode("MySQL").getNode("password").getString();
}
}
}
}

Expand All @@ -123,4 +188,12 @@ public static String getCurrencyPlural(){
public static String getCurrencySingular(){
return currencySingular;
}

public static boolean sqlEnabled(){
return sql;
}

public static MySQL getSQL(){
return mySQL;
}
}
Loading

0 comments on commit 2da2d91

Please sign in to comment.