Repair rewrite pt 2 WIP

This commit is contained in:
nossr50 2019-06-17 16:36:37 -07:00
parent 18fd9bbf9b
commit a6a734befe
8 changed files with 105 additions and 18 deletions

View File

@ -37,7 +37,7 @@ import com.gmail.nossr50.config.hocon.skills.mining.ConfigMining;
import com.gmail.nossr50.config.hocon.skills.ranks.ConfigRanks;
import com.gmail.nossr50.config.hocon.skills.ranks.SkillRankProperty;
import com.gmail.nossr50.config.hocon.skills.repair.ConfigRepair;
import com.gmail.nossr50.config.hocon.skills.repair.RepairWildcard;
import com.gmail.nossr50.skills.repair.RepairWildcard;
import com.gmail.nossr50.config.hocon.skills.salvage.ConfigSalvage;
import com.gmail.nossr50.config.hocon.skills.smelting.ConfigSmelting;
import com.gmail.nossr50.config.hocon.skills.swords.ConfigSwords;

View File

@ -1,6 +1,6 @@
package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.config.hocon.skills.repair.RepairWildcard;
import com.gmail.nossr50.skills.repair.RepairWildcard;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ValueType;
@ -10,7 +10,6 @@ import org.bukkit.inventory.ItemStack;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.Set;
public class RepairWildcardSerializer implements TypeSerializer<RepairWildcard> {

View File

@ -1,5 +1,6 @@
package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.skills.repair.RepairTransaction;
import com.gmail.nossr50.skills.repair.repairables.Repairable;
import com.gmail.nossr50.util.nbt.RawNBT;
import com.google.common.reflect.TypeToken;
@ -15,7 +16,7 @@ import java.util.Set;
public class RepairableSerializer implements TypeSerializer<Repairable> {
private static final String ITEM = "Item";
private static final String ITEMS_USED_TO_REPAIR = "Items-Used-To-Repair";
private static final String ITEMS_USED_TO_REPAIR = "Repair-Transaction-Cost";
private static final String OVERRIDE_LEVEL_REQUIREMENT = "Level-Requirement";
private static final String BASE_XP = "XP-Per-Repair";
private static final String FULL_REPAIR_TRANSACTIONS = "Repair-Count";
@ -25,14 +26,14 @@ public class RepairableSerializer implements TypeSerializer<Repairable> {
@Override
public Repairable deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
String itemMaterial = value.getNode(ITEM).getValue(TypeToken.of(String.class));
HashSet<ItemStack> repairItems = new HashSet(value.getNode(ITEMS_USED_TO_REPAIR).getValue(new TypeToken<Set<ItemStack>>() {}));
RepairTransaction repairTransaction = value.getNode(ITEMS_USED_TO_REPAIR).getValue(TypeToken.of(RepairTransaction.class));
Integer minimumLevel = value.getNode(OVERRIDE_LEVEL_REQUIREMENT).getValue(TypeToken.of(Integer.class));
Integer baseXP = value.getNode(BASE_XP).getValue(TypeToken.of(Integer.class));
Integer minRepairs = value.getNode(FULL_REPAIR_TRANSACTIONS).getValue(TypeToken.of(Integer.class));
Boolean strictMatching = value.getNode(STRICT_MATCHING).getValue(TypeToken.of(Boolean.class));
String rawNBT = value.getNode(RAW_NBT).getValue(TypeToken.of(String.class));
return new Repairable(itemMaterial, repairItems, minimumLevel, minRepairs, baseXP, strictMatching, new RawNBT(rawNBT));
return new Repairable(itemMaterial, repairTransaction, minimumLevel, minRepairs, baseXP, strictMatching, new RawNBT(rawNBT));
}
@Override

View File

@ -4,10 +4,10 @@ import com.gmail.nossr50.config.ConfigConstants;
import com.gmail.nossr50.config.hocon.skills.repair.general.ConfigRepairGeneral;
import com.gmail.nossr50.config.hocon.skills.repair.repairmastery.ConfigRepairRepairMastery;
import com.gmail.nossr50.config.hocon.skills.repair.subskills.ConfigRepairSubSkills;
import com.gmail.nossr50.skills.repair.RepairWildcard;
import com.gmail.nossr50.skills.repair.repairables.Repairable;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.*;

View File

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

View File

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

View File

@ -1,9 +1,7 @@
package com.gmail.nossr50.config.hocon.skills.repair;
package com.gmail.nossr50.skills.repair;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

View File

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