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

Commit

Permalink
Refactored for API 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Flibio committed Mar 8, 2016
1 parent 9fb6ef5 commit 03b61e6
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 46 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>EconomyLite</groupId>
<artifactId>EconomyLite</artifactId>
<version>1.1.8</version>
<version>1.1.9</version>
<packaging>jar</packaging>
<name>EconomyLite</name>
<description>An economy plugin for Sponge</description>
Expand Down Expand Up @@ -60,7 +60,7 @@
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>spongeapi</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
33 changes: 12 additions & 21 deletions src/me/Flibio/EconomyLite/API/LiteEconomyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.spongepowered.api.service.economy.EconomyService;
import org.spongepowered.api.service.economy.account.Account;
import org.spongepowered.api.service.economy.account.UniqueAccount;
import org.spongepowered.api.service.economy.account.VirtualAccount;

import java.util.HashSet;
import java.util.Optional;
Expand All @@ -27,7 +26,7 @@ public void registerContextCalculator(ContextCalculator<Account> arg0) {
}

@Override
public Optional<UniqueAccount> createAccount(UUID uuid) {
public Optional<UniqueAccount> getOrCreateAccount(UUID uuid) {
if(playerManager.playerExists(uuid.toString())) {
return Optional.of(new LiteUniqueAccount(uuid));
} else {
Expand All @@ -40,7 +39,7 @@ public Optional<UniqueAccount> createAccount(UUID uuid) {
}

@Override
public Optional<VirtualAccount> createVirtualAccount(String id) {
public Optional<Account> getOrCreateAccount(String id) {
if(businessManager.businessExists(id)) {
return Optional.of(new LiteVirtualAccount(id));
} else {
Expand All @@ -52,24 +51,6 @@ public Optional<VirtualAccount> createVirtualAccount(String id) {
}
}

@Override
public Optional<UniqueAccount> getAccount(UUID uuid) {
if(playerManager.playerExists(uuid.toString())) {
return Optional.of(new LiteUniqueAccount(uuid));
} else {
return Optional.empty();
}
}

@Override
public Optional<Account> getAccount(String id) {
if(businessManager.businessExists(id)) {
return Optional.of(new LiteVirtualAccount(id));
} else {
return Optional.empty();
}
}

@Override
public Set<Currency> getCurrencies() {
HashSet<Currency> set = new HashSet<Currency>();
Expand All @@ -82,4 +63,14 @@ public Currency getDefaultCurrency() {
return EconomyLite.getCurrency();
}

@Override
public boolean hasAccount(UUID uuid) {
return playerManager.playerExists(uuid.toString());
}

@Override
public boolean hasAccount(String id) {
return businessManager.businessExists(id);
}

}
8 changes: 4 additions & 4 deletions src/me/Flibio/EconomyLite/API/LiteUniqueAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ public String getIdentifier() {
return this.uuid.toString();
}

@Override
public UUID getUUID() {
return this.uuid;
}
@Override
public UUID getUniqueId() {
return this.uuid;
}

}
2 changes: 1 addition & 1 deletion src/me/Flibio/EconomyLite/Commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void run() {
//Get the players UUID
String uuid = playerManager.getUUID(playerName);
if(!uuid.isEmpty()) {
Optional<UniqueAccount> uOpt = economyService.getAccount(UUID.fromString(uuid));
Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(UUID.fromString(uuid));
if(!uOpt.isPresent()) {
//Account is not present
source.sendMessage(textUtils.basicText("An internal error has occured!", TextColors.RED));
Expand Down
2 changes: 1 addition & 1 deletion src/me/Flibio/EconomyLite/Commands/BalanceCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void run() {
}
} else {
//Player wants to view their balance
Optional<UniqueAccount> uOpt = economyService.getAccount(player.getUniqueId());
Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(player.getUniqueId());
if(!uOpt.isPresent()) {
//Account is not present
source.sendMessage(textUtils.basicText("An internal error has occured!", TextColors.RED));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void run() {
player.sendMessage(textUtils.deleteSuccess(correctName));
//Distribute funds to all owners
for(String uuid : owners) {
Optional<UniqueAccount> uOpt = economyService.getAccount(UUID.fromString(uuid));
Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(UUID.fromString(uuid));
if(!uOpt.isPresent()) {
//Account is not present
source.sendMessage(textUtils.basicText("An internal error has occured!", TextColors.RED));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void run() {
int amount = rawAmount.get();
String businessName = rawBusiness.get().trim();
String correctName = businessManager.getCorrectBusinessName(businessName);
Optional<UniqueAccount> uOpt = economyService.getAccount(player.getUniqueId());
Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(player.getUniqueId());
if(!uOpt.isPresent()) {
//Account is not present
source.sendMessage(textUtils.basicText("An internal error has occured!", TextColors.RED));
Expand Down
8 changes: 4 additions & 4 deletions src/me/Flibio/EconomyLite/Commands/PayCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void run() {
//Get the UUID and check if it exists
String targetUuid = playerManager.getUUID(who);
//Check if the uuid is a registered player
if(economyService.getAccount(UUID.fromString(targetUuid)).isPresent()) {
if(economyService.getOrCreateAccount(UUID.fromString(targetUuid)).isPresent()) {
//It is a player, is it also a business?
if(businessManager.businessExists(who)) {
//Present them with an option
Expand Down Expand Up @@ -115,7 +115,7 @@ public void run() {
}

private void payBusiness(String uuid, int amount, Player player, String businessName) {
UniqueAccount account = economyService.getAccount(UUID.fromString(uuid)).get();
UniqueAccount account = economyService.getOrCreateAccount(UUID.fromString(uuid)).get();
int balance = account.getBalance(currency).setScale(0, RoundingMode.HALF_UP).intValue();
//Check for an error
if(balance>-1) {
Expand Down Expand Up @@ -160,8 +160,8 @@ private void payBusiness(String uuid, int amount, Player player, String business
}

private void payPlayer(String uuid, int amount, Player player, String playerName, String targetUUID) {
UniqueAccount account = economyService.getAccount(UUID.fromString(uuid)).get();
UniqueAccount targetAccount = economyService.getAccount(UUID.fromString(targetUUID)).get();
UniqueAccount account = economyService.getOrCreateAccount(UUID.fromString(uuid)).get();
UniqueAccount targetAccount = economyService.getOrCreateAccount(UUID.fromString(targetUUID)).get();
int balance = account.getBalance(currency).setScale(0, RoundingMode.HALF_UP).intValue();
//Check for an error
if(balance>-1) {
Expand Down
6 changes: 3 additions & 3 deletions src/me/Flibio/EconomyLite/Commands/PayOverrideCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void run() {
}

private void payBusiness(String uuid, int amount, Player player, String businessName) {
UniqueAccount account = economyService.getAccount(UUID.fromString(uuid)).get();
UniqueAccount account = economyService.getOrCreateAccount(UUID.fromString(uuid)).get();
int balance = account.getBalance(currency).setScale(0, RoundingMode.HALF_UP).intValue();
//Check for an error
if(balance>-1) {
Expand Down Expand Up @@ -154,8 +154,8 @@ private void payBusiness(String uuid, int amount, Player player, String business
}

private void payPlayer(String uuid, int amount, Player player, String playerName, String targetUUID) {
UniqueAccount account = economyService.getAccount(UUID.fromString(uuid)).get();
UniqueAccount targetAccount = economyService.getAccount(UUID.fromString(targetUUID)).get();
UniqueAccount account = economyService.getOrCreateAccount(UUID.fromString(uuid)).get();
UniqueAccount targetAccount = economyService.getOrCreateAccount(UUID.fromString(targetUUID)).get();
int balance = account.getBalance(currency).setScale(0, RoundingMode.HALF_UP).intValue();
//Check for an error
if(balance>-1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void run() {
//Player wants to view another player's balance
String targetName = target.get();
String uuid = playerManager.getUUID(targetName);
Optional<UniqueAccount> oAct = economyService.getAccount(UUID.fromString(uuid));
Optional<UniqueAccount> oAct = economyService.getOrCreateAccount(UUID.fromString(uuid));
if(oAct.isPresent()) {
int balance = oAct.get().getBalance(currency).setScale(0, RoundingMode.HALF_UP).intValue();
if(balance<0) {
Expand Down
2 changes: 1 addition & 1 deletion src/me/Flibio/EconomyLite/Commands/RemoveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void run() {
//Get the players UUID
String uuid = playerManager.getUUID(playerName);
if(!uuid.isEmpty()) {
Optional<UniqueAccount> uOpt = economyService.getAccount(UUID.fromString(uuid));
Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(UUID.fromString(uuid));
if(!uOpt.isPresent()) {
//Account is not present
source.sendMessage(textUtils.basicText("An internal error has occured!", TextColors.RED));
Expand Down
2 changes: 1 addition & 1 deletion src/me/Flibio/EconomyLite/Commands/SetCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void run() {
//Get the players UUID
String uuid = playerManager.getUUID(playerName);
if(!uuid.isEmpty()) {
Optional<UniqueAccount> uOpt = economyService.getAccount(UUID.fromString(uuid));
Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(UUID.fromString(uuid));
if(!uOpt.isPresent()) {
//Account is not present
source.sendMessage(textUtils.basicText("An internal error has occured!", TextColors.RED));
Expand Down
5 changes: 3 additions & 2 deletions src/me/Flibio/EconomyLite/EconomyLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.plugin.Dependency;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.service.economy.Currency;
import org.spongepowered.api.service.economy.EconomyService;
Expand All @@ -44,8 +45,8 @@
import java.util.HashMap;
import java.util.Optional;

@Updatifier(repoName = "EconomyLite", repoOwner = "Flibio", version = "v1.1.8")
@Plugin(id = "EconomyLite", name = "EconomyLite", version = "1.1.8", dependencies = "after: Updatifier")
@Updatifier(repoName = "EconomyLite", repoOwner = "Flibio", version = "v1.1.9")
@Plugin(id = "EconomyLite", name = "EconomyLite", version = "1.1.9", dependencies = @Dependency(id = "Updatifier", optional = true))
public class EconomyLite {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void onPlayerBalanceChange(BalanceChangeEvent event) {
Text balanceLabel = Text.builder("Balance: ").color(TextColors.GREEN).build();

HashMap<Text, Integer> objectiveValues = new HashMap<Text, Integer>();
Optional<UniqueAccount> uOpt = economyService.getAccount(uuid);
Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(uuid);
if(uOpt.isPresent()) {
UniqueAccount account = uOpt.get();
objectiveValues.put(balanceLabel, account.getBalance(currency).setScale(0, RoundingMode.HALF_UP).intValue());
Expand Down
4 changes: 2 additions & 2 deletions src/me/Flibio/EconomyLite/Listeners/PlayerJoinListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public class PlayerJoinListener {
public void onPlayerJoin(ClientConnectionEvent.Join event) {
Player player = (Player) event.getTargetEntity();

economyService.createAccount(player.getUniqueId());
economyService.getOrCreateAccount(player.getUniqueId());

//Show scoreboard if it is enabled
if(EconomyLite.optionEnabled("scoreboard")) {
Text displayName = Text.builder("EconomyLite").color(TextColors.YELLOW).build();
Text balanceLabel = Text.builder("Balance: ").color(TextColors.GREEN).build();

HashMap<Text, Integer> objectiveValues = new HashMap<Text, Integer>();
Optional<UniqueAccount> uOpt = economyService.getAccount(player.getUniqueId());
Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(player.getUniqueId());
if(uOpt.isPresent()) {
UniqueAccount account = uOpt.get();
objectiveValues.put(balanceLabel, account.getBalance(currency).setScale(0, RoundingMode.HALF_UP).intValue());
Expand Down

0 comments on commit 03b61e6

Please sign in to comment.