From 1e338e6aecaa22b24e0e433339927144afc01273 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Tue, 18 Jun 2019 23:02:20 -0700 Subject: [PATCH] Flesh out RepairCost some more --- .../serializers/RepairCostSerializer.java | 22 ++++++ .../RepairTransactionSerializer.java | 22 ++++++ .../serializers/RepairableSerializer.java | 12 ++-- .../nossr50/skills/repair/ArcaneForging.java | 9 --- .../nossr50/skills/repair/RepairCost.java | 67 +++---------------- .../nossr50/skills/repair/RepairCostItem.java | 51 ++++++++++++++ .../skills/repair/RepairCostWildcard.java | 49 ++++++++++++++ .../skills/repair/RepairTransaction.java | 15 ++++- 8 files changed, 169 insertions(+), 78 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairCostSerializer.java create mode 100644 src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairTransactionSerializer.java delete mode 100644 src/main/java/com/gmail/nossr50/skills/repair/ArcaneForging.java create mode 100644 src/main/java/com/gmail/nossr50/skills/repair/RepairCostItem.java create mode 100644 src/main/java/com/gmail/nossr50/skills/repair/RepairCostWildcard.java diff --git a/src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairCostSerializer.java b/src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairCostSerializer.java new file mode 100644 index 000000000..66393c1e5 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairCostSerializer.java @@ -0,0 +1,22 @@ +package com.gmail.nossr50.config.hocon.serializers; + +import com.gmail.nossr50.skills.repair.RepairCost; +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 { + @Nullable + @Override + public RepairCost deserialize(@NonNull TypeToken type, @NonNull ConfigurationNode value) throws ObjectMappingException { + return null; + } + + @Override + public void serialize(@NonNull TypeToken type, @Nullable RepairCost obj, @NonNull ConfigurationNode value) throws ObjectMappingException { + + } +} diff --git a/src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairTransactionSerializer.java b/src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairTransactionSerializer.java new file mode 100644 index 000000000..8eaaaf34a --- /dev/null +++ b/src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairTransactionSerializer.java @@ -0,0 +1,22 @@ +package com.gmail.nossr50.config.hocon.serializers; + +import com.gmail.nossr50.skills.repair.RepairTransaction; +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 RepairTransactionSerializer implements TypeSerializer { + @Nullable + @Override + public RepairTransaction deserialize(@NonNull TypeToken type, @NonNull ConfigurationNode value) throws ObjectMappingException { + return null; + } + + @Override + public void serialize(@NonNull TypeToken type, @Nullable RepairTransaction obj, @NonNull ConfigurationNode value) throws ObjectMappingException { + + } +} diff --git a/src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairableSerializer.java b/src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairableSerializer.java index f4129f2a2..a6c1ef385 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairableSerializer.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/serializers/RepairableSerializer.java @@ -38,15 +38,13 @@ public class RepairableSerializer implements TypeSerializer { .baseXP(value.getNode(BASE_XP).getValue(TypeToken.of(Integer.class))) .repairCount(value.getNode(REPAIR_COUNT).getValue(TypeToken.of(Integer.class))); - StringBuilder w; - if(value.getNode(MINIMUM_LEVEL).getValueType() != ValueType.NULL) { builder = builder.minLevel(value.getNode(MINIMUM_LEVEL).getValue(TypeToken.of(Integer.class))); } - if(value.getNode(NBT).getValueType() != ValueType.NULL) { - builder = builder.rawNBT(value.getNode(NBT).getValue(TypeToken.of(RawNBT.class))); - } +// if(value.getNode(NBT).getValueType() != ValueType.NULL) { +// builder = builder.rawNBT(value.getNode(NBT).getValue(TypeToken.of(RawNBT.class))); +// } if(value.getNode(PERMISSION).getValueType() != ValueType.NULL) { builder = builder.permissionWrapper(value.getNode(PERMISSION).getValue(TypeToken.of(PermissionWrapper.class))); @@ -67,8 +65,8 @@ public class RepairableSerializer implements TypeSerializer { if(obj.getMinimumLevel() > 0) value.getNode(MINIMUM_LEVEL).setValue(obj.getMinimumLevel()); - if(obj.hasNBT()) - value.getNode(NBT).setValue(obj.getRawNBT()); +// if(obj.hasNBT()) +// value.getNode(NBT).setValue(obj.getRawNBT()); if(obj.hasPermission()) value.getNode(PERMISSION).setValue(obj.getPermissionWrapper()); diff --git a/src/main/java/com/gmail/nossr50/skills/repair/ArcaneForging.java b/src/main/java/com/gmail/nossr50/skills/repair/ArcaneForging.java deleted file mode 100644 index 9b9de716e..000000000 --- a/src/main/java/com/gmail/nossr50/skills/repair/ArcaneForging.java +++ /dev/null @@ -1,9 +0,0 @@ -/* -package com.gmail.nossr50.skills.repair; - -import com.gmail.nossr50.config.AdvancedConfig; - -public class ArcaneForging { - -} -*/ diff --git a/src/main/java/com/gmail/nossr50/skills/repair/RepairCost.java b/src/main/java/com/gmail/nossr50/skills/repair/RepairCost.java index 1dd95d1b5..1b8ea0836 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/RepairCost.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/RepairCost.java @@ -1,70 +1,19 @@ package com.gmail.nossr50.skills.repair; -import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; -import java.util.ArrayList; -import java.util.Objects; - /** * Represents one item in a Repair Transaction */ -public class RepairCost { +public interface RepairCost { - private final ArrayList compatibleRepairItems; + /** + * 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 + * @param strictMatching whether or not to match repair cost items strictly with items in a players inventory + * @return any compatible payment items if found + */ + ItemStack findPayment(PlayerInventory playerInventory, boolean strictMatching); - public RepairCost(ArrayList compatibleRepairItems) { - this.compatibleRepairItems = compatibleRepairItems; - } - - public RepairCost(RepairWildcard repairWildcard) { - compatibleRepairItems = new ArrayList<>(); - compatibleRepairItems.addAll(repairWildcard.getMatchingItems()); - } - - public RepairCost(ItemStack repairItem) { - compatibleRepairItems = new ArrayList<>(); - compatibleRepairItems.add(repairItem); - } - - public ItemStack getCost(PlayerInventory playerInventory, boolean strictMatching) { - for(ItemStack itemStack : playerInventory.getContents()) { - if(itemStack == null || itemStack.getType() == Material.AIR) { - continue; - } - - //Attempt to match the item in the inventory to any of the compatible repair items - for(ItemStack repairItem : compatibleRepairItems) { - if(strictMatching) { - if(itemStack.isSimilar(repairItem)) - return itemStack; - } else { - if(itemStack.getType() == repairItem.getType()) { - return itemStack; - } - } - } - - } - - return null; - } - - public ArrayList getCompatibleRepairItems() { - return compatibleRepairItems; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof RepairCost)) return false; - RepairCost that = (RepairCost) o; - return getCompatibleRepairItems().equals(that.getCompatibleRepairItems()); - } - - @Override - public int hashCode() { - return Objects.hash(getCompatibleRepairItems()); - } } diff --git a/src/main/java/com/gmail/nossr50/skills/repair/RepairCostItem.java b/src/main/java/com/gmail/nossr50/skills/repair/RepairCostItem.java new file mode 100644 index 000000000..963eb4e12 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/skills/repair/RepairCostItem.java @@ -0,0 +1,51 @@ +package com.gmail.nossr50.skills.repair; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; + +import java.util.Objects; + +public class RepairCostItem implements RepairCost { + + private ItemStack repairCostItem; + + public RepairCostItem(ItemStack repairCostItem) { + this.repairCostItem = repairCostItem; + } + + @Override + public ItemStack findPayment(PlayerInventory playerInventory, boolean strictMatching) { + for(ItemStack itemStack : playerInventory.getContents()) { + if(itemStack == null || itemStack.getType() == Material.AIR) { + continue; + } + + //Attempt to match the item in the inventory to any of the compatible repair items + if(strictMatching) { + //TODO: Replace with strict matching code + if(itemStack.isSimilar(repairCostItem)) + return itemStack; + } else { + if(itemStack.getType() == repairCostItem.getType()) { + return itemStack; + } + } + } + + return null; + } + + @Override + public int hashCode() { + return Objects.hash(repairCostItem); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof RepairCostItem)) return false; + RepairCostItem that = (RepairCostItem) o; + return repairCostItem.equals(that.repairCostItem); + } +} diff --git a/src/main/java/com/gmail/nossr50/skills/repair/RepairCostWildcard.java b/src/main/java/com/gmail/nossr50/skills/repair/RepairCostWildcard.java new file mode 100644 index 000000000..27b834bdb --- /dev/null +++ b/src/main/java/com/gmail/nossr50/skills/repair/RepairCostWildcard.java @@ -0,0 +1,49 @@ +package com.gmail.nossr50.skills.repair; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; + +/** + * Represents a piece of a RepairTransaction + * Multiple RepairCost pieces are used to pay for a RepairTransaction + * This one represents a wildcard cost, which can be paid for with multiple items + * + */ +public class RepairCostWildcard implements RepairCost { + + private RepairWildcard repairWildcard; + + @Override + public ItemStack findPayment(PlayerInventory playerInventory, boolean strictMatching) { + for(ItemStack itemStack : playerInventory.getContents()) { + if(itemStack == null || itemStack.getType() == Material.AIR) { + continue; + } + + for(ItemStack wildCardItem : repairWildcard.getMatchingItems()) { + //Attempt to match the item in the inventory to any of the compatible repair items + if(strictMatching) { + //TODO: Replace with strict matching code + if(itemStack.isSimilar(wildCardItem)) + return itemStack; + } else { + if(itemStack.getType() == wildCardItem.getType()) { + return itemStack; + } + } + } + } + + return null; + } + + public RepairWildcard getRepairWildcard() { + return repairWildcard; + } + + public void setRepairWildcard(RepairWildcard repairWildcard) { + this.repairWildcard = repairWildcard; + } + +} diff --git a/src/main/java/com/gmail/nossr50/skills/repair/RepairTransaction.java b/src/main/java/com/gmail/nossr50/skills/repair/RepairTransaction.java index 8da09f767..11b5d28eb 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/RepairTransaction.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/RepairTransaction.java @@ -1,8 +1,6 @@ package com.gmail.nossr50.skills.repair; -import com.gmail.nossr50.skills.repair.RepairCost; - import java.util.HashSet; /** @@ -15,7 +13,18 @@ public class RepairTransaction { private HashSet repairItems; public RepairTransaction() { - + repairItems = new HashSet<>(); } + public void addRepairCost(RepairCost repairCost) { + repairItems.add(repairCost); + } + + public HashSet getRepairItems() { + return repairItems; + } + + public void setRepairItems(HashSet repairItems) { + this.repairItems = repairItems; + } }