Skip to content

Commit

Permalink
[Pull Request] Small addon to the gringott's config (#168)
Browse files Browse the repository at this point in the history
* The `custommodeldata-only` _(default false)_ has been introduced to configuration files, describing the type of currency checking—either the traditional method or the alternative one using key-value of material-custommodeldata.

* The `custommodeldata-only` _(default false)_ has been introduced to configuration files, describing the type of currency checking—either the traditional method or the alternative one using key-value of material-custommodeldata.

* The `custommodeldata-only` _(default false)_ has been introduced to configuration files, describing the type of currency checking—either the traditional method or the alternative one using key-value of material-custommodeldata.

* The `custommodeldata-only` _(default false)_ has been introduced to configuration files, describing the type of currency checking—either the traditional method or the alternative one using key-value of material-custommodeldata.
  • Loading branch information
iomatix authored Jan 7, 2024
1 parent 895d89c commit 9ace3f6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/main/java/org/gestern/gringotts/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public enum Configuration {

public boolean dropOverflowingItem = false;

/**
*
* Check only CustomModelData for compatibility with different plugins.
*
*/
public boolean custommodeldataOnly = false;

/**
* Flat tax on every player-to-player transaction. This is a value in currency units.
*/
Expand Down Expand Up @@ -158,6 +165,8 @@ public void readConfig(FileConfiguration savedConfig) {

CONF.dropOverflowingItem = savedConfig.getBoolean("drop-overflowing-item", false);

CONF.custommodeldataOnly = savedConfig.getBoolean("custommodeldata-only", false);

CONF.transactionTaxFlat = savedConfig.getDouble("transactiontax.flat", 0);
CONF.transactionTaxRate = savedConfig.getDouble("transactiontax.rate", 0);

Expand Down
51 changes: 49 additions & 2 deletions src/main/java/org/gestern/gringotts/currency/DenominationKey.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.gestern.gringotts.currency;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.gestern.gringotts.Configuration;

import java.util.HashMap;
import java.util.Objects;

/**
* Hashable information to identify a denomination by it's ItemStack.
Expand All @@ -11,18 +16,33 @@ public class DenominationKey {
* Item type of this denomination.
*/
public final ItemStack type;

public final Material typeMaterial;
public final Boolean hasCustomModelData;
public final Integer typeCustomModelData;

/**
* Create a denomination key based on an item stack.
*
* @param type item type of denomination
* @param type item type of denominations, based on key-value relation creates the Material typeMaterial - Integer typeCustomModelData variables.
*
* @see #typeMaterial
* @see #hasCustomModelData
* @see #typeCustomModelData
*
*/
public DenominationKey(ItemStack type) {
this.type = new ItemStack(type);
this.typeMaterial = type.getType();
this.hasCustomModelData = Objects.requireNonNull(type.getItemMeta()).hasCustomModelData();
if (this.hasCustomModelData) this.typeCustomModelData = type.getItemMeta().getCustomModelData();
else {
this.typeCustomModelData = 0;
type.getItemMeta().setCustomModelData(typeCustomModelData);
}
this.type.setAmount(0);
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -34,6 +54,33 @@ public boolean equals(Object o) {

}

/**
* Checks if its typeMaterial - typeCustomModelData relation is equal with the key variables.
*
* @param o is the object to validate
* @param onlyRelation defines if to check Material and CustomModelData only. If false equals(Object) is being used.
*
* @see #equals(Object)
* @see #type
* @see #typeMaterial
* @see #hasCustomModelData
* @see #typeCustomModelData
*
* @author iomatix
*
*/
public boolean equals(Object o, Boolean onlyRelation) {
if (onlyRelation) {
if (o == null || this.getClass() != o.getClass()) return false;

DenominationKey that = (DenominationKey) o;
if (that.typeMaterial.equals(this.typeMaterial) && that.typeCustomModelData.equals(this.typeCustomModelData)) return true;
}

return this.equals(o);

}

@Override
public int hashCode() {
return type.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.gestern.gringotts.Configuration;

import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -114,7 +115,6 @@ public long getValue(ItemStack stack) {
}

Denomination d = getDenominationOf(stack);

return d != null ? d.getValue() * stack.getAmount() : 0;
}

Expand Down Expand Up @@ -198,6 +198,13 @@ public String format(String formatString, double value) {
*/
private Denomination getDenominationOf(ItemStack stack) {
DenominationKey d = new DenominationKey(stack);
if(Configuration.CONF.custommodeldataOnly) {
for(Denomination dthis : getDenominations()){
if(dthis.getKey().equals(d,true)) {
return dthis;
}
}
}

return denoms.get(d);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ currency:
# - line1
# - line2

# to check CustomModelData and Material data only during validation
custommodeldata-only: false

# tax on /money pay transactions
transactiontax:
flat: 0.0
Expand Down

0 comments on commit 9ace3f6

Please sign in to comment.