Skip to content

Commit

Permalink
All: Added an economy layer
Browse files Browse the repository at this point in the history
  • Loading branch information
joserobjr committed Nov 10, 2016
1 parent 03e4575 commit 2b943c9
Show file tree
Hide file tree
Showing 16 changed files with 569 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
import br.com.gamemods.minecity.bukkit.command.BukkitPlayer;
import br.com.gamemods.minecity.datasource.api.DataSourceException;
import br.com.gamemods.minecity.datasource.api.unchecked.DBConsumer;
import br.com.gamemods.minecity.vault.VaultEconomy;
import net.md_5.bungee.api.ChatColor;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.*;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.SimplePluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
Expand Down Expand Up @@ -67,6 +70,24 @@ public void onEnable()
getLogger().log(Level.WARNING, "MCStats metrics failed to start", e);
}

try
{
if(getServer().getPluginManager().isPluginEnabled("Vault"))
{
RegisteredServiceProvider<Economy> registration = getServer().getServicesManager().getRegistration(Economy.class);
if(registration != null)
{
Economy economy = registration.getProvider();
if(economy != null)
MineCity.economy = new VaultEconomy(economy);
}
}
}
catch(Error | Exception e)
{
getLogger().severe("Failed to load economy support");
}

BukkitTransformer transformer;
try
{
Expand Down
2 changes: 2 additions & 0 deletions Bukkit/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ database:
user: minecity
pass: password

economy: vault

permissions:
default:
nature:
Expand Down
1 change: 1 addition & 0 deletions Bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: MineCity
version: 1.0.0-SNAPSHOT
main: br.com.gamemods.minecity.bukkit.MineCityPlugin
author: joserobjr
softdepend: [Vault]

commands:
minecity:
Expand Down
4 changes: 4 additions & 0 deletions Core/src/main/java/br/com/gamemods/minecity/MineCity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import br.com.gamemods.minecity.datasource.api.unchecked.DisDBConsumer;
import br.com.gamemods.minecity.datasource.api.unchecked.UncheckedDataSourceException;
import br.com.gamemods.minecity.datasource.sql.SQLSource;
import br.com.gamemods.minecity.economy.EconomyProxy;
import br.com.gamemods.minecity.economy.VoidEconomy;
import br.com.gamemods.minecity.structure.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -29,6 +31,8 @@

public class MineCity
{
@NotNull
public static EconomyProxy economy = new VoidEconomy();
public static final Random RANDOM = new Random();
@NotNull
public final IDataSource dataSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public final class MineCityConfig implements Cloneable
public SimpleFlagHolder defaultReserveFlags = new SimpleFlagHolder();
public boolean defaultNatureDisableCities;
public boolean useTitle = true;
public String economy;

@Override
public MineCityConfig clone()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package br.com.gamemods.minecity.economy;

public class BalanceResult
{
public static final BalanceResult TRUE = new BalanceResult(true);
public static final BalanceResult FALSE = new BalanceResult(false);
public final boolean result;

public static BalanceResult of(boolean bool)
{
if(bool)
return TRUE;
else
return FALSE;
}

public BalanceResult(boolean result)
{
this.result = result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package br.com.gamemods.minecity.economy;

import br.com.gamemods.minecity.api.Async;
import br.com.gamemods.minecity.api.PlayerID;
import br.com.gamemods.minecity.api.world.WorldDim;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface EconomyProxy
{
/**
* Checks if a player has the amount of money in a given world
* @param amount must be positive
* @param world world to be checked
* @return {@code true} if the player has the amount or if the economy does not support world specific balance
* and the player has the amount in global scope.
*/
@Async
BalanceResult has(@NotNull PlayerID player, double amount, @NotNull WorldDim world);

/**
* Checks if a player has an amount of money
* @param amount must be positive
*/
@Async
BalanceResult has(@NotNull PlayerID player, double amount);

/**
* Removes an amount of money from the player
* @param amount must be positive
* @param simulation if true nothing must be changed, the returned value must be only a simulation
* @return requested amount - taken amount. Negative values = took too much, positive values = took too few,
* if the it returns the same value as "amount" so nothing was taken
* @throws IllegalArgumentException If amount is negative
*/
@Async
OperationResult take(@NotNull PlayerID player, double amount, @Nullable BalanceResult balance, boolean simulation) throws IllegalArgumentException;

/**
* Gives money to a player, the money should be added to the player's main account if possible. Item based economies with
* bank support should deposit the value to the player's bank and auto-create it if possible, when it's not possible
* to credit the bank then the implementation is allowed to give the money to the player in any way.
* @param amount must be positive
* @param simulation if true nothing must be changed, the returned value must be only a simulation
* @return requested amount - taken amount. Negative values = gave too much, positive values = gave too few,
* if the it returns the same value as "amount" so nothing was given
* @throws IllegalArgumentException If amount is negative
*/
@Async
OperationResult credit(@NotNull PlayerID player, double amount, @Nullable BalanceResult balance, boolean simulation) throws IllegalArgumentException;

/**
* Removes an amount of money from the player
* @param amount must be positive
* @param simulation if true nothing must be changed, the returned value must be only a simulation
* @return requested amount - taken amount. Negative values = took too much, positive values = took too few,
* if the it returns the same value as "amount" so nothing was taken
* @throws IllegalArgumentException If amount is negative
*/
@Async
OperationResult take(@NotNull PlayerID player, double amount, @Nullable BalanceResult balance, @NotNull WorldDim world, boolean simulation) throws IllegalArgumentException;

/**
* Gives money to a player, the money should be added to the player's main account if possible. Item based economies with
* bank support should deposit the value to the player's bank and auto-create it if possible, when it's not possible
* to credit the bank then the implementation is allowed to give the money to the player in any way.
* @param amount must be positive
* @param simulation if true nothing must be changed, the returned value must be only a simulation
* @return requested amount - taken amount. Negative values = gave too much, positive values = gave too few,
* if the it returns the same value as "amount" so nothing was given
* @throws IllegalArgumentException If amount is negative
*/
@Async
OperationResult credit(@NotNull PlayerID player, double amount, @Nullable BalanceResult balance, @NotNull WorldDim world, boolean simulation) throws IllegalArgumentException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package br.com.gamemods.minecity.economy;

public class OperationResult
{
public final boolean success;
public final double amount;
public final String error;

public OperationResult(boolean success, double amount, String error)
{
this.success = success;
this.amount = amount;
this.error = error;
}

public OperationResult(boolean success, double amount)
{
this.success = success;
this.amount = amount;
this.error = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package br.com.gamemods.minecity.economy;

import br.com.gamemods.minecity.api.PlayerID;
import br.com.gamemods.minecity.api.world.WorldDim;
import org.jetbrains.annotations.NotNull;

public final class VoidEconomy implements EconomyProxy
{
@Override
public BalanceResult has(@NotNull PlayerID player, double amount, @NotNull WorldDim world)
{
return BalanceResult.FALSE;
}

@Override
public BalanceResult has(@NotNull PlayerID player, double amount)
{
return BalanceResult.FALSE;
}

@Override
public OperationResult take(@NotNull PlayerID player, double amount, BalanceResult balance, boolean simulation) throws IllegalArgumentException
{
return new OperationResult(false, amount, "Not Supported");
}

@Override
public OperationResult credit(@NotNull PlayerID player, double amount, BalanceResult balance, boolean simulation) throws IllegalArgumentException
{
return new OperationResult(false, amount, "Not Supported");
}

@Override
public OperationResult take(@NotNull PlayerID player, double amount, BalanceResult balance, @NotNull WorldDim world, boolean simulation) throws IllegalArgumentException
{
return take(player, amount, balance, simulation);
}

@Override
public OperationResult credit(@NotNull PlayerID player, double amount, BalanceResult balance, @NotNull WorldDim world, boolean simulation) throws IllegalArgumentException
{
return credit(player, amount, balance, simulation);
}
}
1 change: 1 addition & 0 deletions Forge/1.7.10/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
compile "mysql:mysql-connector-java:5.1.32"
compile "li.cil.oc:OpenComputers:MC1.7.10-1.6.+:api"
compile "net.industrial-craft:industrialcraft-2:2.2.825-experimental:dev"
compile "com.github.GameModsBR:UniversalCoinsServer:-SNAPSHOT"
testCompile fileTree(dir: "run/mods", include: ["*.jar"], exclude:["*Assets*"])
testCompile fileTree(dir: "run/mods/1.7.10", include: ["*.jar"])
}
Expand Down
Loading

0 comments on commit 2b943c9

Please sign in to comment.