diff --git a/src/main/java/com/gmail/nossr50/bukkit/BukkitFactory.java b/src/main/java/com/gmail/nossr50/bukkit/BukkitFactory.java index c7a14586e..50c977b23 100644 --- a/src/main/java/com/gmail/nossr50/bukkit/BukkitFactory.java +++ b/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())); + } + } diff --git a/src/main/java/com/gmail/nossr50/datatypes/items/BukkitMMOItem.java b/src/main/java/com/gmail/nossr50/datatypes/items/BukkitMMOItem.java index 135b3909b..5fe411e8e 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/items/BukkitMMOItem.java +++ b/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 implements MMOItem { -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; } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/items/ItemMatch.java b/src/main/java/com/gmail/nossr50/datatypes/items/ItemMatch.java index 223414ab8..4b8fec75b 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/items/ItemMatch.java +++ b/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> implements DefinedMatch { +public class ItemMatch> implements DefinedMatch> { private T item; //Abstract representation of the item - private HashSet itemMatchProperties; //Item properties used for matching + private Set itemMatchProperties; //Item properties used for matching public ItemMatch(T item) { this.item = item; itemMatchProperties = new HashSet<>(); } - public ItemMatch(T item, HashSet itemMatchProperties) { + public ItemMatch(T item, Set 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 getItemMatchProperties() { + /** + * Get the item match properties of this ItemMatch + * @return the item match properties + */ + public Set getItemMatchProperties() { return itemMatchProperties; } @@ -56,7 +65,7 @@ public class ItemMatch> implements DefinedMatch { * @return true if this item matches the target item */ @Override - public boolean isMatch(T otherItem) { + public boolean isMatch(MMOItem otherItem) { if(hasStrictMatching()) { return isStrictMatch(otherItem); } else { @@ -69,7 +78,7 @@ public class ItemMatch> implements DefinedMatch { * @param otherItem item to strictly match * @return true if the items are considered a match */ - private boolean isStrictMatch(T otherItem) { + private boolean isStrictMatch(MMOItem otherItem) { for(ItemMatchProperty itemMatchProperty : itemMatchProperties) { if(!mcMMO.getNbtManager().hasNBT(otherItem.getRawNBT().getNbtData(), itemMatchProperty.getNbtData())) { return false; @@ -85,7 +94,7 @@ public class ItemMatch> implements DefinedMatch { * @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 otherItem) { if(otherItem.getNamespaceKey().equalsIgnoreCase(item.getNamespaceKey())) { return true; } diff --git a/src/main/java/com/gmail/nossr50/datatypes/items/ItemWildcards.java b/src/main/java/com/gmail/nossr50/datatypes/items/ItemWildcards.java index 51985c956..b2942b801 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/items/ItemWildcards.java +++ b/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> { private String wildcardName; - private HashSet> itemTargets; + private Set> itemTargets; - public ItemWildcards(String wildcardName, HashSet> itemTargets) { - super(); + public ItemWildcards(String wildcardName, Set> itemTargets) { this.wildcardName = wildcardName; this.itemTargets = itemTargets; } @@ -32,7 +32,7 @@ public class ItemWildcards> { return itemTargets.size(); } - public HashSet> getItemTargets() { + public Set> getItemTargets() { return itemTargets; } 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 7b3ca983d..b7d05c4c1 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/RepairCost.java +++ b/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> { /** * 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 diff --git a/src/main/java/com/gmail/nossr50/skills/repair/SimpleRepairCost.java b/src/main/java/com/gmail/nossr50/skills/repair/SimpleRepairCost.java index 4f9e07dac..bfc989c7d 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/SimpleRepairCost.java +++ b/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 implements RepairCost { +public class SimpleRepairCost implements RepairCost> { private T itemMatch; @@ -26,35 +25,24 @@ public class SimpleRepairCost 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 playerInventoryItem = (MMOItem) BukkitFactory.createItem(itemStack); - //TODO: - //TODO: - //TODO: - //TODO: - //TODO: Write the code that compares playerInventoryItem with the - //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; } diff --git a/src/main/java/com/gmail/nossr50/skills/repair/repairables/MultiRepairCost.java b/src/main/java/com/gmail/nossr50/skills/repair/repairables/MultiRepairCost.java new file mode 100644 index 000000000..6b99f0347 --- /dev/null +++ b/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, U extends ItemMatch> implements RepairCost { + + //Multiple potential item matches + private Set> repairCostWildcards; + + public MultiRepairCost(ItemWildcards itemWildcards) { + repairCostWildcards = new HashSet<>(); + for(ItemMatch wildcard : itemWildcards.getItemTargets()) { + SimpleRepairCost simpleRepairCost = new SimpleRepairCost((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; + } +}