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

Commit

Permalink
Add loan module
Browse files Browse the repository at this point in the history
  • Loading branch information
Flibio committed Nov 24, 2016
1 parent 5e53a5a commit 093b55e
Show file tree
Hide file tree
Showing 16 changed files with 862 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ name=EconomyLite
owner=Flibio
inceptionYear=2015
currentYear=2016
version=2.4.0
version=2.5.0
apiVersion=5.0.0-SNAPSHOT
4 changes: 4 additions & 0 deletions src/main/java/io/github/flibio/economylite/CauseFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public class CauseFactory {
public static Cause create(String reason) {
return Cause.of(NamedCause.of(reason, EconomyLite.getInstance().getPluginContainer()));
}

public static Cause stringCause(String reason) {
return Cause.of(NamedCause.owner("economylite:" + reason.toLowerCase().replaceAll(" ", "_")));
}
}
13 changes: 12 additions & 1 deletion src/main/java/io/github/flibio/economylite/EconomyLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import io.github.flibio.economylite.impl.economy.LiteEconomyService;
import io.github.flibio.economylite.impl.economy.registry.CurrencyRegistryModule;
import io.github.flibio.economylite.modules.Module;
import io.github.flibio.economylite.modules.loan.LoanModule;
import io.github.flibio.economylite.modules.sql.SqlModule;
import io.github.flibio.utils.commands.CommandLoader;
import io.github.flibio.utils.file.ConfigManager;
Expand All @@ -72,6 +73,7 @@
import org.spongepowered.api.service.economy.EconomyService;
import org.spongepowered.api.text.serializer.TextSerializers;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -110,14 +112,18 @@ public void onServerInitialize(GameInitializationEvent event) {
messageStorage = MessageStorage.createInstance(this);
initializeMessage();
// Load modules
List<Module> postInitModules = new ArrayList<>();
getModules().forEach(m -> {
m.initializeConfig();
if (m.isEnabled()) {
if (m.initialize(logger, instance)) {
logger.info("Loaded the " + m.getName() + " module!");
postInitModules.add(m);
} else {
logger.error("Failed to load the " + m.getName() + " module!");
}
} else {
logger.info("The " + m.getName() + " module is disabled!");
}
});
// If the services have not been set, set them to default.
Expand All @@ -129,6 +135,11 @@ public void onServerInitialize(GameInitializationEvent event) {
economyService = new LiteEconomyService();
// Register the Economy Service
game.getServiceManager().setProvider(this, EconomyService.class, economyService);
// Post-initialize modules
postInitModules.forEach(module -> {
module.postInitialization(logger, instance);
});
// Register commands
CommandLoader.registerCommands(this, TextSerializers.FORMATTING_CODE.serialize(messageStorage.getMessage("command.invalidsource")),
new CurrencyCommand(),
new CurrencySetCommand(),
Expand Down Expand Up @@ -260,7 +271,7 @@ public static EconomyLite getInstance() {
}

public static List<Module> getModules() {
return ImmutableList.of(new SqlModule());
return ImmutableList.of(new SqlModule(), new LoanModule());
}

// Setters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*
* This file is part of EconomyLite, licensed under the MIT License (MIT).
*
* Copyright (c) 2015 - 2016 Flibio
* Copyright (c) Contributors
*
* 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 io.github.flibio.economylite.commands.virtual;

import io.github.flibio.economylite.EconomyLite;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*
* This file is part of EconomyLite, licensed under the MIT License (MIT).
*
* Copyright (c) 2015 - 2016 Flibio
* Copyright (c) Contributors
*
* 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 io.github.flibio.economylite.commands.virtual;

import io.github.flibio.economylite.EconomyLite;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ public Map<Currency, BigDecimal> getBalances(Set<Context> contexts) {
public TransactionResult setBalance(Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
// Check if the new balance is in bounds
if (amount.compareTo(BigDecimal.ZERO) == -1 || amount.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT, cause);
}
if (playerService.setBalance(uuid, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT, cause);
}
}

Expand All @@ -130,9 +130,9 @@ public Map<Currency, TransactionResult> resetBalances(Cause cause, Set<Context>
for (Currency currency : currencyService.getCurrencies()) {
if (playerService.accountExists(uuid, currency, cause)) {
if (playerService.setBalance(uuid, getDefaultBalance(currency), currency, cause)) {
results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW));
results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause));
} else {
results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.FAILED, TransactionTypes.WITHDRAW));
results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause));
}
}
}
Expand All @@ -142,9 +142,9 @@ public Map<Currency, TransactionResult> resetBalances(Cause cause, Set<Context>
@Override
public TransactionResult resetBalance(Currency currency, Cause cause, Set<Context> contexts) {
if (playerService.setBalance(uuid, getDefaultBalance(currency), currency, cause)) {
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW);
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
} else {
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.FAILED, TransactionTypes.WITHDRAW);
return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
}
}

Expand All @@ -153,12 +153,12 @@ public TransactionResult deposit(Currency currency, BigDecimal amount, Cause cau
BigDecimal newBal = getBalance(currency).add(amount);
// Check if the new balance is in bounds
if (newBal.compareTo(BigDecimal.ZERO) == -1 || newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT, cause);
}
if (playerService.deposit(uuid, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT);
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT, cause);
}
}

Expand All @@ -167,15 +167,15 @@ public TransactionResult withdraw(Currency currency, BigDecimal amount, Cause ca
BigDecimal newBal = getBalance(currency).subtract(amount);
// Check if the new balance is in bounds
if (newBal.compareTo(BigDecimal.ZERO) == -1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, TransactionTypes.WITHDRAW);
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, TransactionTypes.WITHDRAW, cause);
}
if (newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.WITHDRAW);
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.WITHDRAW, cause);
}
if (playerService.withdraw(uuid, amount, currency, cause)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW);
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.WITHDRAW);
return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
}
}

Expand All @@ -184,17 +184,17 @@ public TransferResult transfer(Account to, Currency currency, BigDecimal amount,
BigDecimal newBal = to.getBalance(currency).add(amount);
// Check if the new balance is in bounds
if (newBal.compareTo(BigDecimal.ZERO) == -1 || newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, to);
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, to, cause);
}
// Check if the account has enough funds
if (amount.compareTo(getBalance(currency)) == 1) {
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, to);
return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, to, cause);
}
if (withdraw(currency, amount, cause).getResult().equals(ResultType.SUCCESS)
&& to.deposit(currency, amount, cause).getResult().equals(ResultType.SUCCESS)) {
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, to);
return resultAndEvent(this, amount, currency, ResultType.SUCCESS, to, cause);
} else {
return resultAndEvent(this, amount, currency, ResultType.FAILED, to);
return resultAndEvent(this, amount, currency, ResultType.FAILED, to, cause);
}
}

Expand All @@ -209,15 +209,15 @@ public Set<Context> getActiveContexts() {
}

private TransactionResult resultAndEvent(Account account, BigDecimal amount, Currency currency, ResultType resultType,
TransactionType transactionType) {
TransactionType transactionType, Cause cause) {
TransactionResult result = new LiteTransactionResult(account, amount, currency, resultType, transactionType);
Sponge.getEventManager().post(new LiteEconomyTransactionEvent(result));
Sponge.getEventManager().post(new LiteEconomyTransactionEvent(result, UUID.fromString(account.getIdentifier()), cause));
return result;
}

private TransferResult resultAndEvent(Account account, BigDecimal amount, Currency currency, ResultType resultType, Account toWho) {
private TransferResult resultAndEvent(Account account, BigDecimal amount, Currency currency, ResultType resultType, Account toWho, Cause cause) {
TransferResult result = new LiteTransferResult(account, amount, currency, resultType, toWho);
Sponge.getEventManager().post(new LiteEconomyTransactionEvent(result));
Sponge.getEventManager().post(new LiteEconomyTransactionEvent(result, UUID.fromString(account.getIdentifier()), cause));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,34 @@

import io.github.flibio.economylite.EconomyLite;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.economy.EconomyTransactionEvent;
import org.spongepowered.api.event.impl.AbstractEvent;
import org.spongepowered.api.service.economy.transaction.TransactionResult;

import java.util.UUID;

public class LiteEconomyTransactionEvent extends AbstractEvent implements EconomyTransactionEvent {

private TransactionResult result;
private Cause cause;

public LiteEconomyTransactionEvent(TransactionResult result) {
this.result = result;
}

public LiteEconomyTransactionEvent(TransactionResult result, UUID user, Cause cause) {
this.result = result;
this.cause = cause.with(NamedCause.source(user));
}

@Override
public Cause getCause() {
return Cause.builder().owner(EconomyLite.getInstance()).build();
if (cause != null) {
return cause;
} else {
return Cause.builder().owner(EconomyLite.getInstance()).build();
}
}

@Override
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/io/github/flibio/economylite/modules/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,40 @@
public interface Module {

/**
* Initializes the module.
* Initializes the module. The EconomyService is not available.
*
* @param logger An instance of the plugin's logger.
* @param plugin An instance of the main plugin class.
* @return If the initialization was successful or not.
*/
public boolean initialize(Logger logger, Object plugin);
boolean initialize(Logger logger, Object plugin);

/**
* Post-initialization of the module. The EconomyService is now available.
*
* @param logger An instance of the plugin's logger.
* @param plugin An instance of the main plugin class.
*/
default void postInitialization(Logger logger, Object plugin) {
}

/**
* Sets all default config variables for the module.
*/
public void initializeConfig();
void initializeConfig();

/**
* Gets the name of the module.
*
* @return The name of the module.
*/
public String getName();
String getName();

/**
* Checks if a module is enabled.
*
* @return If the module is enabled or not.
*/
public boolean isEnabled();
boolean isEnabled();

}
Loading

0 comments on commit 093b55e

Please sign in to comment.