diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..63ffbaa --- /dev/null +++ b/pom.xml @@ -0,0 +1,41 @@ + + 4.0.0 + EconomyLite + EconomyLite + 0.0.1-SNAPSHOT + + src + + + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + + + + sponge-maven-repo + Sponge maven repo + http://repo.spongepowered.org/maven + + true + + + true + + + + + + + org.spongepowered + spongeapi + 2.0 + provided + + + \ No newline at end of file diff --git a/src/me/Flibio/EconomyLite/BalanceCommand.java b/src/me/Flibio/EconomyLite/BalanceCommand.java new file mode 100644 index 0000000..fce8382 --- /dev/null +++ b/src/me/Flibio/EconomyLite/BalanceCommand.java @@ -0,0 +1,87 @@ +package me.Flibio.EconomyLite; + +import java.util.Arrays; +import java.util.List; + +import org.slf4j.Logger; +import org.spongepowered.api.entity.player.Player; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.Texts; +import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.util.command.CommandCallable; +import org.spongepowered.api.util.command.CommandException; +import org.spongepowered.api.util.command.CommandResult; +import org.spongepowered.api.util.command.CommandSource; + +import com.google.common.base.Optional; + +public class BalanceCommand implements CommandCallable { + + private DataEditor dataEditor; + + public BalanceCommand(Logger log){ + dataEditor = new DataEditor(log); + } + + @Override + public Optional getHelp(CommandSource source) { + return Optional.of(Texts.builder("Usage: /balance").build()); + } + + @Override + public Optional getShortDescription(CommandSource source) { + return Optional.of(Texts.builder("Check your EconomyLite balance").build()); + } + + @Override + public List getSuggestions(CommandSource source, String args) + throws CommandException { + return Arrays.asList("/balance"); + } + + @Override + public Text getUsage(CommandSource source) { + return Texts.builder("/balance").build(); + } + + @Override + public Optional 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()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + Player player = (Player) source; + + String name = player.getName(); + + String[] args = arg_string.split(" "); + + if(!args[0].isEmpty()){ + player.sendMessage(Texts.builder("Usage: /balance").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + if(!dataEditor.playerExists(name)){ + player.sendMessage(Texts.builder("Error: Player not found").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + player.sendMessage(Texts.builder("Your").color(TextColors.YELLOW).append( + Texts.builder(" EconomyLite ").color(TextColors.GREEN).build() + ).append( + Texts.builder("balance: ").color(TextColors.YELLOW).build() + ).append( + Texts.builder(""+dataEditor.getBalance(name)).color(TextColors.DARK_GREEN).build() + ).build()); + + return Optional.of(CommandResult.success()); + } + + @Override + public boolean testPermission(CommandSource source) { + return true; + } + +} diff --git a/src/me/Flibio/EconomyLite/DataEditor.java b/src/me/Flibio/EconomyLite/DataEditor.java new file mode 100644 index 0000000..27a90a9 --- /dev/null +++ b/src/me/Flibio/EconomyLite/DataEditor.java @@ -0,0 +1,116 @@ +package me.Flibio.EconomyLite; + +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.Map.Entry; + +import ninja.leaping.configurate.ConfigurationNode; +import ninja.leaping.configurate.hocon.HoconConfigurationLoader; +import ninja.leaping.configurate.loader.ConfigurationLoader; + +import org.slf4j.Logger; + +public class DataEditor { + + private Logger logger; + + public DataEditor(Logger log){ + this.logger = log; + } + + 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(playerExists(name)){ + Map playerMap = root.getChildrenMap(); + for(Entry 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 loading 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 playerMap = root.getChildrenMap(); + + for(Entry entry : playerMap.entrySet()){ + String uuid = (String) entry.getKey(); + if(root.getNode(uuid).getNode("name").getValue().equals(name)){ + return true; + } + } + 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 playerMap = root.getChildrenMap(); + + for(Entry 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; + } + + protected boolean addCurrency(String name, int amount){ + return setBalance(name, amount+getBalance(name)); + } + + protected boolean removeCurrency(String name, int amount){ + return setBalance(name, getBalance(name)-amount); + } +} diff --git a/src/me/Flibio/EconomyLite/EconCommand.java b/src/me/Flibio/EconomyLite/EconCommand.java new file mode 100644 index 0000000..295efbf --- /dev/null +++ b/src/me/Flibio/EconomyLite/EconCommand.java @@ -0,0 +1,172 @@ +package me.Flibio.EconomyLite; + +import java.util.Arrays; +import java.util.List; + +import org.slf4j.Logger; +import org.spongepowered.api.entity.player.Player; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.Texts; +import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.util.command.CommandCallable; +import org.spongepowered.api.util.command.CommandException; +import org.spongepowered.api.util.command.CommandResult; +import org.spongepowered.api.util.command.CommandSource; + +import com.google.common.base.Optional; + +public class EconCommand implements CommandCallable { + + private DataEditor dataEditor; + + public EconCommand(Logger log){ + dataEditor = new DataEditor(log); + } + + @Override + public Optional getHelp(CommandSource source) { + return Optional.of(Texts.builder("Usage: /econ add|remove|set ").build()); + } + + @Override + public Optional getShortDescription(CommandSource source) { + return Optional.of(Texts.builder("Admin EconomyLite command").build()); + } + + @Override + public List getSuggestions(CommandSource source, String args) + throws CommandException { + return Arrays.asList("/econ add|remove|set "); + } + + @Override + public Text getUsage(CommandSource source) { + return Texts.builder("/econ add|remove|set ").build(); + } + + @Override + public Optional process(CommandSource source, String arg_string) + throws CommandException { + String[] args = arg_string.split(" "); + + if(args.length<3){ + source.sendMessage(Texts.builder("Usage: /econ add|remove|set ").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + if(args[0].equalsIgnoreCase("add")){ + + String name = args[2]; + int currentBalance = dataEditor.getBalance(name); + + //Check if player exists + if(!dataEditor.playerExists(name)){ + source.sendMessage(Texts.builder("Error: Player not found").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + //Parse the amount + int amnt; + try{ + amnt = Integer.parseInt(args[1]); + } catch(NumberFormatException e){ + source.sendMessage(Texts.builder("Error: Invalid amount").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + //Amount checks + if(currentBalance+amnt<0||currentBalance+amnt>1000000||amnt<0){ + source.sendMessage(Texts.builder("Error: New balance must be greater than 0 and less than 1,000,000").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + if(dataEditor.addCurrency(name, amnt)){ + source.sendMessage(Texts.builder("Successfully added "+amnt+" currency to "+name+"'s balance!").color(TextColors.GREEN).build()); + return Optional.of(CommandResult.success()); + } else { + source.sendMessage(Texts.builder("Error: Internal plugin error occured while changing the balance").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + } else if(args[0].equalsIgnoreCase("remove")){ + String name = args[2]; + int currentBalance = dataEditor.getBalance(name); + + //Check if player exists + if(!dataEditor.playerExists(name)){ + source.sendMessage(Texts.builder("Error: Player not found").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + //Parse the amount + int amnt; + try{ + amnt = Integer.parseInt(args[1]); + } catch(NumberFormatException e){ + source.sendMessage(Texts.builder("Error: Invalid amount").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + //Amount checks + if(currentBalance-amnt<0||currentBalance-amnt>1000000||amnt<0){ + source.sendMessage(Texts.builder("Error: New balance must be greater than 0 and less than 1,000,000").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + if(dataEditor.removeCurrency(name, amnt)){ + source.sendMessage(Texts.builder("Successfully removed "+amnt+" currency from "+name+"!").color(TextColors.GREEN).build()); + return Optional.of(CommandResult.success()); + } else { + source.sendMessage(Texts.builder("Error: Internal plugin error occured while changing the balance").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + } else if(args[0].equalsIgnoreCase("set")){ + String name = args[2]; + + //Check if player exists + if(!dataEditor.playerExists(name)){ + source.sendMessage(Texts.builder("Error: Player not found").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + //Parse the amount + int amnt; + try{ + amnt = Integer.parseInt(args[1]); + } catch(NumberFormatException e){ + source.sendMessage(Texts.builder("Error: Invalid amount").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + //Amount checks + if(amnt<0||amnt>1000000){ + source.sendMessage(Texts.builder("Error: Balance must be greater than 0 and less than 1,000,000").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + if(dataEditor.setBalance(name, amnt)){ + source.sendMessage(Texts.builder("Successfully set the balance of "+name+" to "+amnt+"!").color(TextColors.GREEN).build()); + return Optional.of(CommandResult.success()); + } else { + source.sendMessage(Texts.builder("Error: Internal plugin error occured while changing the balance").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + + } else { + //Invalid first argument + source.sendMessage(Texts.builder("Usage: /econ add|remove|set ").color(TextColors.RED).build()); + return Optional.of(CommandResult.builder().successCount(0).build()); + } + } + + @Override + public boolean testPermission(CommandSource source) { + if(source instanceof Player){ + Player p = (Player) source; + return p.hasPermission("econ.admin"); + } + return source.hasPermission("econ.admin"); + } + +} diff --git a/src/me/Flibio/EconomyLite/Main.java b/src/me/Flibio/EconomyLite/Main.java new file mode 100644 index 0000000..5b2b478 --- /dev/null +++ b/src/me/Flibio/EconomyLite/Main.java @@ -0,0 +1,70 @@ +package me.Flibio.EconomyLite; + +import java.io.File; +import java.io.IOException; + +import org.slf4j.Logger; +import org.spongepowered.api.event.Subscribe; +import org.spongepowered.api.event.state.ServerStartedEvent; +import org.spongepowered.api.event.state.ServerStartingEvent; +import org.spongepowered.api.plugin.Plugin; + +import com.google.inject.Inject; + +@Plugin(id = "EconomyLite", name = "EconomyLite", version = "0.0.2") +public class Main { + + @Inject + private Logger logger; + + @Subscribe + public void onServerStart(ServerStartedEvent event) { + logger.info("EconomyLite by Flibio enabling!"); + + event.getGame().getEventManager().register(this, new PlayerJoin(logger)); + + event.getGame().getCommandDispatcher().register(this, new EconCommand(logger), "econ"); + event.getGame().getCommandDispatcher().register(this, new BalanceCommand(logger), "balance"); + + } + + @Subscribe + public void onServerStarting(ServerStartingEvent event){ + createFiles(); + } + + private void createFiles(){ + //Create EconomyLite folder if doesn't exist + File folder = new File("EconomyLite"); + try{ + if(!folder.exists()){ + logger.info("No EconomyLite folder found, attempting to create one"); + if(folder.mkdir()){ + logger.info("Successfully created EconomyLite folder!"); + } else { + logger.warn("Error creating EconomyLite folder!"); + } + + } + } catch(Exception e){ + 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"); + if(!data.exists()){ + logger.info("No existing data file was found, attempting to create a new one!"); + try { + data.createNewFile(); + logger.info("Successfully created the data file!"); + } catch (IOException e) { + logger.error("Error while creating data file!"); + e.printStackTrace(); + } + } + + } + +} \ No newline at end of file diff --git a/src/me/Flibio/EconomyLite/PlayerJoin.java b/src/me/Flibio/EconomyLite/PlayerJoin.java new file mode 100644 index 0000000..cbea62f --- /dev/null +++ b/src/me/Flibio/EconomyLite/PlayerJoin.java @@ -0,0 +1,58 @@ +package me.Flibio.EconomyLite; + +import java.io.File; +import java.io.IOException; + +import ninja.leaping.configurate.ConfigurationNode; +import ninja.leaping.configurate.hocon.HoconConfigurationLoader; +import ninja.leaping.configurate.loader.ConfigurationLoader; + +import org.slf4j.Logger; +import org.spongepowered.api.entity.player.Player; +import org.spongepowered.api.event.Subscribe; +import org.spongepowered.api.event.entity.player.PlayerJoinEvent; + +public class PlayerJoin { + + Logger logger; + + public PlayerJoin(Logger logger){ + this.logger = logger; + } + + @Subscribe + public void onPlayerJoin(PlayerJoinEvent event) { + Player player = event.getUser(); + + String uuid = player.getUniqueId().toString(); + + 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; + } + + //If UUID doesn't exist add it + if(!root.getChildrenMap().containsKey(uuid)){ + root.getNode(uuid).getNode("name").setValue(player.getName()); + root.getNode(uuid).getNode("balance").setValue(0); + } else { + //UUID Exists set the name in case of name change + root.getNode(uuid).getNode("name").setValue(player.getName()); + } + + try { + manager.save(root); + } catch (IOException e) { + logger.error("Error loading data file!"); + logger.error(e.getMessage()); + return; + } + } +}