Ver código fonte

More work on the new repair system

nossr50 6 anos atrás
pai
commit
121f881d59

+ 9 - 1
src/main/java/com/gmail/nossr50/bukkit/BukkitFactory.java

@@ -1,7 +1,11 @@
 package com.gmail.nossr50.bukkit;
 
 import com.gmail.nossr50.datatypes.items.BukkitMMOItem;
+import com.gmail.nossr50.datatypes.items.MMOItem;
+import com.gmail.nossr50.mcMMO;
 import com.gmail.nossr50.util.nbt.RawNBT;
+import com.sk89q.jnbt.NBTUtils;
+import org.bukkit.inventory.ItemStack;
 
 /**
  * Used to convert or construct platform independent types into Bukkit types
@@ -12,8 +16,12 @@ public class BukkitFactory {
      * Creates a BukkitMMOItem which contains Bukkit implementations for the type MMOItem
      * @return a new BukkitMMOItem
      */
-    public static BukkitMMOItem createBukkitMMOItem(String namespaceKey, int amount, RawNBT rawNBT) {
+    public static MMOItem<?> createItem(String namespaceKey, int amount, RawNBT rawNBT) {
         return new BukkitMMOItem(namespaceKey, amount, rawNBT);
     }
 
+    public static MMOItem<?> createItem(ItemStack itemStack) {
+        return createItem(itemStack.getType().getKey().toString(), itemStack.getAmount(), new RawNBT(mcMMO.getNbtManager().getNBT(itemStack).toString()));
+    }
+
 }

+ 12 - 14
src/main/java/com/gmail/nossr50/datatypes/items/BukkitMMOItem.java

@@ -1,28 +1,27 @@
 package com.gmail.nossr50.datatypes.items;
 
-import com.gmail.nossr50.mcMMO;
 import com.gmail.nossr50.util.nbt.RawNBT;
-import net.minecraft.server.v1_13_R2.NBTTagCompound;
 import org.bukkit.Bukkit;
 import org.bukkit.Material;
 import org.bukkit.inventory.ItemStack;
 import org.bukkit.inventory.meta.ItemMeta;
 
-import java.util.HashSet;
+public class BukkitMMOItem<T extends ItemStack> implements MMOItem<T> {
 
-public class BukkitMMOItem implements MMOItem {
+    private T itemImplementation;
+    private RawNBT rawNBT;
 
-    private ItemStack itemImplementation;
-
-    public BukkitMMOItem(String namespaceKey, int amount) throws NullPointerException {
-        ItemStack itemStack;
+    //Suppressed because the type is always an ItemStack
+    @SuppressWarnings("unchecked")
+    public BukkitMMOItem(String namespaceKey, int amount, RawNBT rawNBT) throws NullPointerException {
+        T itemStack;
         Material material = Material.matchMaterial(namespaceKey);
 
         if(material == null) {
             throw new NullPointerException("Material for user defined item could not be found in the server software.");
         }
 
-        itemStack = new ItemStack(material);
+        itemStack = (T) new ItemStack(material);
 
         //Get default item meta
         ItemMeta itemMeta = Bukkit.getItemFactory().getItemMeta(itemStack.getType());
@@ -34,15 +33,15 @@ public class BukkitMMOItem implements MMOItem {
         itemStack.setAmount(amount);
 
         this.itemImplementation = itemStack;
+        this.rawNBT = rawNBT;
     }
 
-    public BukkitMMOItem(ItemStack itemStack) {
-        NBTTagCompound nbtTagCompound = mcMMO.getNbtManager().getNBT(itemStack);
+    public BukkitMMOItem(T itemStack) {
         this.itemImplementation = itemStack;
     }
 
     @Override
-    public ItemStack getItemImplementation() {
+    public T getItemImplementation() {
         return itemImplementation;
     }
 
@@ -58,8 +57,7 @@ public class BukkitMMOItem implements MMOItem {
 
     @Override
     public RawNBT getRawNBT() {
-        NBTTagCompound nbtTagCompound = mcMMO.getNbtManager().getNBT(itemImplementation);
-        return new RawNBT(nbtTagCompound.toString(), nbtTagCompound);
+        return rawNBT;
     }
 
 }

+ 17 - 8
src/main/java/com/gmail/nossr50/datatypes/items/ItemMatch.java

@@ -4,6 +4,7 @@ import com.gmail.nossr50.mcMMO;
 
 import java.util.HashSet;
 import java.util.Objects;
+import java.util.Set;
 
 /**
  * This type contains data and rules which govern equating equivalency between one item and another in Minecraft.
@@ -26,26 +27,34 @@ 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 ItemMatch<T extends MMOItem<?>> implements DefinedMatch<T> {
+public class ItemMatch<T extends MMOItem<?>> implements DefinedMatch<MMOItem<T>> {
 
     private T item; //Abstract representation of the item
-    private HashSet<ItemMatchProperty> itemMatchProperties; //Item properties used for matching
+    private Set<ItemMatchProperty> itemMatchProperties; //Item properties used for matching
 
     public ItemMatch(T item) {
         this.item = item;
         itemMatchProperties = new HashSet<>();
     }
 
-    public ItemMatch(T item, HashSet<ItemMatchProperty> itemMatchProperties) {
+    public ItemMatch(T item, Set<ItemMatchProperty> itemMatchProperties) {
         this.item = item;
         this.itemMatchProperties = itemMatchProperties;
     }
 
-    public MMOItem getItem() {
+    /**
+     * Gets the item held by this ItemMatch
+     * @return the item used for matching
+     */
+    public T getItem() {
         return item;
     }
 
-    public HashSet<ItemMatchProperty> getItemMatchProperties() {
+    /**
+     * Get the item match properties of this ItemMatch
+     * @return the item match properties
+     */
+    public Set<ItemMatchProperty> getItemMatchProperties() {
         return itemMatchProperties;
     }
 
@@ -56,7 +65,7 @@ public class ItemMatch<T extends MMOItem<?>> implements DefinedMatch<T> {
      * @return true if this item matches the target item
      */
     @Override
-    public boolean isMatch(T otherItem) {
+    public boolean isMatch(MMOItem<T> otherItem) {
         if(hasStrictMatching()) {
             return isStrictMatch(otherItem);
         } else {
@@ -69,7 +78,7 @@ public class ItemMatch<T extends MMOItem<?>> implements DefinedMatch<T> {
      * @param otherItem item to strictly match
      * @return true if the items are considered a match
      */
-    private boolean isStrictMatch(T otherItem) {
+    private boolean isStrictMatch(MMOItem<T> otherItem) {
         for(ItemMatchProperty itemMatchProperty : itemMatchProperties) {
             if(!mcMMO.getNbtManager().hasNBT(otherItem.getRawNBT().getNbtData(), itemMatchProperty.getNbtData())) {
                 return false;
@@ -85,7 +94,7 @@ public class ItemMatch<T extends MMOItem<?>> implements DefinedMatch<T> {
      * @param otherItem item to compare namespace keys with
      * @return true if the items share namespace keys
      */
-    private boolean isUnstrictMatch(MMOItem otherItem) {
+    private boolean isUnstrictMatch(MMOItem<T> otherItem) {
         if(otherItem.getNamespaceKey().equalsIgnoreCase(item.getNamespaceKey())) {
             return true;
         }

+ 4 - 4
src/main/java/com/gmail/nossr50/datatypes/items/ItemWildcards.java

@@ -2,6 +2,7 @@ package com.gmail.nossr50.datatypes.items;
 
 import java.util.HashSet;
 import java.util.Objects;
+import java.util.Set;
 
 /**
  * Represents a series of items that are all acceptable inputs for a behaviour
@@ -20,10 +21,9 @@ import java.util.Objects;
 public class ItemWildcards<T extends MMOItem<?>> {
 
     private String wildcardName;
-    private HashSet<ItemMatch<T>> itemTargets;
+    private Set<ItemMatch<T>> itemTargets;
 
-    public ItemWildcards(String wildcardName, HashSet<ItemMatch<T>> itemTargets) {
-        super();
+    public ItemWildcards(String wildcardName, Set<ItemMatch<T>> itemTargets) {
         this.wildcardName = wildcardName;
         this.itemTargets = itemTargets;
     }
@@ -32,7 +32,7 @@ public class ItemWildcards<T extends MMOItem<?>> {
         return itemTargets.size();
     }
 
-    public HashSet<ItemMatch<T>> getItemTargets() {
+    public Set<ItemMatch<T>> getItemTargets() {
         return itemTargets;
     }
 

+ 3 - 4
src/main/java/com/gmail/nossr50/skills/repair/RepairCost.java

@@ -1,20 +1,19 @@
 package com.gmail.nossr50.skills.repair;
 
-import org.bukkit.inventory.ItemStack;
+import com.gmail.nossr50.datatypes.items.ItemMatch;
 import org.bukkit.inventory.PlayerInventory;
 
 /**
  * Represents one item in a Repair Transaction
  */
-public interface RepairCost {
-
+public interface RepairCost<T extends ItemMatch<?>> {
 
     /**
      * 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
      * @return any compatible payment items if found, can be null
      */
-    ItemStack findPayment(PlayerInventory playerInventory);
+    T findPayment(PlayerInventory playerInventory);
 
     /**
      * Whether or not there is an item that can be used for this repair cost in the player's inventory

+ 10 - 22
src/main/java/com/gmail/nossr50/skills/repair/SimpleRepairCost.java

@@ -1,13 +1,12 @@
 package com.gmail.nossr50.skills.repair;
 
-import com.gmail.nossr50.datatypes.items.BukkitMMOItem;
+import com.gmail.nossr50.bukkit.BukkitFactory;
 import com.gmail.nossr50.datatypes.items.ItemMatch;
+import com.gmail.nossr50.datatypes.items.MMOItem;
 import org.bukkit.Material;
 import org.bukkit.inventory.ItemStack;
 import org.bukkit.inventory.PlayerInventory;
 
-import java.util.List;
-
 /**
  * Implementation of RepairCost
  *
@@ -17,7 +16,7 @@ import java.util.List;
  * 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<T extends ItemMatch> implements RepairCost {
+public class SimpleRepairCost<T extends ItemMatch> implements RepairCost<ItemMatch<?>> {
 
     private T itemMatch;
 
@@ -26,35 +25,24 @@ public class SimpleRepairCost<T extends ItemMatch> implements RepairCost {
     }
 
     @Override
-    public ItemStack findPayment(PlayerInventory playerInventory) {
+    public T findPayment(PlayerInventory playerInventory) {
         for(ItemStack itemStack : playerInventory.getContents()) {
             if(itemStack == null || itemStack.getType() == Material.AIR)
                 continue;
 
-            BukkitMMOItem playerInventoryItem = new BukkitMMOItem(itemStack);
+            MMOItem<T> playerInventoryItem = (MMOItem<T>) BukkitFactory.createItem(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(itemMatch.isMatch(playerInventoryItem)) {
+                //Item is a match
+                return (T) playerInventoryItem;
+            }
 
-            //If the item matches return it
-            if(itemMatch.isMatch(playerInventoryItem))
-                return itemStack;
         }
 
         return null;
     }
 
-    public ItemMatch getItemMatch() {
+    public T getItemMatch() {
         return itemMatch;
     }
 

+ 41 - 0
src/main/java/com/gmail/nossr50/skills/repair/repairables/MultiRepairCost.java

@@ -0,0 +1,41 @@
+package com.gmail.nossr50.skills.repair.repairables;
+
+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.skills.repair.RepairCost;
+import com.gmail.nossr50.skills.repair.SimpleRepairCost;
+import org.bukkit.inventory.PlayerInventory;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class MultiRepairCost<T extends MMOItem<T>, U extends ItemMatch<T>> implements RepairCost<U> {
+
+    //Multiple potential item matches
+    private Set<SimpleRepairCost<U>> repairCostWildcards;
+
+    public MultiRepairCost(ItemWildcards<T> itemWildcards) {
+        repairCostWildcards = new HashSet<>();
+        for(ItemMatch<T> wildcard : itemWildcards.getItemTargets()) {
+            SimpleRepairCost<U> simpleRepairCost = new SimpleRepairCost<U>((U)wildcard);
+            repairCostWildcards.add(simpleRepairCost);
+        }
+    }
+
+    @Override
+    public U findPayment(PlayerInventory playerInventory) {
+        for(SimpleRepairCost simpleRepairCost : repairCostWildcards) {
+            if(simpleRepairCost.findPayment(playerInventory) != null) {
+                return (U) simpleRepairCost.findPayment(playerInventory);
+            }
+        }
+
+        return null;
+    }
+
+    @Override
+    public boolean hasPayment(PlayerInventory playerInventory) {
+        return findPayment(playerInventory) != null;
+    }
+}