mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-07-30 19:15:28 +02:00
Repair rewrite pt 2 WIP
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
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 {
|
||||
|
||||
private final ArrayList<ItemStack> compatibleRepairItems;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package com.gmail.nossr50.skills.repair;
|
||||
|
||||
|
||||
import com.gmail.nossr50.skills.repair.RepairCost;
|
||||
|
||||
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
|
||||
*/
|
||||
public class RepairTransaction {
|
||||
private HashSet<RepairCost> repairItems;
|
||||
|
||||
public RepairTransaction() {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
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());
|
||||
}
|
||||
}
|
@@ -1,30 +1,28 @@
|
||||
package com.gmail.nossr50.skills.repair.repairables;
|
||||
|
||||
import com.gmail.nossr50.skills.repair.RepairTransaction;
|
||||
import com.gmail.nossr50.util.nbt.RawNBT;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
public class Repairable {
|
||||
private final Material itemMaterial;
|
||||
private final int minimumLevel;
|
||||
private final short maximumDurability;
|
||||
private HashSet<ItemStack> repairTransaction;
|
||||
private RepairTransaction repairTransaction;
|
||||
private boolean strictMatching;
|
||||
private int baseXP;
|
||||
private RawNBT rawNBT;
|
||||
private int repairCount;
|
||||
|
||||
public Repairable(Material itemMaterial, HashSet<ItemStack> repairTransaction, int minimumLevel, int repairCount, int baseXP, RawNBT rawNBT) {
|
||||
public Repairable(Material itemMaterial, RepairTransaction repairTransaction, int minimumLevel, int repairCount, int baseXP, RawNBT rawNBT) {
|
||||
this(itemMaterial.getKey().getKey(), repairTransaction, minimumLevel, repairCount, baseXP, false, rawNBT);
|
||||
}
|
||||
|
||||
public Repairable(Material itemMaterial, HashSet<ItemStack> repairTransaction, int minimumLevel, int repairCount, int baseXP) {
|
||||
public Repairable(Material itemMaterial, RepairTransaction repairTransaction, int minimumLevel, int repairCount, int baseXP) {
|
||||
this(itemMaterial.getKey().getKey(), repairTransaction, minimumLevel, repairCount, baseXP, false, null);
|
||||
}
|
||||
|
||||
public Repairable(String itemMaterial, HashSet<ItemStack> repairTransaction, int minimumLevel, int repairCount, int baseXP, boolean strictMatching, RawNBT rawNBT) {
|
||||
public Repairable(String itemMaterial, RepairTransaction repairTransaction, int minimumLevel, int repairCount, int baseXP, boolean strictMatching, RawNBT rawNBT) {
|
||||
this.itemMaterial = Material.matchMaterial(itemMaterial);
|
||||
this.minimumLevel = Math.max(0, minimumLevel);
|
||||
|
||||
@@ -48,7 +46,7 @@ public class Repairable {
|
||||
return itemMaterial;
|
||||
}
|
||||
|
||||
public HashSet<ItemStack> getRepairTransaction() {
|
||||
public RepairTransaction getRepairTransaction() {
|
||||
return repairTransaction;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user