More work on the new repair system

This commit is contained in:
nossr50 2019-06-22 19:21:57 -07:00
parent 5c31bdbd49
commit 121f881d59
7 changed files with 96 additions and 53 deletions

View File

@ -1,7 +1,11 @@
package com.gmail.nossr50.bukkit; package com.gmail.nossr50.bukkit;
import com.gmail.nossr50.datatypes.items.BukkitMMOItem; 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.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 * 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 * Creates a BukkitMMOItem which contains Bukkit implementations for the type MMOItem
* @return a new BukkitMMOItem * @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); 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()));
}
} }

View File

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

View File

@ -4,6 +4,7 @@ import com.gmail.nossr50.mcMMO;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects; 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. * 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 * 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 * 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 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) { public ItemMatch(T item) {
this.item = item; this.item = item;
itemMatchProperties = new HashSet<>(); itemMatchProperties = new HashSet<>();
} }
public ItemMatch(T item, HashSet<ItemMatchProperty> itemMatchProperties) { public ItemMatch(T item, Set<ItemMatchProperty> itemMatchProperties) {
this.item = item; this.item = item;
this.itemMatchProperties = itemMatchProperties; this.itemMatchProperties = itemMatchProperties;
} }
public MMOItem getItem() { /**
* Gets the item held by this ItemMatch
* @return the item used for matching
*/
public T getItem() {
return item; 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; return itemMatchProperties;
} }
@ -56,7 +65,7 @@ public class ItemMatch<T extends MMOItem<?>> implements DefinedMatch<T> {
* @return true if this item matches the target item * @return true if this item matches the target item
*/ */
@Override @Override
public boolean isMatch(T otherItem) { public boolean isMatch(MMOItem<T> otherItem) {
if(hasStrictMatching()) { if(hasStrictMatching()) {
return isStrictMatch(otherItem); return isStrictMatch(otherItem);
} else { } else {
@ -69,7 +78,7 @@ public class ItemMatch<T extends MMOItem<?>> implements DefinedMatch<T> {
* @param otherItem item to strictly match * @param otherItem item to strictly match
* @return true if the items are considered a 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) { for(ItemMatchProperty itemMatchProperty : itemMatchProperties) {
if(!mcMMO.getNbtManager().hasNBT(otherItem.getRawNBT().getNbtData(), itemMatchProperty.getNbtData())) { if(!mcMMO.getNbtManager().hasNBT(otherItem.getRawNBT().getNbtData(), itemMatchProperty.getNbtData())) {
return false; return false;
@ -85,7 +94,7 @@ public class ItemMatch<T extends MMOItem<?>> implements DefinedMatch<T> {
* @param otherItem item to compare namespace keys with * @param otherItem item to compare namespace keys with
* @return true if the items share namespace keys * @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())) { if(otherItem.getNamespaceKey().equalsIgnoreCase(item.getNamespaceKey())) {
return true; return true;
} }

View File

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

View File

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

View File

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

View File

@ -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;
}
}