Simplify the design of Repairable

This commit is contained in:
nossr50 2019-06-21 15:10:43 -07:00
parent 0050a73be8
commit d782d64750
12 changed files with 186 additions and 145 deletions

View File

@ -7,7 +7,7 @@ import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
public class ConfigAdmin {
@Setting(value = "Admin-Notifications", comment = "Settings related to admin alerts in mcMMO.")
public ConfigAdminNotifications configAdminNotifications = new ConfigAdminNotifications();
private ConfigAdminNotifications configAdminNotifications = new ConfigAdminNotifications();
public boolean isSendAdminNotifications() {
return configAdminNotifications.isSendAdminNotifications();

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.permissions.PermissionWrapper;
import com.gmail.nossr50.skills.repair.RepairTransaction;
import com.gmail.nossr50.skills.repair.repairables.Repairable;
@ -7,63 +8,75 @@ import com.gmail.nossr50.skills.repair.repairables.RepairableBuilder;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ValueType;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.commented.SimpleCommentedConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import org.bukkit.inventory.ItemStack;
public class RepairableSerializer implements TypeSerializer<Repairable> {
private static final String ITEM = "Item";
private static final String BASE_XP = "XP-Per-Repair";
private static final String REPAIR_TRANSACTION = "Repair-Transaction";
private static final String STRICT_MATCH_ITEM = "Strict-Match-Item";
private static final String STRICT_MATCHING_REPAIR_TRANSACTION = "Strict-Matching-Repair-Transaction";
private static final String REPAIR_COUNT = "Repair-Count";
// private static final String NBT = "NBT";
private static final String PERMISSION = "Permission";
private static final String MINIMUM_LEVEL = "Minimum-Level";
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";
@Override
public Repairable deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
ItemStack itemStack = value.getNode(ITEM).getValue(TypeToken.of(ItemStack.class));
RepairableBuilder builder = new RepairableBuilder(itemStack)
.repairTransaction(value.getNode(REPAIR_TRANSACTION).getValue(TypeToken.of(RepairTransaction.class)))
.strictMatchingItem(value.getNode(STRICT_MATCH_ITEM).getValue(TypeToken.of(Boolean.class)))
// .strictMatchingRepairTransaction(value.getNode(STRICT_MATCHING_REPAIR_TRANSACTION).getValue(TypeToken.of(Boolean.class)))
.baseXP(value.getNode(BASE_XP).getValue(TypeToken.of(Integer.class)))
.repairCount(value.getNode(REPAIR_COUNT).getValue(TypeToken.of(Integer.class)));
/* Necessary fields */
CustomItemTarget customItemTarget = value.getNode(REPAIRABLE_ITEM).getValue(TypeToken.of(CustomItemTarget.class));
Short maximumDurability = value.getNode(MAXIMUM_DURABILITY).getValue(TypeToken.of(Short.class));
RepairTransaction repairTransaction = value.getNode(ITEMS_REQUIRED_TO_REPAIR).getValue(TypeToken.of(RepairTransaction.class));
if(value.getNode(MINIMUM_LEVEL).getValueType() != ValueType.NULL) {
builder = builder.minLevel(value.getNode(MINIMUM_LEVEL).getValue(TypeToken.of(Integer.class)));
RepairableBuilder repairableBuilder = new RepairableBuilder(customItemTarget, maximumDurability, repairTransaction);
if(value.getNode(SKILL_LEVEL_REQUIRED_TO_REPAIR).getValueType() != ValueType.NULL) {
repairableBuilder.addMinLevel(value.getNode(SKILL_LEVEL_REQUIRED_TO_REPAIR).getInt());
}
// if(value.getNode(NBT).getValueType() != ValueType.NULL) {
// builder = builder.rawNBT(value.getNode(NBT).getValue(TypeToken.of(RawNBT.class)));
// }
if(value.getNode(PERMISSION).getValueType() != ValueType.NULL) {
builder = builder.permissionWrapper(value.getNode(PERMISSION).getValue(TypeToken.of(PermissionWrapper.class)));
if(value.getNode(BASE_XP_REWARD).getValueType() != ValueType.NULL) {
repairableBuilder.setBaseXP(value.getNode(BASE_XP_REWARD).getInt());
}
return builder.build();
if(value.getNode(BASE_REPAIR_COUNT).getValueType() != ValueType.NULL) {
repairableBuilder.setRepairCount(value.getNode(BASE_REPAIR_COUNT).getInt());
}
if(value.getNode(REQUIRED_PERMISSION_NODE).getValueType() != ValueType.NULL) {
repairableBuilder.addPermissionWrapper(value.getNode(REQUIRED_PERMISSION_NODE).getValue(TypeToken.of(PermissionWrapper.class)));
}
return repairableBuilder.build();
}
@Override
public void serialize(TypeToken<?> type, Repairable obj, ConfigurationNode value) {
value.getNode(ITEM).setValue(obj.getItem());
value.getNode(REPAIR_TRANSACTION).setValue(obj.getRepairTransaction());
value.getNode(STRICT_MATCH_ITEM).setValue(obj.isStrictMatchingItem());
// value.getNode(STRICT_MATCHING_REPAIR_TRANSACTION).setValue(obj.isStrictMatchingRepairTransaction());
value.getNode(BASE_XP).setValue(obj.getBaseXP());
value.getNode(REPAIR_COUNT).setValue(obj.getRepairCount());
value.getNode(REPAIRABLE_ITEM).setValue(obj.getCustomItemTarget());
value.getNode(MAXIMUM_DURABILITY).setValue(obj.getMaximumDurability());
value.getNode(ITEMS_REQUIRED_TO_REPAIR).setValue(obj.getRepairTransaction());
if(obj.getMinimumLevel() > 0)
value.getNode(MINIMUM_LEVEL).setValue(obj.getMinimumLevel());
if(obj.getMinimumLevel() > 0) {
value.getNode(SKILL_LEVEL_REQUIRED_TO_REPAIR).setValue(obj.getMinimumLevel());
}
// if(obj.hasNBT())
// value.getNode(NBT).setValue(obj.getRawNBT());
if(obj.getBaseXP() != 0) {
value.getNode(BASE_XP_REWARD).setValue(obj.getBaseXP());
SerializerUtil.addCommentIfCompatible(value.getNode(BASE_XP_REWARD), "The minimum amount of XP to reward a player when they repair this item.");
}
if(obj.hasPermission())
value.getNode(PERMISSION).setValue(obj.getPermissionWrapper());
if(obj.getRepairCount() != 0) {
value.getNode(BASE_REPAIR_COUNT).setValue(obj.getRepairCount());
SerializerUtil.addCommentIfCompatible(value.getNode(BASE_REPAIR_COUNT), "How many times it should take a player to repair this item from fully damaged to brand new without any skill in Repair." +
"\nThis value is used in calculating how much damage to remove from an item, typically you want this to be at least equal to the number of mats used to craft the item.");
}
if(obj.getPermissionWrapper() != null) {
value.getNode(REQUIRED_PERMISSION_NODE).setValue(obj.getPermissionWrapper());
SerializerUtil.addCommentIfCompatible(value.getNode(REQUIRED_PERMISSION_NODE), "A custom permission node required to repair this item." +
"\nThis setting is optional.");
}
}
}

View File

@ -0,0 +1,19 @@
package com.gmail.nossr50.config.hocon.serializers;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
public class SerializerUtil {
/**
* Adds a comment to a Configuration node if its possible
* @param configurationNode target configuration node
* @param comment desired comment
*/
public static void addCommentIfCompatible(ConfigurationNode configurationNode, String comment) {
if(configurationNode instanceof CommentedConfigurationNode) {
CommentedConfigurationNode node = (CommentedConfigurationNode) configurationNode;
node.setComment(comment);
}
}
}

View File

@ -1,6 +1,9 @@
package com.gmail.nossr50.datatypes.items;
import com.gmail.nossr50.mcMMO;
import java.util.HashSet;
import java.util.Objects;
/**
* This type contains data and rules which govern equating equivalency between one item and another in Minecraft.
@ -56,7 +59,26 @@ public class CustomItemTarget implements CustomItemMatching {
public boolean isMatch(MMOItem otherItem) {
//First compare the basic things that need to match between each item
if(item.equals(otherItem)) {
for(ItemMatchProperty itemMatchProperty : itemMatchProperties) {
if(!mcMMO.getNbtManager().hasNBT(otherItem.getRawNBT().getNbtData(), itemMatchProperty.getNbtData()))
return false;
}
return true;
}
return false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CustomItemTarget)) return false;
CustomItemTarget that = (CustomItemTarget) o;
return getItem().equals(that.getItem()) &&
getItemMatchProperties().equals(that.getItemMatchProperties());
}
@Override
public int hashCode() {
return Objects.hash(getItem(), getItemMatchProperties());
}
}

View File

@ -1,5 +1,9 @@
package com.gmail.nossr50.datatypes.items;
import net.minecraft.server.v1_13_R2.NBTTagCompound;
import java.util.Objects;
/**
* Represents a property of an item which is used in strict matching
* Typically this represents and NBT field and its value
@ -7,20 +11,20 @@ package com.gmail.nossr50.datatypes.items;
*/
public class ItemMatchProperty {
final private Object propertyValue;
final private NBTTagCompound nbtData;
final private String nbtID;
public ItemMatchProperty(Object propertyValue, String nbtID) {
this.propertyValue = propertyValue;
public ItemMatchProperty(String nbtID, NBTTagCompound nbtData) {
this.nbtData = nbtData;
this.nbtID = nbtID;
}
/**
* The expected value for this NBT entry
* The expected value for this NBT
* @return the expected value for this NBT entry
*/
public Object getPropertyValue() {
return propertyValue;
public NBTTagCompound getNbtData() {
return nbtData;
}
/**
@ -30,4 +34,18 @@ public class ItemMatchProperty {
public String getID() {
return nbtID;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ItemMatchProperty)) return false;
ItemMatchProperty that = (ItemMatchProperty) o;
return getNbtData().equals(that.getNbtData()) &&
nbtID.equals(that.nbtID);
}
@Override
public int hashCode() {
return Objects.hash(getNbtData(), nbtID);
}
}

View File

@ -33,7 +33,7 @@ public class ItemWildcards {
return itemTargets.size();
}
public HashSet<ItemStack> getItemTargets() {
public HashSet<CustomItemTarget> getItemTargets() {
return itemTargets;
}

View File

@ -31,6 +31,7 @@ import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManager;
import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManagerFactory;
import com.gmail.nossr50.util.commands.CommandRegistrationManager;
import com.gmail.nossr50.util.experience.FormulaManager;
import com.gmail.nossr50.util.nbt.NBTManager;
import com.gmail.nossr50.util.player.NotificationManager;
import com.gmail.nossr50.util.player.PlayerLevelUtils;
import com.gmail.nossr50.util.player.UserManager;
@ -65,6 +66,7 @@ public class mcMMO extends JavaPlugin {
private static PlayerLevelUtils playerLevelUtils;
private static NotificationManager notificationManager;
private static CommandRegistrationManager commandRegistrationManager;
private static NBTManager nbtManager;
/* File Paths */
private static String mainDirectory;
@ -153,6 +155,8 @@ public class mcMMO extends JavaPlugin {
commandRegistrationManager = new CommandRegistrationManager(this);
commandRegistrationManager.registerCommands();
nbtManager = new NBTManager();
placeStore = ChunkManagerFactory.getChunkManager(); // Get our ChunkletManager
if (getConfigManager().getConfigParty().getPTP().isPtpWorldBasedPermissions()) {
@ -318,6 +322,10 @@ public class mcMMO extends JavaPlugin {
return databaseManager;
}
public static NBTManager getNbtManager() {
return nbtManager;
}
@Deprecated
public static void setDatabaseManager(DatabaseManager newDatabaseManager) {
databaseManager = newDatabaseManager;

View File

@ -1,6 +1,8 @@
package com.gmail.nossr50.skills.repair;
import org.bukkit.inventory.PlayerInventory;
import java.util.HashSet;
/**
@ -18,21 +20,25 @@ import java.util.HashSet;
* to pay that part of the RepairTransaction
*/
public class RepairTransaction {
private HashSet<RepairCost> repairItems;
private HashSet<RepairCost> repairCosts;
public RepairTransaction() {
repairItems = new HashSet<>();
repairCosts = new HashSet<>();
}
public void addRepairCost(RepairCost repairCost) {
repairItems.add(repairCost);
repairCosts.add(repairCost);
}
public HashSet<RepairCost> getRepairItems() {
return repairItems;
public HashSet<RepairCost> getRepairCosts() {
return repairCosts;
}
public void setRepairItems(HashSet<RepairCost> repairItems) {
this.repairItems = repairItems;
public void setRepairCosts(HashSet<RepairCost> repairItems) {
this.repairCosts = repairItems;
}
public boolean canPayRepairCosts(PlayerInventory playerInventory) {
return true;
}
}

View File

@ -1,37 +1,32 @@
package com.gmail.nossr50.skills.repair.repairables;
import com.gmail.nossr50.datatypes.items.CustomItemTarget;
import com.gmail.nossr50.datatypes.permissions.PermissionWrapper;
import com.gmail.nossr50.skills.repair.RepairTransaction;
import com.gmail.nossr50.util.nbt.RawNBT;
import org.bukkit.inventory.ItemStack;
public class Repairable {
private final ItemStack item;
private int minimumLevel = 0;
private int minimumLevel;
private short maximumDurability;
private RepairTransaction repairTransaction;
private boolean strictMatchingItem = false;
// private boolean strictMatchingRepairTransaction = false;
private int baseXP = 0;
private RawNBT rawNBT;
private int repairCount = 1;
private int baseXP;
private CustomItemTarget customItemTarget;
private int repairCount;
private PermissionWrapper permissionWrapper;
private boolean hasPermission = false;
private boolean hasNBT = false;
public Repairable(ItemStack item, int minimumLevel, short maximumDurability, RepairTransaction repairTransaction, boolean strictMatchingItem, int baseXP, int repairCount) {
this.item = item;
public Repairable(CustomItemTarget customItemTarget, int minimumLevel, short maximumDurability, RepairTransaction repairTransaction, int baseXP, int repairCount, PermissionWrapper permissionWrapper) {
this.minimumLevel = minimumLevel;
this.maximumDurability = maximumDurability;
this.repairTransaction = repairTransaction;
this.strictMatchingItem = strictMatchingItem;
// this.strictMatchingRepairTransaction = strictMatchingRepairTransaction;
this.baseXP = baseXP;
this.customItemTarget = customItemTarget;
this.repairCount = repairCount;
}
this.permissionWrapper = permissionWrapper;
public ItemStack getItem() {
return item;
if(permissionWrapper != null)
hasPermission = true;
}
public int getMinimumLevel() {
@ -58,22 +53,6 @@ public class Repairable {
this.repairTransaction = repairTransaction;
}
public boolean isStrictMatchingItem() {
return strictMatchingItem;
}
public void setStrictMatchingItem(boolean strictMatchingItem) {
this.strictMatchingItem = strictMatchingItem;
}
// public boolean isStrictMatchingRepairTransaction() {
// return strictMatchingRepairTransaction;
// }
//
// public void setStrictMatchingRepairTransaction(boolean strictMatchingRepairTransaction) {
// this.strictMatchingRepairTransaction = strictMatchingRepairTransaction;
// }
public int getBaseXP() {
return baseXP;
}
@ -82,13 +61,12 @@ public class Repairable {
this.baseXP = baseXP;
}
public RawNBT getRawNBT() {
return rawNBT;
public CustomItemTarget getCustomItemTarget() {
return customItemTarget;
}
public void setRawNBT(RawNBT rawNBT) {
this.rawNBT = rawNBT;
hasNBT = true;
public void setCustomItemTarget(CustomItemTarget customItemTarget) {
this.customItemTarget = customItemTarget;
}
public int getRepairCount() {
@ -105,10 +83,9 @@ public class Repairable {
public void setPermissionWrapper(PermissionWrapper permissionWrapper) {
this.permissionWrapper = permissionWrapper;
hasPermission = true;
}
public boolean hasPermission() {
public boolean hasPermissionNode() {
return hasPermission;
}
@ -116,11 +93,4 @@ public class Repairable {
this.hasPermission = hasPermission;
}
public boolean hasNBT() {
return hasNBT;
}
public void setHasNBT(boolean hasNBT) {
this.hasNBT = hasNBT;
}
}

View File

@ -1,5 +1,6 @@
package com.gmail.nossr50.skills.repair.repairables;
import com.gmail.nossr50.datatypes.items.CustomItemTarget;
import com.gmail.nossr50.datatypes.permissions.PermissionWrapper;
import com.gmail.nossr50.skills.repair.RepairTransaction;
import com.gmail.nossr50.util.nbt.RawNBT;
@ -7,63 +8,46 @@ import org.bukkit.inventory.ItemStack;
public class RepairableBuilder {
private final ItemStack item;
private int minimumLevel = 0;
private int minimumLevel;
private short maximumDurability;
private RepairTransaction repairTransaction;
private boolean strictMatchingItem = false;
// private boolean strictMatchingRepairTransaction = false;
private int baseXP = 0;
private RawNBT rawNBT;
private int repairCount = 1;
private int baseXP;
private CustomItemTarget customItemTarget;
private int repairCount;
private PermissionWrapper permissionWrapper;
public RepairableBuilder(ItemStack item) {
this.item = item;
this.maximumDurability = item.getType().getMaxDurability();
public RepairableBuilder(CustomItemTarget customItemTarget, Short maximumDurability, RepairTransaction repairTransaction) {
this.customItemTarget = customItemTarget;
this.maximumDurability = maximumDurability;
this.repairTransaction = repairTransaction;
}
public RepairableBuilder minLevel(Integer minimumLevel) {
public RepairableBuilder addMinLevel(Integer minimumLevel) {
this.minimumLevel = minimumLevel;
return this;
}
public RepairableBuilder maximumDurability(Short maximumDurability) {
public RepairableBuilder setMaximumDurability(Short maximumDurability) {
this.maximumDurability = maximumDurability;
return this;
}
public RepairableBuilder repairTransaction(RepairTransaction repairTransaction) {
public RepairableBuilder setRepairTransaction(RepairTransaction repairTransaction) {
this.repairTransaction = repairTransaction;
return this;
}
public RepairableBuilder strictMatchingItem(Boolean strictMatchingItem) {
this.strictMatchingItem = strictMatchingItem;
return this;
}
// public RepairableBuilder strictMatchingRepairTransaction(Boolean strictMatchingRepairTransaction) {
// this.strictMatchingRepairTransaction = strictMatchingRepairTransaction;
// return this;
// }
public RepairableBuilder baseXP(Integer baseXP) {
public RepairableBuilder setBaseXP(Integer baseXP) {
this.baseXP = baseXP;
return this;
}
public RepairableBuilder rawNBT(RawNBT rawNBT) {
this.rawNBT = rawNBT;
return this;
}
public RepairableBuilder repairCount(Integer repairCount) {
public RepairableBuilder setRepairCount(Integer repairCount) {
this.repairCount = repairCount;
return this;
}
public RepairableBuilder permissionWrapper(PermissionWrapper permissionWrapper) {
public RepairableBuilder addPermissionWrapper(PermissionWrapper permissionWrapper) {
this.permissionWrapper = permissionWrapper;
return this;
}
@ -73,16 +57,8 @@ public class RepairableBuilder {
}
private Repairable makeRepairable() {
Repairable repairable = new Repairable(item, minimumLevel, maximumDurability, repairTransaction,
strictMatchingItem, baseXP, repairCount);
if(permissionWrapper != null) {
repairable.setPermissionWrapper(permissionWrapper);
}
if(rawNBT != null) {
repairable.setRawNBT(rawNBT);
}
Repairable repairable = new Repairable(customItemTarget, minimumLevel, maximumDurability, repairTransaction,
baseXP, repairCount, permissionWrapper);
return repairable;
}

View File

@ -9,8 +9,6 @@ import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_13_R2.util.CraftNBTTagConfigSerializer;
import org.bukkit.inventory.ItemStack;
public class NBTManager {
private static final String CRAFT_META_ITEM_CLASS_PATH = "org.bukkit.craftbukkit.inventory.CraftMetaItem";
@ -47,4 +45,9 @@ public class NBTManager {
}
}
public boolean hasNBT(NBTTagCompound root, NBTTagCompound nbtData) {
//TODO: Implement this
return false;
}
}

View File

@ -1,5 +1,7 @@
package com.gmail.nossr50.util.nbt;
import net.minecraft.server.v1_13_R2.NBTTagCompound;
/**
* A simple class that acts as a container for raw NBT data
* NBT data will be constructed from the raw NBT string using server internals
@ -10,13 +12,14 @@ package com.gmail.nossr50.util.nbt;
* 1) Read partial or complete NBT from the config file for an item
* 2) Check an items NBT tree for this NBT data during runtime once transformed
*/
public class RawNBT<T> {
public class RawNBT {
private String nbtContents;
private T nbtTree; //Will be constructed using server internals to make matching NBT easier
private NBTTagCompound nbtData; //Will be constructed using server internals to make matching NBT easier
public RawNBT(String nbtContents, T nbtTree) {
public RawNBT(String nbtContents, NBTTagCompound nbtData) {
this.nbtContents = nbtContents;
NBTManager
this.nbtData = nbtData;
}
public String getNbtContents() {
@ -27,4 +30,7 @@ public class RawNBT<T> {
this.nbtContents = nbtContents;
}
public NBTTagCompound getNbtData() {
return nbtData;
}
}