Repair rewrite pt 1 - WIP

This commit is contained in:
nossr50 2019-06-17 14:01:44 -07:00
parent bca1298918
commit 18fd9bbf9b
9 changed files with 197 additions and 136 deletions

View File

@ -37,6 +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.config.hocon.skills.salvage.ConfigSalvage;
import com.gmail.nossr50.config.hocon.skills.smelting.ConfigSmelting;
import com.gmail.nossr50.config.hocon.skills.swords.ConfigSwords;
@ -64,6 +65,7 @@ import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializerCollection;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.ArrayList;
@ -275,6 +277,8 @@ public final class ConfigManager {
customSerializers.registerType(TypeToken.of(MaxBonusLevel.class), new MaxBonusLevelSerializer());
customSerializers.registerType(TypeToken.of(PlayerNotificationSettings.class), new PlayerNotificationSerializer());
customSerializers.registerType(TypeToken.of(SoundSetting.class), new SoundSettingSerializer());
customSerializers.registerType(TypeToken.of(ItemStack.class), new ItemStackSerializer());
customSerializers.registerType(TypeToken.of(RepairWildcard.class), new RepairWildcardSerializer());
}
/**

View File

@ -0,0 +1,65 @@
package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.mcMMO;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ValueType;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
public class ItemStackSerializer implements TypeSerializer<ItemStack> {
private static final String ITEM_MINECRAFT_NAME = "Item-Minecraft-Name";
private static final String AMOUNT = "Amount";
private static final String ITEM_LORE = "Item-Lore";
@Nullable
@Override
public ItemStack deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException {
ItemStack itemStack;
String itemIdentifier = value.getNode(ITEM_MINECRAFT_NAME).getValue(TypeToken.of(String.class));
Material itemMatch = Material.matchMaterial(itemIdentifier);
if(itemMatch == null) {
mcMMO.p.getLogger().info("Could not find a match for "+itemIdentifier);
return null;
}
ConfigurationNode itemNode = value.getNode(ITEM_MINECRAFT_NAME);
//Get the amount of items in the stack
if(itemNode.getNode(AMOUNT).getValueType() != ValueType.NULL) {
Integer amount = itemNode.getNode(AMOUNT).getValue(TypeToken.of(Integer.class));
itemStack = new ItemStack(itemMatch, amount);
} else {
itemStack = new ItemStack(itemMatch, 1);
}
//Init default item meta
itemStack.setItemMeta(Bukkit.getItemFactory().getItemMeta(itemMatch));
//Set Lore if it exists
if(itemNode.getNode(ITEM_LORE).getValueType() != ValueType.NULL) {
List<String> lore = itemNode.getNode(ITEM_LORE).getValue(new TypeToken<List<String>>() {});
itemStack.getItemMeta().setLore(lore);
}
return null;
}
@Override
public void serialize(@NonNull TypeToken<?> type, @Nullable ItemStack obj, @NonNull ConfigurationNode value) throws ObjectMappingException {
}
}

View File

@ -3,20 +3,40 @@ package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.config.hocon.skills.repair.RepairWildcard;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ValueType;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
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> {
private static final String WILDCARD_IDENTIFIER_NAME = "Wildcard-Identifier-Name";
private static final String MATCHING_ITEMS = "Matching-Items";
@Nullable
@Override
public RepairWildcard deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException {
String wildCardName = value.getNode(WILDCARD_IDENTIFIER_NAME).getValue(TypeToken.of(String.class));
if(value.getNode(WILDCARD_IDENTIFIER_NAME).getNode(MATCHING_ITEMS).getValueType() != ValueType.NULL) {
Set<ItemStack> matchCandidates = value.getNode(WILDCARD_IDENTIFIER_NAME).getNode(MATCHING_ITEMS).getValue(new TypeToken<Set<ItemStack>>() {});
return new RepairWildcard(wildCardName, matchCandidates);
}
return null;
}
@Override
public void serialize(@NonNull TypeToken<?> type, @Nullable RepairWildcard obj, @NonNull ConfigurationNode value) throws ObjectMappingException {
value.getNode(WILDCARD_IDENTIFIER_NAME).setValue(obj.getWildcardName());
value.getNode(WILDCARD_IDENTIFIER_NAME).getNode(MATCHING_ITEMS).setValue(obj.getMatchingItems());
}
}
}

View File

@ -1,95 +1,48 @@
package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.skills.repair.repairables.Repairable;
import com.gmail.nossr50.util.nbt.RawNBT;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import ninja.leaping.configurate.util.EnumLookup;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
public class RepairableSerializer implements TypeSerializer<Repairable> {
public static final String ITEM = "Item";
public static final String ITEMS_USED_TO_REPAIR = "Items-Used-To-Repair";
public static final String MINIMUM_QUANTITY_USED_TO_REPAIR = "Minimum-Quantity-Used-To-Repair";
public static final String OVERRIDE_LEVEL_REQUIREMENT = "Override-Level-Requirement";
public static final String XP_MULTIPLIER = "XP-Multiplier";
/*
TypeTokens are obtained in two ways
For Raw basic classes:
TypeToken<String> stringTok = TypeToken.of(String.class);
TypeToken<Integer> intTok = TypeToken.of(Integer.class);
For Generics:
TypeToken<List<String>> stringListTok = new TypeToken<List<String>>() {};
Wildcard example:
TypeToken<Map<?, ?>> wildMapTok = new TypeToken<Map<?, ?>>() {};
*/
private static final String ITEM = "Item";
private static final String ITEMS_USED_TO_REPAIR = "Items-Used-To-Repair";
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";
private static final String STRICT_MATCHING = "Use-Strict-Matching";
private static final String RAW_NBT = "Raw-NBT";
@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>>() {}));
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));
/*
CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(WOODEN_SWORD, OAK_PLANKS, 1, 0, .25D));
*/
/* Repairable(Material itemMaterial, Material repairMaterial, int minimumQuantity, int minimumLevel, double xpMultiplier) */
String item = value.getNode(ITEM).getValue(TypeToken.of(String.class));
List<String> repairItems = value.getNode(ITEMS_USED_TO_REPAIR).getValue(new TypeToken<List<String>>() {
});
/*String itemConstant = HOCONUtil.deserializeENUMName(value.getNode("Item").getString());
String repairConstant = HOCONUtil.deserializeENUMName(value.getNode("Item-Used-To-Repair").getString());
Material item = (Material) getEnum(itemConstant, TypeToken.of(Material.class));
Material repairItem = (Material) getEnum(repairConstant, TypeToken.of(Material.class));*/
int minimumQuantity = value.getNode(MINIMUM_QUANTITY_USED_TO_REPAIR).getValue(TypeToken.of(Integer.class));
if(minimumQuantity == 0)
minimumQuantity = -1;
int minimumLevel = value.getNode(OVERRIDE_LEVEL_REQUIREMENT).getValue(TypeToken.of(Integer.class));
double xpMultiplier = value.getNode(XP_MULTIPLIER).getValue(TypeToken.of(Double.class));
return new Repairable(item, repairItems, minimumQuantity, minimumLevel, xpMultiplier);
return new Repairable(itemMaterial, repairItems, minimumLevel, minRepairs, baseXP, strictMatching, new RawNBT(rawNBT));
}
@Override
public void serialize(TypeToken<?> type, Repairable obj, ConfigurationNode value) {
/*value.getNode("Item").setValue(HOCONUtil.serializeENUMName(obj.getItemMaterial().getKey().getKey()));
value.getNode("Item-Used-To-Repair").setValue(HOCONUtil.serializeENUMName(obj.getRepairMaterials().getKey().getKey()));*/
value.getNode(ITEM).setValue(obj.getItemMaterial().getKey().toString());
value.getNode(ITEMS_USED_TO_REPAIR).setValue(obj.getRepairMaterialsRegistryKeys());
value.getNode(MINIMUM_QUANTITY_USED_TO_REPAIR).setValue(obj.getMinimumQuantity());
value.getNode(OVERRIDE_LEVEL_REQUIREMENT).setValue(obj.getMinimumLevel());
value.getNode(XP_MULTIPLIER).setValue(obj.getXpMultiplier());
}
private Enum getEnum(String enumConstant, TypeToken<?> type) throws ObjectMappingException {
//noinspection RedundantCast
Optional<Enum> ret = (Optional) EnumLookup.lookupEnum(type.getRawType().asSubclass(Enum.class),
enumConstant); // XXX: intellij says this cast is optional but it isnt
if (!ret.isPresent()) {
throw new ObjectMappingException("Invalid enum constant provided for " + enumConstant + ": " +
"Expected a value of enum " + type + ", got " + enumConstant);
}
return ret.get();
value.getNode(ITEMS_USED_TO_REPAIR).setValue(obj.getRepairTransaction());
value.getNode(BASE_XP).setValue(obj.getBaseXP());
value.getNode(FULL_REPAIR_TRANSACTIONS).setValue(obj.getRepairCount());
value.getNode(STRICT_MATCHING).setValue(obj.useStrictMatching());
value.getNode(RAW_NBT).setValue(obj.getRawNBT().getNbtContents());
}
}

View File

@ -8,6 +8,7 @@ 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.*;
@ -18,12 +19,21 @@ public class ConfigRepair {
public static final ArrayList<Repairable> CONFIG_REPAIRABLES_DEFAULTS;
public static final HashSet<RepairWildcard> REPAIR_WILDCARDS_DEFAULTS;
public static final Material[] PLANKS = new Material[]{OAK_PLANKS, BIRCH_PLANKS, DARK_OAK_PLANKS, ACACIA_PLANKS, JUNGLE_PLANKS, SPRUCE_PLANKS};
// public static final Material[] PLANKS = new Material[]{OAK_PLANKS, BIRCH_PLANKS, DARK_OAK_PLANKS, ACACIA_PLANKS, JUNGLE_PLANKS, SPRUCE_PLANKS};
static {
REPAIR_WILDCARDS_DEFAULTS = new HashSet<>();
List<ItemStack> planksList = Arrays.asList(new ItemStack[]{new ItemStack(OAK_PLANKS, 1),
new ItemStack(BIRCH_PLANKS, 1), new ItemStack(DARK_OAK_PLANKS, 1),
new ItemStack(ACACIA_PLANKS, 1), new ItemStack(JUNGLE_PLANKS, 1),
new ItemStack(SPRUCE_PLANKS, 1)});
RepairWildcard planksWildCard = new RepairWildcard("Planks", new HashSet<>(planksList));
REPAIR_WILDCARDS_DEFAULTS.add(planksWildCard);
CONFIG_REPAIRABLES_DEFAULTS = new ArrayList<>();
CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(WOODEN_SWORD, Arrays.asList(PLANKS), 1, 0, .25D));
CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(WOODEN_SHOVEL, Arrays.asList(PLANKS), 1, 0, .15D));
CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(WOODEN_SWORD, planksWildCard, 1, 0, .25D));
CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(WOODEN_SHOVEL, planksWildCard, 1, 0, .15D));
CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(WOODEN_PICKAXE, Arrays.asList(PLANKS), 1, 0, .5D));
CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(WOODEN_AXE, Arrays.asList(PLANKS), 1, 0, .5D));
CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(WOODEN_HOE, Arrays.asList(PLANKS), 1, 0, .25D));
@ -70,13 +80,7 @@ public class ConfigRepair {
CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(DIAMOND_LEGGINGS, DIAMOND, 1, 0, 2D));
CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(DIAMOND_BOOTS, DIAMOND, 1, 0, 2D));
REPAIR_WILDCARDS_DEFAULTS = new HashSet<>();
RepairWildcard repairWildcardPlanks = new RepairWildcard("Planks");
List<String> planksList = Arrays.asList(new String[]{OAK_PLANKS.getKey().toString(),
BIRCH_PLANKS.getKey().toString(), DARK_OAK_PLANKS.getKey().toString(), ACACIA_PLANKS.getKey().toString(), JUNGLE_PLANKS.getKey().toString(), SPRUCE_PLANKS.getKey().toString()});
repairWildcardPlanks.addMatchCandidates(planksList);
REPAIR_WILDCARDS_DEFAULTS.add(repairWildcardPlanks);
}
@Setting(value = "General")

View File

@ -1,25 +1,28 @@
package com.gmail.nossr50.config.hocon.skills.repair;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
public class RepairWildcard {
private String wildcardName;
private ArrayList<String> matchCandidates;
private Set<ItemStack> matchingItems;
public RepairWildcard(String wildcardName) {
public RepairWildcard(String wildcardName, Set<ItemStack> matchingItems) {
this.wildcardName = wildcardName;
matchCandidates = new ArrayList<>();
this.matchingItems = matchingItems;
}
public void addMatchCandidates(List<String> arrayList) {
matchCandidates.addAll(arrayList);
public Set<ItemStack> getMatchingItems() {
return matchingItems;
}
public ArrayList<String> getMatchCandidates() {
return matchCandidates;
public void setMatchingItems(Set<ItemStack> matchingItems) {
this.matchingItems = matchingItems;
}
public String getWildcardName() {
@ -32,11 +35,11 @@ public class RepairWildcard {
if (!(o instanceof RepairWildcard)) return false;
RepairWildcard that = (RepairWildcard) o;
return getWildcardName().equals(that.getWildcardName()) &&
Objects.equals(getMatchCandidates(), that.getMatchCandidates());
getMatchingItems().equals(that.getMatchingItems());
}
@Override
public int hashCode() {
return Objects.hash(getWildcardName(), getMatchCandidates());
return Objects.hash(getWildcardName(), getMatchingItems());
}
}

View File

@ -1,71 +1,63 @@
package com.gmail.nossr50.skills.repair.repairables;
import com.gmail.nossr50.datatypes.skills.ItemMaterialCategory;
import com.gmail.nossr50.datatypes.skills.ItemType;
import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.skills.SkillUtils;
import com.gmail.nossr50.util.nbt.RawNBT;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.Collections;
import java.util.List;
import java.util.HashSet;
public class Repairable {
private final Material itemMaterial;
private final List<Material> repairMaterials;
private final int minimumQuantity, minimumLevel;
private final short maximumDurability, baseRepairDurability;
private final ItemType repairItemType;
private final ItemMaterialCategory repairItemMaterialCategory;
private final double xpMultiplier;
private final int minimumLevel;
private final short maximumDurability;
private HashSet<ItemStack> repairTransaction;
private boolean strictMatching;
private int baseXP;
private RawNBT rawNBT;
private int repairCount;
public Repairable(Material itemMaterial, Material repairMaterial, int minimumQuantity, int minimumLevel, double xpMultiplier) {
this(itemMaterial.getKey().getKey(), ItemUtils.getRepairItemMaterials(Collections.singletonList(repairMaterial)), minimumQuantity, minimumLevel, xpMultiplier);
public Repairable(Material itemMaterial, HashSet<ItemStack> repairTransaction, int minimumLevel, int repairCount, int baseXP, RawNBT rawNBT) {
this(itemMaterial.getKey().getKey(), repairTransaction, minimumLevel, repairCount, baseXP, false, rawNBT);
}
public Repairable(Material itemMaterial, List<Material> repairMaterials, int minimumQuantity, int minimumLevel, double xpMultiplier) {
this(itemMaterial.getKey().getKey(), ItemUtils.getRepairItemMaterials(repairMaterials), minimumQuantity, minimumLevel, xpMultiplier);
public Repairable(Material itemMaterial, HashSet<ItemStack> repairTransaction, int minimumLevel, int repairCount, int baseXP) {
this(itemMaterial.getKey().getKey(), repairTransaction, minimumLevel, repairCount, baseXP, false, null);
}
public Repairable(String itemMaterial, List<String> repairMaterials, int minimumQuantity, int minimumLevel, double xpMultiplier) {
public Repairable(String itemMaterial, HashSet<ItemStack> repairTransaction, int minimumLevel, int repairCount, int baseXP, boolean strictMatching, RawNBT rawNBT) {
this.itemMaterial = Material.matchMaterial(itemMaterial);
this.repairMaterials = ItemUtils.matchMaterials(repairMaterials);
this.minimumQuantity = Math.max(1, minimumQuantity);
this.minimumLevel = Math.max(0, minimumLevel);
this.xpMultiplier = Math.max(0, xpMultiplier);
this.maximumDurability = this.itemMaterial.getMaxDurability();
this.baseRepairDurability = (short) (maximumDurability / minimumQuantity);
this.repairCount = repairCount;
this.repairTransaction = repairTransaction;
this.strictMatching = strictMatching;
this.baseXP = baseXP;
this.rawNBT = rawNBT;
}
this.repairItemType = ItemUtils.determineItemType(this.itemMaterial);
this.repairItemMaterialCategory = ItemUtils.determineMaterialType(this.repairMaterials.get(0));
public RawNBT getRawNBT() {
return rawNBT;
}
public int getRepairCount() {
return repairCount;
}
public Material getItemMaterial() {
return itemMaterial;
}
public List<Material> getRepairMaterials() {
return repairMaterials;
public HashSet<ItemStack> getRepairTransaction() {
return repairTransaction;
}
public List<String> getRepairMaterialsRegistryKeys() {
return ItemUtils.getRepairItemMaterials(repairMaterials);
public boolean useStrictMatching() {
return strictMatching;
}
public ItemType getRepairItemType() {
return repairItemType;
}
public ItemMaterialCategory getRepairItemMaterialCategory() {
return repairItemMaterialCategory;
}
public int getMinimumQuantity() {
if(minimumQuantity == -1)
return Math.max(SkillUtils.getRepairAndSalvageQuantities(itemMaterial, repairMaterials), 1);
else
return minimumQuantity;
public int getBaseXP() {
return baseXP;
}
public short getMaximumDurability() {
@ -73,14 +65,10 @@ public class Repairable {
}
public short getBaseRepairDurability() {
return baseRepairDurability;
return (short) (maximumDurability / repairCount);
}
public int getMinimumLevel() {
return minimumLevel;
}
public double getXpMultiplier() {
return xpMultiplier;
}
}

View File

@ -0,0 +1,6 @@
package com.gmail.nossr50.util.nbt;
public class NBTUtils {
}

View File

@ -0,0 +1,18 @@
package com.gmail.nossr50.util.nbt;
public class RawNBT {
String nbtContents;
public RawNBT(String nbtContents) {
this.nbtContents = nbtContents;
}
public String getNbtContents() {
return nbtContents;
}
public void setNbtContents(String nbtContents) {
this.nbtContents = nbtContents;
}
}