Skip to content

Commit

Permalink
Update to use EconomyService
Browse files Browse the repository at this point in the history
  • Loading branch information
hsyyid committed Jan 2, 2016
1 parent 0fe15b7 commit 339d760
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ repositories {
}

dependencies {
compile "org.spongepowered:spongeapi:2.1-SNAPSHOT"
compile files('libs/TotalEconomy.jar')
compile "org.spongepowered:spongeapi:3.0.0"
}

test {
Expand Down
Binary file removed libs/TotalEconomy.jar
Binary file not shown.
63 changes: 42 additions & 21 deletions src/main/java/io/github/hsyyid/adminshop/AdminShop.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.github.hsyyid.adminshop;

import com.erigitic.config.AccountManager;
import com.erigitic.main.TotalEconomy;
import com.google.inject.Inject;
import io.github.hsyyid.adminshop.cmdexecutors.SetItemShopExecutor;
import io.github.hsyyid.adminshop.utils.AdminShopModifierObject;
Expand All @@ -28,29 +26,32 @@
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.block.tileentity.ChangeSignEvent;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.service.economy.EconomyService;
import org.spongepowered.api.service.economy.account.UniqueAccount;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.TeleportHelper;
import org.spongepowered.api.world.World;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Optional;

@Plugin(id = "AdminShop", name = "AdminShop", version = "1.1", dependencies = "required-after:TotalEconomy")
public class AdminShop
{
public static Game game = null;
public static ConfigurationNode config = null;
public static Game game;
public static ConfigurationNode config;
public static EconomyService economyService;
public static ConfigurationLoader<CommentedConfigurationNode> configurationManager;
public static TeleportHelper helper;
public static ArrayList<AdminShopObject> adminShops = new ArrayList<AdminShopObject>();
public static ArrayList<AdminShopObject> buyAdminShops = new ArrayList<AdminShopObject>();
public static ArrayList<AdminShopModifierObject> adminShopModifiers = new ArrayList<AdminShopModifierObject>();
Expand All @@ -76,8 +77,19 @@ public void onServerInit(GameInitializationEvent event)
{
getLogger().info("AdminShop loading..");

Optional<EconomyService> optionalEconomyService = game.getServiceManager().provide(EconomyService.class);

if(!optionalEconomyService.isPresent())
{
getLogger().error("There is no Economy Plugin installed on this Server! This plugin will not work correctly!");
return;
}
else
{
economyService = optionalEconomyService.get();
}

game = Sponge.getGame();
helper = game.getTeleportHelper();

try
{
Expand Down Expand Up @@ -106,7 +118,7 @@ public void onServerInit(GameInitializationEvent event)
.build();

game.getCommandManager().register(this, setItemShopCommandSpec, "setitem");

getLogger().info("-----------------------------");
getLogger().info("AdminShop was made by HassanS6000!");
getLogger().info("Please post all errors on the Sponge Thread or on GitHub!");
Expand Down Expand Up @@ -299,13 +311,18 @@ public void onPlayerInteractBlock(InteractBlockEvent event)
double price = thisShop.getPrice();
String itemName = thisShop.getItemName();

TotalEconomy totalEconomy = (TotalEconomy) game.getPluginManager().getPlugin("TotalEconomy").get().getInstance().get();
AccountManager accountManager = totalEconomy.getAccountManager();
BigDecimal amount = new BigDecimal(price);

if (accountManager.getBalance(player.getUniqueId()).compareTo(amount) >= 0)
if(!economyService.getAccount(player.getUniqueId()).isPresent())
{
accountManager.removeFromBalance(player.getUniqueId(), amount);
economyService.createAccount(player.getUniqueId());
}

UniqueAccount playerAccount = economyService.getAccount(player.getUniqueId()).get();

if (playerAccount.getBalance(economyService.getDefaultCurrency()).compareTo(amount) >= 0)
{
playerAccount.withdraw(economyService.getDefaultCurrency(), amount, Cause.of(this));
player.sendMessage(Text.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "You have just bought " + itemAmount + " " + itemName + " for " + price + " dollars."));

if (thisShop.getMeta() != -1)
Expand Down Expand Up @@ -361,29 +378,33 @@ public void onPlayerInteractBlock(InteractBlockEvent event)
double price = thisBuyShop.getPrice();
String itemName = thisBuyShop.getItemName();

TotalEconomy totalEconomy = (TotalEconomy) game.getPluginManager().getPlugin("TotalEconomy").get().getInstance().get();
AccountManager accountManager = totalEconomy.getAccountManager();
if(!economyService.getAccount(player.getUniqueId()).isPresent())
{
economyService.createAccount(player.getUniqueId());
}

UniqueAccount playerAccount = economyService.getAccount(player.getUniqueId()).get();
BigDecimal amount = new BigDecimal(price);
int quantityInHand = 0;

if (thisBuyShop.getMeta() != -1)
{
int meta = thisBuyShop.getMeta();

if (player.getItemInHand().isPresent() && player.getItemInHand().get().getItem().getName().equals(itemName) && player.getItemInHand().get().getQuantity() == itemAmount && player.getItemInHand().get().toContainer().get(new DataQuery("UnsafeDamage")).isPresent() && (Integer) player.getItemInHand().get().toContainer().get(new DataQuery("UnsafeDamage")).get() == meta)
if (player.getItemInHand().isPresent() && player.getItemInHand().get().getItem().getName().equals(itemName) && player.getItemInHand().get().getQuantity() == itemAmount && player.getItemInHand().get().toContainer().get(DataQuery.of("UnsafeDamage")).isPresent() && (Integer) player.getItemInHand().get().toContainer().get(DataQuery.of("UnsafeDamage")).get() == meta)
{
player.setItemInHand(null);
accountManager.addToBalance(player.getUniqueId(), amount, true);
playerAccount.deposit(economyService.getDefaultCurrency(), amount, Cause.of(this));
player.sendMessage(Text.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "You have just sold " + itemAmount + " " + itemName + " for " + price + " dollars."));
}
else if (player.getItemInHand().isPresent() && player.getItemInHand().get().getItem().getName().equals(itemName) && player.getItemInHand().get().getQuantity() > itemAmount && player.getItemInHand().get().toContainer().get(new DataQuery("UnsafeDamage")).isPresent() && (Integer) player.getItemInHand().get().toContainer().get(new DataQuery("UnsafeDamage")).get() == meta)
else if (player.getItemInHand().isPresent() && player.getItemInHand().get().getItem().getName().equals(itemName) && player.getItemInHand().get().getQuantity() > itemAmount && player.getItemInHand().get().toContainer().get(DataQuery.of("UnsafeDamage")).isPresent() && (Integer) player.getItemInHand().get().toContainer().get(DataQuery.of("UnsafeDamage")).get() == meta)
{
quantityInHand = player.getItemInHand().get().getQuantity() - itemAmount;
player.setItemInHand(game.getRegistry().createBuilder(ItemStack.Builder.class)
.fromItemStack(player.getItemInHand().get())
.quantity(quantityInHand)
.build());
accountManager.addToBalance(player.getUniqueId(), amount, true);
playerAccount.deposit(economyService.getDefaultCurrency(), amount, Cause.of(this));
player.sendMessage(Text.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "You have just sold " + itemAmount + " " + itemName + " for " + price + " dollars."));
}
else
Expand All @@ -396,15 +417,15 @@ else if (player.getItemInHand().isPresent() && player.getItemInHand().get().getI
if (player.getItemInHand().isPresent() && player.getItemInHand().get().getItem().getName().equals(itemName) && player.getItemInHand().get().getQuantity() == itemAmount)
{
player.setItemInHand(null);
accountManager.addToBalance(player.getUniqueId(), amount, true);
playerAccount.deposit(economyService.getDefaultCurrency(), amount, Cause.of(this));
player.sendMessage(Text.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "You have just sold " + itemAmount + " " + itemName + " for " + price + " dollars."));
}
else if (player.getItemInHand().isPresent() && player.getItemInHand().get().getItem().getName().equals(itemName) && player.getItemInHand().get().getQuantity() > itemAmount)
{
quantityInHand = player.getItemInHand().get().getQuantity() - itemAmount;
player.setItemInHand(null);
game.getCommandManager().process(game.getServer().getConsole(), "minecraft:give" + " " + player.getName() + " " + itemName + " " + quantityInHand);
accountManager.addToBalance(player.getUniqueId(), amount, true);
playerAccount.deposit(economyService.getDefaultCurrency(), amount, Cause.of(this));
player.sendMessage(Text.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "You have just sold " + itemAmount + " " + itemName + " for " + price + " dollars."));
}
else
Expand Down

0 comments on commit 339d760

Please sign in to comment.