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

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Flibio committed Jul 11, 2015
1 parent 1760978 commit af6c854
Show file tree
Hide file tree
Showing 6 changed files with 544 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>EconomyLite</groupId>
<artifactId>EconomyLite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>sponge-maven-repo</id>
<name>Sponge maven repo</name>
<url>http://repo.spongepowered.org/maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>spongeapi</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
87 changes: 87 additions & 0 deletions src/me/Flibio/EconomyLite/BalanceCommand.java
Original file line number Diff line number Diff line change
@@ -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<Text> getHelp(CommandSource source) {
return Optional.of(Texts.builder("Usage: /balance").build());
}

@Override
public Optional<Text> getShortDescription(CommandSource source) {
return Optional.of(Texts.builder("Check your EconomyLite balance").build());
}

@Override
public List<String> 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<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());
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;
}

}
116 changes: 116 additions & 0 deletions src/me/Flibio/EconomyLite/DataEditor.java
Original file line number Diff line number Diff line change
@@ -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<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 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<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;
}
}
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;
}
}
}
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);
}
}
Loading

0 comments on commit af6c854

Please sign in to comment.