Skip to content

Commit

Permalink
Add CurrencyDropCallback in order to allow for modify currency drops.
Browse files Browse the repository at this point in the history
  • Loading branch information
creatorfromhell committed May 24, 2024
1 parent f714ec8 commit c6d64d0
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 4 deletions.
1 change: 1 addition & 0 deletions Core/src/net/tnemc/core/api/callback/TNECallbacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum TNECallbacks {
ACCOUNT_DELETE("account_delete"),
TRANSACTION_PRE("transaction_pre"),
TRANSACTION_POST("transaction_post"),
CURRENCY_DROP("currency_drop"),
CURRENCY_LOAD("currency_load"),
DENOMINATION_LOAD("denomination_load");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package net.tnemc.core.api.callback.currency;
/*
* The New Economy
* Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import net.tnemc.core.currency.Currency;
import net.tnemc.item.AbstractItemStack;
import net.tnemc.plugincore.core.api.callback.Callback;

import java.util.Collection;
import java.util.UUID;

/**
* CurrencyDropCallback
*
* @author creatorfromhell
* @since 0.1.2.0
*/
public class CurrencyDropCallback implements Callback {

private final UUID player;
private Currency currency;

private final Collection<AbstractItemStack<Object>> drops;

public CurrencyDropCallback(UUID player, Currency currency, Collection<AbstractItemStack<Object>> drops) {
this.player = player;
this.currency = currency;
this.drops = drops;
}

/**
* The name of this callback.
*
* @return The name of this callback.
*/
@Override
public String name() {
return "currency_drop";
}

public Collection<AbstractItemStack<Object>> getDrops() {
return drops;
}

public Currency getCurrency() {
return currency;
}

public UUID getPlayer() {
return player;
}

public void setCurrency(Currency currency) {
this.currency = currency;
}
}
16 changes: 13 additions & 3 deletions Core/src/net/tnemc/core/currency/calculations/CalculationData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/

import net.tnemc.core.TNECore;
import net.tnemc.core.api.callback.currency.CurrencyDropCallback;
import net.tnemc.core.api.callback.currency.CurrencyLoadCallback;
import net.tnemc.core.currency.Denomination;
import net.tnemc.core.currency.item.ItemCurrency;
import net.tnemc.core.currency.item.ItemDenomination;
Expand Down Expand Up @@ -117,10 +119,18 @@ public void provideMaterials(final Denomination denomination, Integer amount) {
final Collection<AbstractItemStack<Object>> left = PluginCore.server().calculations().giveItems(Collections.singletonList((AbstractItemStack<Object>)stack), inventory);

if(left.size() > 0) {
contains = contains - left.stream().findFirst().get().amount();
failedDrop = PluginCore.server().calculations().drop(left, player);

dropped = true;
final CurrencyDropCallback currencyDrop = new CurrencyDropCallback(player, currency, left);
if(PluginCore.callbacks().call(currencyDrop)) {
PluginCore.log().error("Cancelled currency drop through callback.", DebugLevel.OFF);

} else {

contains = contains - left.stream().findFirst().get().amount();
failedDrop = PluginCore.server().calculations().drop(left, player);

dropped = true;
}
}

PluginCore.log().debug("Weight: " + denomination.weight() + " - Amount: " + amount, DebugLevel.DETAILED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public BigDecimal calculateHoldings(CalculationData<I> data) {
final int amount = data.getInventoryMaterials().getOrDefault(entry.getKey(), 0);

if(amount > 0) {
holdings = holdings.add(entry.getKey().multiply(new BigDecimal("" + amount)));
holdings = holdings.add(entry.getKey().multiply(new BigDecimal(amount)));
}
}
return holdings;
Expand Down
38 changes: 38 additions & 0 deletions TestCore/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store

0 comments on commit c6d64d0

Please sign in to comment.