Flesh out RepairCost some more

This commit is contained in:
nossr50
2019-06-18 23:02:20 -07:00
parent 8f04b83954
commit 1e338e6aec
8 changed files with 169 additions and 78 deletions

View File

@@ -1,9 +0,0 @@
/*
package com.gmail.nossr50.skills.repair;
import com.gmail.nossr50.config.AdvancedConfig;
public class ArcaneForging {
}
*/

View File

@@ -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<ItemStack> 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<ItemStack> 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<ItemStack> 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());
}
}

View File

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

View File

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

View File

@@ -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<RepairCost> repairItems;
public RepairTransaction() {
repairItems = new HashSet<>();
}
public void addRepairCost(RepairCost repairCost) {
repairItems.add(repairCost);
}
public HashSet<RepairCost> getRepairItems() {
return repairItems;
}
public void setRepairItems(HashSet<RepairCost> repairItems) {
this.repairItems = repairItems;
}
}