More work on the repair rewrite, fleshing out more serializers

This commit is contained in:
nossr50 2019-06-21 18:26:02 -07:00
parent d782d64750
commit d843108164
13 changed files with 129 additions and 161 deletions

View File

@ -52,7 +52,9 @@ import com.gmail.nossr50.config.treasure.FishingTreasureConfig;
import com.gmail.nossr50.config.treasure.HerbalismTreasureConfig;
import com.gmail.nossr50.datatypes.experience.CustomXPPerk;
import com.gmail.nossr50.datatypes.experience.FormulaType;
import com.gmail.nossr50.datatypes.items.BukkitMMOItem;
import com.gmail.nossr50.datatypes.items.ItemWildcards;
import com.gmail.nossr50.datatypes.items.MMOItem;
import com.gmail.nossr50.datatypes.party.PartyFeature;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.properties.DamageProperty;
@ -62,6 +64,7 @@ import com.gmail.nossr50.skills.repair.RepairCost;
import com.gmail.nossr50.skills.repair.RepairTransaction;
import com.gmail.nossr50.skills.repair.repairables.Repairable;
import com.gmail.nossr50.skills.salvage.salvageables.Salvageable;
import com.gmail.nossr50.util.nbt.RawNBT;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializerCollection;
@ -268,6 +271,7 @@ public final class ConfigManager {
customSerializers.registerType(new TypeToken<Material>() {}, new CustomEnumValueSerializer());
customSerializers.registerType(new TypeToken<PartyFeature>() {}, new CustomEnumValueSerializer());
customSerializers.registerType(new TypeToken<FormulaType>() {}, new CustomEnumValueSerializer());
customSerializers.registerType(new TypeToken<MMOItem<?>>() {}, new ItemStackSerializer());
customSerializers.registerType(new TypeToken<Set<?>>() {}, new SetSerializer());
customSerializers.registerType(TypeToken.of(Repairable.class), new RepairableSerializer());
@ -279,10 +283,10 @@ 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(ItemWildcards.class), new RepairWildcardSerializer());
customSerializers.registerType(TypeToken.of(ItemWildcards.class), new ItemWildcardSerializer());
customSerializers.registerType(TypeToken.of(RepairCost.class), new RepairCostSerializer());
customSerializers.registerType(TypeToken.of(RepairTransaction.class), new RepairTransactionSerializer());
customSerializers.registerType(TypeToken.of(RawNBT.class), new RawNBTSerializer());
}
/**

View File

@ -1,5 +1,7 @@
package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.datatypes.items.BukkitMMOItem;
import com.gmail.nossr50.datatypes.items.MMOItem;
import com.gmail.nossr50.mcMMO;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
@ -12,17 +14,14 @@ import org.bukkit.inventory.ItemStack;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
public class ItemStackSerializer implements TypeSerializer<ItemStack> {
public class ItemStackSerializer implements TypeSerializer<MMOItem<?>> {
private static final String ITEM_MINECRAFT_NAME = "Item-Name";
private static final String AMOUNT = "Amount";
private static final String NBT = "NBT";
@Nullable
@Override
public ItemStack deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException {
ItemStack itemStack;
public MMOItem<?> deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException {
String itemIdentifier = value.getNode(ITEM_MINECRAFT_NAME).getValue(TypeToken.of(String.class));
Material itemMatch = Material.matchMaterial(itemIdentifier);
@ -34,37 +33,22 @@ public class ItemStackSerializer implements TypeSerializer<ItemStack> {
ConfigurationNode itemNode = value.getNode(ITEM_MINECRAFT_NAME);
Integer amount;
//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);
amount = itemNode.getNode(AMOUNT).getValue(TypeToken.of(Integer.class));
} else {
itemStack = new ItemStack(itemMatch, 1);
amount = 1;
}
//Init default item meta
itemStack.setItemMeta(Bukkit.getItemFactory().getItemMeta(itemMatch));
if(itemNode.getNode(NBT).getValueType() != ValueType.NULL) {
//TODO: NBT Stuff
}
return itemStack;
//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 new BukkitMMOItem(itemIdentifier, amount);
}
@Override
public void serialize(@NonNull TypeToken<?> type, @Nullable ItemStack obj, @NonNull ConfigurationNode value) throws ObjectMappingException {
public void serialize(@NonNull TypeToken<?> type, @Nullable MMOItem<?> obj, @NonNull ConfigurationNode value) throws ObjectMappingException {
ConfigurationNode itemNode = value.getNode(ITEM_MINECRAFT_NAME);
value.getNode(ITEM_MINECRAFT_NAME).setValue(obj.getType().getKey().toString());
itemNode.getNode(AMOUNT).setValue(obj.getAmount());
//TODO: NBT Stuff
//itemNode.getNode(NBT).setValue()
value.getNode(ITEM_MINECRAFT_NAME).setValue(obj.getNamespaceKey());
itemNode.getNode(AMOUNT).setValue(obj.getItemAmount());
}
}

View File

@ -1,5 +1,6 @@
package com.gmail.nossr50.config.hocon.serializers;
import com.gmail.nossr50.datatypes.items.CustomItemTarget;
import com.gmail.nossr50.datatypes.items.ItemWildcards;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
@ -10,9 +11,10 @@ import org.bukkit.inventory.ItemStack;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.HashSet;
import java.util.Set;
public class RepairWildcardSerializer implements TypeSerializer<ItemWildcards> {
public class ItemWildcardSerializer implements TypeSerializer<ItemWildcards> {
private static final String WILDCARD_IDENTIFIER_NAME = "Wildcard-Identifier-Name";
private static final String MATCHING_ITEMS = "Matching-Items";
@ -24,9 +26,9 @@ public class RepairWildcardSerializer implements TypeSerializer<ItemWildcards> {
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>>() {});
Set<CustomItemTarget> matchCandidates = value.getNode(WILDCARD_IDENTIFIER_NAME).getNode(MATCHING_ITEMS).getValue(new TypeToken<Set<CustomItemTarget>>() {});
return new ItemWildcards(wildCardName, matchCandidates);
return new ItemWildcards(wildCardName, new HashSet<>(matchCandidates));
}
return null;

View File

@ -15,13 +15,13 @@ import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import org.bukkit.inventory.ItemStack;
public class RepairableSerializer implements TypeSerializer<Repairable> {
public static final String REPAIRABLE_ITEM = "Repairable-Item";
public static final String MAXIMUM_DURABILITY = "Maximum-Durability";
public static final String ITEMS_REQUIRED_TO_REPAIR = "Items-Required-To-Repair";
public static final String SKILL_LEVEL_REQUIRED_TO_REPAIR = "Skill-Level-Required-To-Repair";
public static final String BASE_XP_REWARD = "Base-XP-Reward";
public static final String BASE_REPAIR_COUNT = "Base-Repair-Count";
public static final String REQUIRED_PERMISSION_NODE = "Required-Permission-Node";
private static final String REPAIRABLE_ITEM = "Repairable-Item";
private static final String MAXIMUM_DURABILITY = "Maximum-Durability";
private static final String ITEMS_REQUIRED_TO_REPAIR = "Items-Required-To-Repair";
private static final String SKILL_LEVEL_REQUIRED_TO_REPAIR = "Skill-Level-Required-To-Repair";
private static final String BASE_XP_REWARD = "Base-XP-Reward";
private static final String BASE_REPAIR_COUNT = "Base-Repair-Count";
private static final String REQUIRED_PERMISSION_NODE = "Required-Permission-Node";
@Override
public Repairable deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {

View File

@ -1,17 +1,20 @@
package com.gmail.nossr50.datatypes.items;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.nbt.RawNBT;
import net.minecraft.server.v1_13_R2.NBTTagCompound;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class BukkitMMOItem implements MMOItem<ItemStack> {
import java.util.HashSet;
public class BukkitMMOItem implements MMOItem {
private ItemStack itemImplementation;
private RawNBT rawNBT;
public BukkitMMOItem(String namespaceKey, int amount, RawNBT rawNBT) throws NullPointerException {
public BukkitMMOItem(String namespaceKey, int amount) throws NullPointerException {
ItemStack itemStack;
Material material = Material.matchMaterial(namespaceKey);
@ -30,12 +33,12 @@ public class BukkitMMOItem implements MMOItem<ItemStack> {
//Set amount
itemStack.setAmount(amount);
//Set item implementation
this.itemImplementation = itemStack;
}
//Set raw NBT
if(rawNBT != null)
this.rawNBT = rawNBT;
public BukkitMMOItem(ItemStack itemStack) {
NBTTagCompound nbtTagCompound = mcMMO.getNbtManager().getNBT(itemStack);
this.itemImplementation = itemStack;
}
@Override
@ -55,7 +58,8 @@ public class BukkitMMOItem implements MMOItem<ItemStack> {
@Override
public RawNBT getRawNBT() {
return rawNBT;
NBTTagCompound nbtTagCompound = mcMMO.getNbtManager().getNBT(itemImplementation);
return new RawNBT(nbtTagCompound.toString(), nbtTagCompound);
}
}

View File

@ -57,17 +57,52 @@ public class CustomItemTarget implements CustomItemMatching {
*/
@Override
public boolean isMatch(MMOItem otherItem) {
//First compare the basic things that need to match between each item
if(item.equals(otherItem)) {
if(hasStrictMatching()) {
return isStrictMatch(otherItem);
} else {
return isUnstrictMatch(otherItem);
}
}
/**
* Compare this item to another while comparing specific nbt tags for matching values, if all values are found and match it is considered a strict match
* @param otherItem item to strictly match
* @return true if the items are considered a match
*/
private boolean isStrictMatch(MMOItem otherItem) {
for(ItemMatchProperty itemMatchProperty : itemMatchProperties) {
if(!mcMMO.getNbtManager().hasNBT(otherItem.getRawNBT().getNbtData(), itemMatchProperty.getNbtData()))
if(!mcMMO.getNbtManager().hasNBT(otherItem.getRawNBT().getNbtData(), itemMatchProperty.getNbtData())) {
return false;
}
}
//All item match properties were found and matched
return true;
}
/**
* Compare this item to another only by namespace key
* @param otherItem item to compare namespace keys with
* @return true if the items share namespace keys
*/
private boolean isUnstrictMatch(MMOItem otherItem) {
if(otherItem.getNamespaceKey().equalsIgnoreCase(item.getNamespaceKey())) {
return true;
}
//Namespace didn't match reject item
return false;
}
/**
* If this item has strict matching or loose match
* Solely determined by having any item match properties present
* @return true if this item target has strict matching
*/
public boolean hasStrictMatching() {
return itemMatchProperties.size() > 1;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -36,5 +36,4 @@ public interface MMOItem<T> {
* @return the raw NBT if it exists, null otherwise
*/
RawNBT getRawNBT();
}

View File

@ -11,15 +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
* @return any compatible payment items if found
* @return any compatible payment items if found, can be null
*/
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
* Whether or not there is an item that can be used for this repair cost in the player's inventory
* @param playerInventory target player
* @return true if payment is found
*/
boolean hasStrictMatching();
boolean hasPayment(PlayerInventory playerInventory);
}

View File

@ -1,55 +0,0 @@
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
* This one represents a wildcard cost, which can be paid for with multiple items
*
*/
public class RepairCostWildcard implements RepairCost {
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) {
for(ItemStack itemStack : playerInventory.getContents()) {
if(itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}
for(SimpleRepairCost simpleRepairCost : simpleRepairCosts) {
//Attempt to match the item in the inventory to any of the compatible repair items
if(simpleRepairCost.findPayment(playerInventory) != null) {
return simpleRepairCost.findPayment(playerInventory);
}
}
}
return null;
}
public ItemWildcards getItemWildcards() {
return itemWildcards;
}
public void setItemWildcards(ItemWildcards itemWildcards) {
this.itemWildcards = itemWildcards;
}
}

View File

@ -39,6 +39,12 @@ public class RepairTransaction {
}
public boolean canPayRepairCosts(PlayerInventory playerInventory) {
for(RepairCost repairCost : repairCosts) {
if(!repairCost.hasPayment(playerInventory)) {
return false;
}
}
return true;
}
}

View File

@ -1,15 +1,14 @@
package com.gmail.nossr50.skills.repair;
import com.gmail.nossr50.datatypes.items.ItemMatchProperty;
import com.gmail.nossr50.datatypes.items.BukkitMMOItem;
import com.gmail.nossr50.datatypes.items.CustomItemTarget;
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.
*
@ -18,49 +17,30 @@ import java.util.Objects;
*/
public class SimpleRepairCost implements RepairCost {
private ItemMatchProperty itemMatchProperty;
private CustomItemTarget desiredItemTarget;
public SimpleRepairCost(ItemMatchProperty itemMatchProperty) {
this.itemMatchProperty = itemMatchProperty;
public SimpleRepairCost(CustomItemTarget customItemTarget) {
this.desiredItemTarget = customItemTarget;
}
@Override
public ItemStack findPayment(PlayerInventory playerInventory) {
for(ItemStack itemStack : playerInventory.getContents()) {
if(itemStack == null || itemStack.getType() == Material.AIR) {
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)
BukkitMMOItem playerInventoryItem = new BukkitMMOItem(itemStack);
//If the item matches return it
if(desiredItemTarget.isMatch(playerInventoryItem))
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;
public boolean hasPayment(PlayerInventory playerInventory) {
return findPayment(playerInventory) != null;
}
}

View File

@ -3,6 +3,7 @@ package com.gmail.nossr50.util.nbt;
import com.gmail.nossr50.mcMMO;
import net.minecraft.server.v1_13_R2.NBTBase;
import net.minecraft.server.v1_13_R2.NBTList;
import net.minecraft.server.v1_13_R2.NBTTagCompound;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack;
@ -26,9 +27,16 @@ public class NBTManager {
}
}
public RawNBT<?> constructNBT(String nbtString) {
public NBTTagCompound getNBT(ItemStack itemStack) {
Bukkit.broadcastMessage("Checking NBT for "+itemStack.toString());
net.minecraft.server.v1_13_R2.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagCompound rootTag = nmsItemStack.getTag();
return rootTag;
}
public NBTBase constructNBT(String nbtString) {
try {
return new RawNBT<NBTBase>(nbtString, CraftNBTTagConfigSerializer.deserialize(nbtString));
return CraftNBTTagConfigSerializer.deserialize(nbtString);
} catch (Exception e) {
e.printStackTrace();
mcMMO.p.getLogger().severe("mcMMO was unable parse the NBT string from your config! Double check that it is proper NBT!");
@ -37,15 +45,18 @@ public class NBTManager {
}
public void printNBT(ItemStack itemStack) {
Bukkit.broadcastMessage("Checking NBT for "+itemStack.toString());
net.minecraft.server.v1_13_R2.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagCompound rootTag = nmsItemStack.getTag();
for(String key : rootTag.getKeys()) {
for(String key : getNBT(itemStack).getKeys()) {
Bukkit.broadcastMessage("NBT Key found: "+key);
}
}
public boolean hasNBT(NBTTagCompound root, NBTTagCompound nbtData) {
public boolean hasNBT(NBTBase nbt, NBTTagCompound otherNbt) {
if(nbt instanceof NBTList<?>) {
} else {
}
//TODO: Implement this
return false;
}

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.util.nbt;
import net.minecraft.server.v1_13_R2.NBTTagCompound;
import com.gmail.nossr50.mcMMO;
import net.minecraft.server.v1_13_R2.NBTBase;
/**
* A simple class that acts as a container for raw NBT data
@ -14,12 +15,9 @@ import net.minecraft.server.v1_13_R2.NBTTagCompound;
*/
public class RawNBT {
private String nbtContents;
private NBTTagCompound nbtData; //Will be constructed using server internals to make matching NBT easier
public RawNBT(String nbtContents, NBTTagCompound nbtData) {
public RawNBT(String nbtContents) {
this.nbtContents = nbtContents;
this.nbtData = nbtData;
}
public String getNbtContents() {
@ -30,7 +28,7 @@ public class RawNBT {
this.nbtContents = nbtContents;
}
public NBTTagCompound getNbtData() {
return nbtData;
public NBTBase getNbtData() {
return mcMMO.getNbtManager().constructNBT(nbtContents);
}
}