Repair rewrite continues

This commit is contained in:
nossr50
2019-06-19 23:28:11 -07:00
parent 458e198fdb
commit b41a30fa26
25 changed files with 511 additions and 155 deletions

View File

@@ -11,9 +11,15 @@ 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
* @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);
ItemStack findPayment(PlayerInventory playerInventory);
/**
* Whether or not this repair cost is strictly matched
* Strict matching compares Items by using metadata and material type
* @return true if the RepairCost uses strict matching
*/
boolean hasStrictMatching();
}

View File

@@ -1,51 +0,0 @@
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);
}
}

View File

@@ -1,9 +1,12 @@
package com.gmail.nossr50.skills.repair;
import com.gmail.nossr50.datatypes.items.ItemWildcards;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.HashSet;
/**
* Represents a piece of a RepairTransaction
* Multiple RepairCost pieces are used to pay for a RepairTransaction
@@ -12,25 +15,28 @@ import org.bukkit.inventory.PlayerInventory;
*/
public class RepairCostWildcard implements RepairCost {
private RepairWildcard repairWildcard;
private HashSet<SimpleRepairCost> simpleRepairCosts;
private ItemWildcards itemWildcards;
public RepairCostWildcard(ItemWildcards itemWildcards) {
this.itemWildcards = itemWildcards;
simpleRepairCosts = new HashSet<>();
for(ItemStack itemStack : )
}
@Override
public ItemStack findPayment(PlayerInventory playerInventory, boolean strictMatching) {
public ItemStack findPayment(PlayerInventory playerInventory) {
for(ItemStack itemStack : playerInventory.getContents()) {
if(itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}
for(ItemStack wildCardItem : repairWildcard.getMatchingItems()) {
for(SimpleRepairCost simpleRepairCost : simpleRepairCosts) {
//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;
}
if(simpleRepairCost.findPayment(playerInventory) != null) {
return simpleRepairCost.findPayment(playerInventory);
}
}
}
@@ -38,12 +44,12 @@ public class RepairCostWildcard implements RepairCost {
return null;
}
public RepairWildcard getRepairWildcard() {
return repairWildcard;
public ItemWildcards getItemWildcards() {
return itemWildcards;
}
public void setRepairWildcard(RepairWildcard repairWildcard) {
this.repairWildcard = repairWildcard;
public void setItemWildcards(ItemWildcards itemWildcards) {
this.itemWildcards = itemWildcards;
}
}

View File

@@ -4,10 +4,18 @@ package com.gmail.nossr50.skills.repair;
import java.util.HashSet;
/**
* Represents a complete repair transaction
* A repair transaction is made up of a multiple RepairCost objects
* A RepairCost object is used to find a matching ItemStack in a players inventory if one exists
* A RepairCost object can be a single item or it can be multiple items representing a range of compatible items to pay that part of the RepairTransaction
* Represents a complete "repair transaction"
*
* I will define a "repair transaction" as such
* - The items used to pay the cost of repairing an item in mcMMO via the Repair Skill
*
* A single "RepairTransaction" is made up of a multiple RepairCost objects
* No two RepairCosts contained within this type can be exact duplicates
*
* A RepairCost is used to find a matching ItemStack in a players inventory if one exists to pay its cost
*
* A RepairCost can be a single item or it can be multiple items representing a range of compatible items
* to pay that part of the RepairTransaction
*/
public class RepairTransaction {
private HashSet<RepairCost> repairItems;

View File

@@ -1,43 +0,0 @@
package com.gmail.nossr50.skills.repair;
import org.bukkit.inventory.ItemStack;
import java.util.Objects;
import java.util.Set;
public class RepairWildcard {
private String wildcardName;
private Set<ItemStack> matchingItems;
public RepairWildcard(String wildcardName, Set<ItemStack> matchingItems) {
this.wildcardName = wildcardName;
this.matchingItems = matchingItems;
}
public Set<ItemStack> getMatchingItems() {
return matchingItems;
}
public void setMatchingItems(Set<ItemStack> matchingItems) {
this.matchingItems = matchingItems;
}
public String getWildcardName() {
return wildcardName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RepairWildcard)) return false;
RepairWildcard that = (RepairWildcard) o;
return getWildcardName().equals(that.getWildcardName()) &&
getMatchingItems().equals(that.getMatchingItems());
}
@Override
public int hashCode() {
return Objects.hash(getWildcardName(), getMatchingItems());
}
}

View File

@@ -0,0 +1,66 @@
package com.gmail.nossr50.skills.repair;
import com.gmail.nossr50.datatypes.items.ItemMatchProperty;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.Objects;
/**
* Implementation of RepairCost
* <p>
* A SimpleRepairCost can be one or more items, any one of which can be used to pay for a RepairTransaction
* If a SimpleRepairCost is more than one item, then only one of the items are required to represent its cost.
*
* 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 {
private ItemMatchProperty itemMatchProperty;
public SimpleRepairCost(ItemMatchProperty itemMatchProperty) {
this.itemMatchProperty = itemMatchProperty;
}
@Override
public ItemStack findPayment(PlayerInventory playerInventory) {
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(hasStrictMatching()) {
//TODO: Replace with strict matching code
if(item)
return itemStack;
} else {
if(itemStack.getType() == itemMatchProperty.getType()) {
return itemStack;
}
}
}
return null;
}
@Override
public int hashCode() {
return Objects.hash(itemMatchProperty);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SimpleRepairCost)) return false;
SimpleRepairCost that = (SimpleRepairCost) o;
return itemMatchProperty.equals(that.itemMatchProperty);
}
@Override
public boolean hasStrictMatching() {
return strictMatching;
}
}

View File

@@ -12,7 +12,7 @@ public class Repairable {
private short maximumDurability;
private RepairTransaction repairTransaction;
private boolean strictMatchingItem = false;
private boolean strictMatchingRepairTransaction = false;
// private boolean strictMatchingRepairTransaction = false;
private int baseXP = 0;
private RawNBT rawNBT;
private int repairCount = 1;
@@ -20,13 +20,13 @@ public class Repairable {
private boolean hasPermission = false;
private boolean hasNBT = false;
public Repairable(ItemStack item, int minimumLevel, short maximumDurability, RepairTransaction repairTransaction, boolean strictMatchingItem, boolean strictMatchingRepairTransaction, int baseXP, int repairCount) {
public Repairable(ItemStack item, int minimumLevel, short maximumDurability, RepairTransaction repairTransaction, boolean strictMatchingItem, int baseXP, int repairCount) {
this.item = item;
this.minimumLevel = minimumLevel;
this.maximumDurability = maximumDurability;
this.repairTransaction = repairTransaction;
this.strictMatchingItem = strictMatchingItem;
this.strictMatchingRepairTransaction = strictMatchingRepairTransaction;
// this.strictMatchingRepairTransaction = strictMatchingRepairTransaction;
this.baseXP = baseXP;
this.repairCount = repairCount;
}
@@ -67,13 +67,13 @@ public class Repairable {
this.strictMatchingItem = strictMatchingItem;
}
public boolean isStrictMatchingRepairTransaction() {
return strictMatchingRepairTransaction;
}
public void setStrictMatchingRepairTransaction(boolean strictMatchingRepairTransaction) {
this.strictMatchingRepairTransaction = strictMatchingRepairTransaction;
}
// public boolean isStrictMatchingRepairTransaction() {
// return strictMatchingRepairTransaction;
// }
//
// public void setStrictMatchingRepairTransaction(boolean strictMatchingRepairTransaction) {
// this.strictMatchingRepairTransaction = strictMatchingRepairTransaction;
// }
public int getBaseXP() {
return baseXP;

View File

@@ -12,7 +12,7 @@ public class RepairableBuilder {
private short maximumDurability;
private RepairTransaction repairTransaction;
private boolean strictMatchingItem = false;
private boolean strictMatchingRepairTransaction = false;
// private boolean strictMatchingRepairTransaction = false;
private int baseXP = 0;
private RawNBT rawNBT;
private int repairCount = 1;
@@ -43,10 +43,10 @@ public class RepairableBuilder {
return this;
}
public RepairableBuilder strictMatchingRepairTransaction(Boolean strictMatchingRepairTransaction) {
this.strictMatchingRepairTransaction = strictMatchingRepairTransaction;
return this;
}
// public RepairableBuilder strictMatchingRepairTransaction(Boolean strictMatchingRepairTransaction) {
// this.strictMatchingRepairTransaction = strictMatchingRepairTransaction;
// return this;
// }
public RepairableBuilder baseXP(Integer baseXP) {
this.baseXP = baseXP;
@@ -74,7 +74,7 @@ public class RepairableBuilder {
private Repairable makeRepairable() {
Repairable repairable = new Repairable(item, minimumLevel, maximumDurability, repairTransaction,
strictMatchingItem, strictMatchingRepairTransaction, baseXP, repairCount);
strictMatchingItem, baseXP, repairCount);
if(permissionWrapper != null) {
repairable.setPermissionWrapper(permissionWrapper);