Skip to content

Commit

Permalink
Made economy throw exception if it fails to initialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
MuresanSergiu committed Apr 14, 2015
1 parent e1bfff3 commit 354e5e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if (System.env.TRAVIS_TAG != null && !System.env.TRAVIS_TAG.isEmpty()) {
ext.config.build_number = System.env.TRAVIS_TAG.substring(0, System.env.TRAVIS_TAG.length()-1)
ext.config.build_type = System.env.TRAVIS_TAG.substring(System.env.TRAVIS_TAG.length()-1)
} else {
ext.config.build_number = "0.0"
ext.config.build_number = "1.5"
ext.config.build_type = "r"
}

Expand Down
37 changes: 12 additions & 25 deletions src/main/java/mytown/core/utils/economy/Economy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mytown.core.utils.economy;

import cpw.mods.fml.common.Loader;
import mytown.core.MyEssentialsCore;
import mytown.core.bukkit.BukkitCompat;
import mytown.core.utils.ItemUtils;
import mytown.core.utils.PlayerUtils;
Expand All @@ -9,14 +10,14 @@
import net.minecraft.server.MinecraftServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import scala.reflect.runtime.ThreadLocalStorage;

import java.util.UUID;

/**
* @author Joe Goett
*/
public class Economy {
private static Logger log = LogManager.getLogger("MyEconomy");
private String costItemName;
public Class<IEconManager> econManagerClass;

Expand All @@ -26,23 +27,26 @@ public Economy(String costItemName) {
if (MinecraftServer.getServer().getServerModName().contains("cauldron") || MinecraftServer.getServer().getServerModName().contains("mcpc")) {
econManagerClass = BukkitCompat.initEconomy();
}
if(econManagerClass == null)
throw new RuntimeException("Failed to initialize Vault economy!");
} else if(costItemName.equals("$ForgeEssentials")) {
econManagerClass = (Class<IEconManager>) ((Class<?>) ForgeessentialsEconomy.class);
//MyTown.instance.log.info("Enabling ForgeEssentials economy system!");
if(Loader.isModLoaded("ForgeEssentials"))
econManagerClass = (Class<IEconManager>) ((Class<?>) ForgeessentialsEconomy.class);
if(econManagerClass == null)
throw new RuntimeException("Failed to initialize ForgeEssentials economy!");
}
}

public IEconManager economyManagerForUUID(UUID uuid) {
if (econManagerClass == null) {
return null;
}

try {
IEconManager manager = econManagerClass.newInstance();
manager.setUUID(uuid);
return manager;
} catch(Exception ex) {
log.info("Failed to create IEconManager", ex);
MyEssentialsCore.Instance.log.info("Failed to create IEconManager", ex);
}

return null; // Hopefully this doesn't break things...
Expand All @@ -53,7 +57,7 @@ public IEconManager economyManagerForUUID(UUID uuid) {
* Returns false if player doesn't have the money necessary
*/
public boolean takeMoneyFromPlayer(EntityPlayer player, int amount) {
if(costItemName.startsWith("$")) {
if(costItemName.equals("$ForgeEssentials") || costItemName.equals("$Vault")) {
IEconManager eco = economyManagerForUUID(player.getUniqueID());
if (eco == null) return false;
int wallet = eco.getWallet();
Expand All @@ -72,7 +76,7 @@ public boolean takeMoneyFromPlayer(EntityPlayer player, int amount) {
* Returns false if player doesn't have the money necessary
*/
public void giveMoneyToPlayer(EntityPlayer player, int amount) {
if (costItemName.startsWith("$")) {
if (costItemName.equals("$ForgeEssentials") || costItemName.equals("$Vault")) {
IEconManager eco = economyManagerForUUID(player.getUniqueID());
if (eco == null) return;
eco.addToWallet(amount);
Expand All @@ -93,29 +97,12 @@ public String getCurrency(int amount) {
IEconManager manager = econManagerClass.newInstance();
return manager.currency(amount);
} catch(Exception ex) {
log.info("Failed to create IEconManager", ex);
MyEssentialsCore.Instance.log.info("Failed to create IEconManager", ex);
}
return "$";

} else {
return ItemUtils.itemStackFromName(costItemName).getDisplayName() + (amount == 1 ? "" : "s");
}
}

/**
* Returns true if the string matches one of the implemented Economy systems.
* Returns false if the item based Economy is used.
*/
public boolean checkCurrencyString(String currencyString) {
if(currencyString.equals("$ForgeEssentials")) {
if(!Loader.isModLoaded("ForgeEssentials") || econManagerClass == null)
throw new RuntimeException("ForgeEssentials economy failed to initialize.");
return true;
} else if(currencyString.equals("$Vault")) {
if(econManagerClass == null)
throw new RuntimeException("Vault economy failed to initialize");
return true;
}
return false;
}
}

0 comments on commit 354e5e2

Please sign in to comment.