Skip to content

Instantly share code, notes, and snippets.

@Flibio
Last active October 20, 2015 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Flibio/22916e446108e873320f to your computer and use it in GitHub Desktop.
Save Flibio/22916e446108e873320f to your computer and use it in GitHub Desktop.
EconomyManager for Sponge (Required Variant)
/*
The MIT License (MIT)
Copyright (c) 2015 Flibio
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package me.Flibio.JobsLite;
import me.Flibio.EconomyLite.API.EconomyLiteAPI;
import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.text.format.TextColors;
import com.erigitic.service.TEService;
import java.math.BigDecimal;
import java.util.Optional;
import java.util.UUID;
public class EconomyManager {
public enum EconomyType { ECONOMY_LITE,TOTAL_ECONOMY,INVALID }
private Logger logger;
private Game game;
private String pluginName;
private boolean firstRun = true;
private EconomyType economyType = EconomyType.INVALID;
private EconomyLiteAPI economyLiteAPI;
private TEService totalEconomyAPI;
public EconomyManager(Logger logger, Game game, String pluginName) {
this.logger = logger;
this.game = game;
this.pluginName = pluginName;
}
public EconomyType getEconomyType() {
return economyType;
}
/**
* Sets the balance of a player
* @param uuid
* UUID of the player whose balance to change
* @return
* Boolean based on if the method was successful or not
*/
public boolean setBalance(UUID uuid, double amount) {
if(firstRun) {
initializeEconomy();
}
if(economyType.equals(EconomyType.INVALID)) {
return false;
}
if(amount<0) {
return false;
}
if(economyType.equals(EconomyType.ECONOMY_LITE)) {
return economyLiteAPI.getPlayerAPI().setBalance(uuid.toString(), Integer.valueOf((int) Math.round(amount)));
} else if(economyType.equals(EconomyType.TOTAL_ECONOMY)) {
totalEconomyAPI.setBalance(uuid, BigDecimal.valueOf(amount));
return true;
}
return false;
}
/**
* Gets the balance of a player
* @param uuid
* UUID of the player to get the balance of
* @return
* The balance of the player
*/
public Optional<Double> getBalance(UUID uuid) {
if(firstRun) {
initializeEconomy();
}
if(economyType.equals(EconomyType.INVALID)) {
return Optional.empty();
}
if(economyType.equals(EconomyType.ECONOMY_LITE)) {
int bal = economyLiteAPI.getPlayerAPI().getBalance(uuid.toString());
if(bal<0) {
return Optional.empty();
}
return Optional.of((double) bal);
} else if(economyType.equals(EconomyType.TOTAL_ECONOMY)) {
return Optional.of(totalEconomyAPI.getBalance(uuid).doubleValue());
}
return Optional.empty();
}
/**
* Adds currency to a players balance
* @param uuid
* UUID of the player whose balance to change
* @param amount
* Amount of currency to add to the player
* @return
* Boolean based on if the method was successful or not
*/
public boolean addCurrency(UUID uuid, double amount) {
if(firstRun) {
initializeEconomy();
}
if(economyType.equals(EconomyType.INVALID)) {
return false;
}
if(amount<0) {
return false;
}
if(economyType.equals(EconomyType.ECONOMY_LITE)) {
return economyLiteAPI.getPlayerAPI().addCurrency(uuid.toString(), Integer.valueOf((int) Math.round(amount)));
} else if(economyType.equals(EconomyType.TOTAL_ECONOMY)) {
totalEconomyAPI.addToBalance(uuid, BigDecimal.valueOf(amount), false);
return true;
}
return false;
}
/**
* Removes currency from a players balance
* @param uuid
* UUID of the player whose balance to change
* @param amount
* Amount of currency to remove from the player
* @return
* Boolean based on if the method was successful or not
*/
public boolean removeCurrency(UUID uuid, double amount) {
if(firstRun) {
initializeEconomy();
}
if(economyType.equals(EconomyType.INVALID)) {
return false;
}
if(amount<0) {
return false;
}
if(economyType.equals(EconomyType.ECONOMY_LITE)) {
return economyLiteAPI.getPlayerAPI().removeCurrency(uuid.toString(), Integer.valueOf((int) Math.round(amount)));
} else if(economyType.equals(EconomyType.TOTAL_ECONOMY)) {
totalEconomyAPI.removeFromBalance(uuid, BigDecimal.valueOf(amount));
return true;
}
return false;
}
/**
* Intializes the economy system
*/
public void initializeEconomy() {
firstRun = false;
boolean economyLitePresent = false;
boolean totalEconomyPresent = false;
//Check for EconomyLite
if(game.getPluginManager().getPlugin("EconomyLite").isPresent()) {
Optional<EconomyLiteAPI> service = game.getServiceManager().provide(EconomyLiteAPI.class);
if(service.isPresent()) {
economyLitePresent = true;
}
}
//Check for TotalEconomy
if(game.getPluginManager().getPlugin("TotalEconomy").isPresent()) {
Optional<TEService> service = game.getServiceManager().provide(TEService.class);
if(service.isPresent()) {
totalEconomyPresent = true;
}
}
if(economyLitePresent&&totalEconomyPresent) {
//Too many economy plugins installed
economyType = EconomyType.INVALID;
logger.error(pluginName+" requires only 1 economy plugin! Please only have 1 installed!");
game.getServer().shutdown(Texts.of(TextColors.RED, pluginName+": Please only install one economy plugin!"));
return;
} else if(!economyLitePresent&&!totalEconomyPresent) {
//No economy plugin detected
economyType = EconomyType.INVALID;
logger.error(pluginName+" an economy plugin! Please install either TotalEconomy or EconomyLite!");
game.getServer().shutdown(Texts.of(TextColors.RED, pluginName+": Please install an economy plugin!"));
return;
} else if(economyLitePresent) {
//Load EconomyLite
economyType = EconomyType.ECONOMY_LITE;
Optional<EconomyLiteAPI> service = game.getServiceManager().provide(EconomyLiteAPI.class);
if(service.isPresent()) {
economyLiteAPI = service.get();
} else {
economyType = EconomyType.INVALID;
logger.error(pluginName+": An unknown error has occured!");
game.getServer().shutdown(Texts.of(TextColors.RED, pluginName+": An unknown error has occured!"));
return;
}
} else if(totalEconomyPresent) {
//Load TotalEconomy
economyType = EconomyType.TOTAL_ECONOMY;
Optional<TEService> service = game.getServiceManager().provide(TEService.class);
if(service.isPresent()) {
totalEconomyAPI = service.get();
} else {
economyType = EconomyType.INVALID;
logger.error(pluginName+": An unknown error has occured!");
game.getServer().shutdown(Texts.of(TextColors.RED, pluginName+": An unknown error has occured!"));
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment