Lets generify some of this stuff

This commit is contained in:
nossr50 2019-06-22 13:05:28 -07:00
parent d843108164
commit 5c31bdbd49
14 changed files with 139 additions and 74 deletions

View File

@ -52,7 +52,7 @@ import com.gmail.nossr50.config.treasure.FishingTreasureConfig;
import com.gmail.nossr50.config.treasure.HerbalismTreasureConfig;
import com.gmail.nossr50.datatypes.experience.CustomXPPerk;
import com.gmail.nossr50.datatypes.experience.FormulaType;
import com.gmail.nossr50.datatypes.items.BukkitMMOItem;
import com.gmail.nossr50.datatypes.items.ItemMatch;
import com.gmail.nossr50.datatypes.items.ItemWildcards;
import com.gmail.nossr50.datatypes.items.MMOItem;
import com.gmail.nossr50.datatypes.party.PartyFeature;
@ -70,7 +70,6 @@ import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializerCollection;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.ArrayList;
@ -284,9 +283,10 @@ public final class ConfigManager {
customSerializers.registerType(TypeToken.of(PlayerNotificationSettings.class), new PlayerNotificationSerializer());
customSerializers.registerType(TypeToken.of(SoundSetting.class), new SoundSettingSerializer());
customSerializers.registerType(TypeToken.of(ItemWildcards.class), new ItemWildcardSerializer());
customSerializers.registerType(TypeToken.of(RepairCost.class), new RepairCostSerializer());
customSerializers.registerType(TypeToken.of(ItemMatch.class), new CustomItemTargetSerializer());
customSerializers.registerType(TypeToken.of(RepairTransaction.class), new RepairTransactionSerializer());
customSerializers.registerType(TypeToken.of(RawNBT.class), new RawNBTSerializer());
customSerializers.registerType(TypeToken.of(RepairCost.class), new RepairCostSerializer());
}
/**

View File

@ -0,0 +1,43 @@
package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.datatypes.items.ItemMatch;
import com.gmail.nossr50.datatypes.items.ItemMatchProperty;
import com.gmail.nossr50.datatypes.items.MMOItem;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ValueType;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.HashSet;
public class CustomItemTargetSerializer implements TypeSerializer<ItemMatch> {
private static final String ITEM_CONSUMED_FOR_REPAIR = "Item-Consumed-For-Repair";
private static final String NBT_REQUIREMENTS = "NBT-Requirements";
@Nullable
@Override
public ItemMatch deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException {
MMOItem<?> mmoItem = value.getNode(ITEM_CONSUMED_FOR_REPAIR).getValue(new TypeToken<MMOItem<?>>() {});
if(value.getNode(NBT_REQUIREMENTS).getValueType() != ValueType.NULL)
{
HashSet<ItemMatchProperty> itemMatchProperties = value.getNode(NBT_REQUIREMENTS).getValue(new TypeToken<HashSet<ItemMatchProperty>>() {});
return new ItemMatch(mmoItem, itemMatchProperties);
}
return new ItemMatch(mmoItem, null);
}
@Override
public void serialize(@NonNull TypeToken<?> type, @Nullable ItemMatch obj, @NonNull ConfigurationNode value) throws ObjectMappingException {
value.getNode(ITEM_CONSUMED_FOR_REPAIR).setValue(obj.getItem());
if(obj.getItemMatchProperties().size() > 0) {
value.getNode(NBT_REQUIREMENTS).setValue(obj.getItemMatchProperties());
SerializerUtil.addCommentIfCompatible(value.getNode(NBT_REQUIREMENTS), "List optional NBT that is required here, you write it the same way you do in vanilla commands.");
}
}
}

View File

@ -1,13 +1,12 @@
package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.datatypes.items.CustomItemTarget;
import com.gmail.nossr50.datatypes.items.ItemMatch;
import com.gmail.nossr50.datatypes.items.ItemWildcards;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ValueType;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import org.bukkit.inventory.ItemStack;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -26,7 +25,7 @@ public class ItemWildcardSerializer implements TypeSerializer<ItemWildcards> {
String wildCardName = value.getNode(WILDCARD_IDENTIFIER_NAME).getValue(TypeToken.of(String.class));
if(value.getNode(WILDCARD_IDENTIFIER_NAME).getNode(MATCHING_ITEMS).getValueType() != ValueType.NULL) {
Set<CustomItemTarget> matchCandidates = value.getNode(WILDCARD_IDENTIFIER_NAME).getNode(MATCHING_ITEMS).getValue(new TypeToken<Set<CustomItemTarget>>() {});
Set<ItemMatch> matchCandidates = value.getNode(WILDCARD_IDENTIFIER_NAME).getNode(MATCHING_ITEMS).getValue(new TypeToken<Set<ItemMatch>>() {});
return new ItemWildcards(wildCardName, new HashSet<>(matchCandidates));
}

View File

@ -1,22 +1,27 @@
package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.datatypes.items.ItemMatch;
import com.gmail.nossr50.skills.repair.RepairCost;
import com.gmail.nossr50.skills.repair.SimpleRepairCost;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
public class RepairCostSerializer implements TypeSerializer<RepairCost> {
public class RepairCostSerializer implements TypeSerializer<RepairCost<?>> {
private static final String TARGET_ITEM = "Target-Item";
@Nullable
@Override
public RepairCost deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException {
return null;
public RepairCost<?> deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException {
ItemMatch itemMatch = value.getNode(TARGET_ITEM).getValue(TypeToken.of(ItemMatch.class));
return new SimpleRepairCost(itemMatch);
}
@Override
public void serialize(@NonNull TypeToken<?> type, @Nullable RepairCost obj, @NonNull ConfigurationNode value) throws ObjectMappingException {
public void serialize(@NonNull TypeToken<?> type, @Nullable RepairCost<?> obj, @NonNull ConfigurationNode value) throws ObjectMappingException {
value.getNode(TARGET_ITEM).setValue(obj.getRepairCosts());
}
}

View File

@ -1,6 +1,6 @@
package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.datatypes.items.CustomItemTarget;
import com.gmail.nossr50.datatypes.items.ItemMatch;
import com.gmail.nossr50.datatypes.permissions.PermissionWrapper;
import com.gmail.nossr50.skills.repair.RepairTransaction;
import com.gmail.nossr50.skills.repair.repairables.Repairable;
@ -8,11 +8,8 @@ import com.gmail.nossr50.skills.repair.repairables.RepairableBuilder;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ValueType;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.commented.SimpleCommentedConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import org.bukkit.inventory.ItemStack;
public class RepairableSerializer implements TypeSerializer<Repairable> {
private static final String REPAIRABLE_ITEM = "Repairable-Item";
@ -26,11 +23,11 @@ public class RepairableSerializer implements TypeSerializer<Repairable> {
@Override
public Repairable deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
/* Necessary fields */
CustomItemTarget customItemTarget = value.getNode(REPAIRABLE_ITEM).getValue(TypeToken.of(CustomItemTarget.class));
ItemMatch itemMatch = value.getNode(REPAIRABLE_ITEM).getValue(TypeToken.of(ItemMatch.class));
Short maximumDurability = value.getNode(MAXIMUM_DURABILITY).getValue(TypeToken.of(Short.class));
RepairTransaction repairTransaction = value.getNode(ITEMS_REQUIRED_TO_REPAIR).getValue(TypeToken.of(RepairTransaction.class));
RepairableBuilder repairableBuilder = new RepairableBuilder(customItemTarget, maximumDurability, repairTransaction);
RepairableBuilder repairableBuilder = new RepairableBuilder(itemMatch, maximumDurability, repairTransaction);
if(value.getNode(SKILL_LEVEL_REQUIRED_TO_REPAIR).getValueType() != ValueType.NULL) {
repairableBuilder.addMinLevel(value.getNode(SKILL_LEVEL_REQUIRED_TO_REPAIR).getInt());
@ -53,7 +50,7 @@ public class RepairableSerializer implements TypeSerializer<Repairable> {
@Override
public void serialize(TypeToken<?> type, Repairable obj, ConfigurationNode value) {
value.getNode(REPAIRABLE_ITEM).setValue(obj.getCustomItemTarget());
value.getNode(REPAIRABLE_ITEM).setValue(obj.getItemMatch());
value.getNode(MAXIMUM_DURABILITY).setValue(obj.getMaximumDurability());
value.getNode(ITEMS_REQUIRED_TO_REPAIR).setValue(obj.getRepairTransaction());

View File

@ -1,13 +0,0 @@
package com.gmail.nossr50.datatypes.items;
public interface CustomItemMatching {
/**
* Determines whether or not an item matches this one
* Behaviours for matching can vary based on the implementation
* @param otherItem target item to compare itself to
* @return true if this item matches the target item
*/
boolean isMatch(MMOItem otherItem);
}

View File

@ -0,0 +1,19 @@
package com.gmail.nossr50.datatypes.items;
/**
* The interface Defined match.
*
* @param <T> the type parameter
*/
public interface DefinedMatch<T> {
/**
* Determines whether or not this object of type T matches certain criteria of another object of type T
* Behaviours for matching depend solely on the implementation of DefinedMatch
*
* @param other target item to compare itself to
* @return true if this item matches the target item
*/
boolean isMatch(T other);
}

View File

@ -26,17 +26,17 @@ import java.util.Objects;
* 1) Abstract away platform specific implementations of MC Items
* 2) Contain information about an item and which properties of said item that are considered important and thus will be used to equate equivalency to another item when doing comparisons
*/
public class CustomItemTarget implements CustomItemMatching {
public class ItemMatch<T extends MMOItem<?>> implements DefinedMatch<T> {
private MMOItem item; //Abstract representation of the item
private T item; //Abstract representation of the item
private HashSet<ItemMatchProperty> itemMatchProperties; //Item properties used for matching
public CustomItemTarget(MMOItem item) {
public ItemMatch(T item) {
this.item = item;
itemMatchProperties = new HashSet<>();
}
public CustomItemTarget(MMOItem item, HashSet<ItemMatchProperty> itemMatchProperties) {
public ItemMatch(T item, HashSet<ItemMatchProperty> itemMatchProperties) {
this.item = item;
this.itemMatchProperties = itemMatchProperties;
}
@ -56,7 +56,7 @@ public class CustomItemTarget implements CustomItemMatching {
* @return true if this item matches the target item
*/
@Override
public boolean isMatch(MMOItem otherItem) {
public boolean isMatch(T otherItem) {
if(hasStrictMatching()) {
return isStrictMatch(otherItem);
} else {
@ -69,7 +69,7 @@ public class CustomItemTarget implements CustomItemMatching {
* @param otherItem item to strictly match
* @return true if the items are considered a match
*/
private boolean isStrictMatch(MMOItem otherItem) {
private boolean isStrictMatch(T otherItem) {
for(ItemMatchProperty itemMatchProperty : itemMatchProperties) {
if(!mcMMO.getNbtManager().hasNBT(otherItem.getRawNBT().getNbtData(), itemMatchProperty.getNbtData())) {
return false;
@ -106,10 +106,10 @@ public class CustomItemTarget implements CustomItemMatching {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CustomItemTarget)) return false;
CustomItemTarget that = (CustomItemTarget) o;
return getItem().equals(that.getItem()) &&
getItemMatchProperties().equals(that.getItemMatchProperties());
if (!(o instanceof ItemMatch)) return false;
ItemMatch<?> itemMatch = (ItemMatch<?>) o;
return getItem().equals(itemMatch.getItem()) &&
getItemMatchProperties().equals(itemMatch.getItemMatchProperties());
}
@Override

View File

@ -1,7 +1,5 @@
package com.gmail.nossr50.datatypes.items;
import org.bukkit.inventory.ItemStack;
import java.util.HashSet;
import java.util.Objects;
@ -19,12 +17,13 @@ import java.util.Objects;
* to solve a problem with Repair. Given its flexible nature it can be used for many purposes.
*
*/
public class ItemWildcards {
public class ItemWildcards<T extends MMOItem<?>> {
private String wildcardName;
private HashSet<CustomItemTarget> itemTargets;
private HashSet<ItemMatch<T>> itemTargets;
public ItemWildcards(String wildcardName, HashSet<CustomItemTarget> itemTargets) {
public ItemWildcards(String wildcardName, HashSet<ItemMatch<T>> itemTargets) {
super();
this.wildcardName = wildcardName;
this.itemTargets = itemTargets;
}
@ -33,11 +32,11 @@ public class ItemWildcards {
return itemTargets.size();
}
public HashSet<CustomItemTarget> getItemTargets() {
public HashSet<ItemMatch<T>> getItemTargets() {
return itemTargets;
}
public void setItemTargets(HashSet<CustomItemTarget> itemTargets) {
public void setItemTargets(HashSet<ItemMatch<T>> itemTargets) {
this.itemTargets = itemTargets;
}

View File

@ -8,6 +8,7 @@ import org.bukkit.inventory.PlayerInventory;
*/
public interface RepairCost {
/**
* Searches a player inventory for a matching ItemStack that can be used to pay for the repair transaction
* @param playerInventory inventory of player attempting to pay the cost

View File

@ -22,8 +22,8 @@ import java.util.HashSet;
public class RepairTransaction {
private HashSet<RepairCost> repairCosts;
public RepairTransaction() {
repairCosts = new HashSet<>();
public RepairTransaction(HashSet<RepairCost> repairCosts) {
this.repairCosts = repairCosts;
}
public void addRepairCost(RepairCost repairCost) {

View File

@ -1,11 +1,13 @@
package com.gmail.nossr50.skills.repair;
import com.gmail.nossr50.datatypes.items.BukkitMMOItem;
import com.gmail.nossr50.datatypes.items.CustomItemTarget;
import com.gmail.nossr50.datatypes.items.ItemMatch;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.List;
/**
* Implementation of RepairCost
*
@ -15,12 +17,12 @@ import org.bukkit.inventory.PlayerInventory;
* This type is strictly for use with RepairTransaction, which represents the full cost of a Repair.
* @see com.gmail.nossr50.skills.repair.RepairTransaction for more details
*/
public class SimpleRepairCost implements RepairCost {
public class SimpleRepairCost<T extends ItemMatch> implements RepairCost {
private CustomItemTarget desiredItemTarget;
private T itemMatch;
public SimpleRepairCost(CustomItemTarget customItemTarget) {
this.desiredItemTarget = customItemTarget;
public SimpleRepairCost(T customItemTarget) {
this.itemMatch = customItemTarget;
}
@Override
@ -31,14 +33,31 @@ public class SimpleRepairCost implements RepairCost {
BukkitMMOItem playerInventoryItem = new BukkitMMOItem(itemStack);
//TODO:
//TODO:
//TODO:
//TODO:
//TODO: Write the code that compares playerInventoryItem with the <T extends itemMatch>
//TODO:
//TODO:
//TODO:
//TODO:
//TODO:
//TODO:
//TODO:
//If the item matches return it
if(desiredItemTarget.isMatch(playerInventoryItem))
if(itemMatch.isMatch(playerInventoryItem))
return itemStack;
}
return null;
}
public ItemMatch getItemMatch() {
return itemMatch;
}
@Override
public boolean hasPayment(PlayerInventory playerInventory) {
return findPayment(playerInventory) != null;

View File

@ -1,27 +1,25 @@
package com.gmail.nossr50.skills.repair.repairables;
import com.gmail.nossr50.datatypes.items.CustomItemTarget;
import com.gmail.nossr50.datatypes.items.ItemMatch;
import com.gmail.nossr50.datatypes.permissions.PermissionWrapper;
import com.gmail.nossr50.skills.repair.RepairTransaction;
import com.gmail.nossr50.util.nbt.RawNBT;
import org.bukkit.inventory.ItemStack;
public class Repairable {
private int minimumLevel;
private short maximumDurability;
private RepairTransaction repairTransaction;
private int baseXP;
private CustomItemTarget customItemTarget;
private ItemMatch itemMatch;
private int repairCount;
private PermissionWrapper permissionWrapper;
private boolean hasPermission = false;
public Repairable(CustomItemTarget customItemTarget, int minimumLevel, short maximumDurability, RepairTransaction repairTransaction, int baseXP, int repairCount, PermissionWrapper permissionWrapper) {
public Repairable(ItemMatch itemMatch, int minimumLevel, short maximumDurability, RepairTransaction repairTransaction, int baseXP, int repairCount, PermissionWrapper permissionWrapper) {
this.minimumLevel = minimumLevel;
this.maximumDurability = maximumDurability;
this.repairTransaction = repairTransaction;
this.baseXP = baseXP;
this.customItemTarget = customItemTarget;
this.itemMatch = itemMatch;
this.repairCount = repairCount;
this.permissionWrapper = permissionWrapper;
@ -61,12 +59,12 @@ public class Repairable {
this.baseXP = baseXP;
}
public CustomItemTarget getCustomItemTarget() {
return customItemTarget;
public ItemMatch getItemMatch() {
return itemMatch;
}
public void setCustomItemTarget(CustomItemTarget customItemTarget) {
this.customItemTarget = customItemTarget;
public void setItemMatch(ItemMatch itemMatch) {
this.itemMatch = itemMatch;
}
public int getRepairCount() {

View File

@ -1,10 +1,8 @@
package com.gmail.nossr50.skills.repair.repairables;
import com.gmail.nossr50.datatypes.items.CustomItemTarget;
import com.gmail.nossr50.datatypes.items.ItemMatch;
import com.gmail.nossr50.datatypes.permissions.PermissionWrapper;
import com.gmail.nossr50.skills.repair.RepairTransaction;
import com.gmail.nossr50.util.nbt.RawNBT;
import org.bukkit.inventory.ItemStack;
public class RepairableBuilder {
@ -12,12 +10,12 @@ public class RepairableBuilder {
private short maximumDurability;
private RepairTransaction repairTransaction;
private int baseXP;
private CustomItemTarget customItemTarget;
private ItemMatch itemMatch;
private int repairCount;
private PermissionWrapper permissionWrapper;
public RepairableBuilder(CustomItemTarget customItemTarget, Short maximumDurability, RepairTransaction repairTransaction) {
this.customItemTarget = customItemTarget;
public RepairableBuilder(ItemMatch itemMatch, Short maximumDurability, RepairTransaction repairTransaction) {
this.itemMatch = itemMatch;
this.maximumDurability = maximumDurability;
this.repairTransaction = repairTransaction;
}
@ -57,7 +55,7 @@ public class RepairableBuilder {
}
private Repairable makeRepairable() {
Repairable repairable = new Repairable(customItemTarget, minimumLevel, maximumDurability, repairTransaction,
Repairable repairable = new Repairable(itemMatch, minimumLevel, maximumDurability, repairTransaction,
baseXP, repairCount, permissionWrapper);
return repairable;