mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-25 06:36:45 +01:00
RepairableBuilder, ItemStackSerializer WIP
This commit is contained in:
parent
c0685bd1eb
commit
8f04b83954
@ -16,9 +16,9 @@ import java.util.List;
|
||||
|
||||
public class ItemStackSerializer implements TypeSerializer<ItemStack> {
|
||||
|
||||
private static final String ITEM_MINECRAFT_NAME = "Item-Minecraft-Name";
|
||||
private static final String ITEM_MINECRAFT_NAME = "Item-Name";
|
||||
private static final String AMOUNT = "Amount";
|
||||
private static final String ITEM_LORE = "Item-Lore";
|
||||
private static final String NBT = "NBT";
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
@ -47,19 +47,26 @@ public class ItemStackSerializer implements TypeSerializer<ItemStack> {
|
||||
//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);
|
||||
if(itemNode.getNode(NBT).getValueType() != ValueType.NULL) {
|
||||
//TODO: NBT Stuff
|
||||
}
|
||||
|
||||
return itemStack;
|
||||
|
||||
return null;
|
||||
//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);
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(@NonNull TypeToken<?> type, @Nullable ItemStack 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()
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
package com.gmail.nossr50.config.hocon.serializers;
|
||||
|
||||
import com.gmail.nossr50.datatypes.permissions.PermissionWrapper;
|
||||
import com.gmail.nossr50.skills.repair.RepairTransaction;
|
||||
import com.gmail.nossr50.skills.repair.repairables.Repairable;
|
||||
import com.gmail.nossr50.skills.repair.repairables.RepairableBuilder;
|
||||
import com.gmail.nossr50.util.nbt.RawNBT;
|
||||
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 ninja.leaping.configurate.util.EnumLookup;
|
||||
@ -16,34 +19,59 @@ import java.util.Set;
|
||||
|
||||
public class RepairableSerializer implements TypeSerializer<Repairable> {
|
||||
private static final String ITEM = "Item";
|
||||
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";
|
||||
private static final String STRICT_MATCHING = "Use-Strict-Matching";
|
||||
private static final String RAW_NBT = "Raw-NBT";
|
||||
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";
|
||||
|
||||
@Override
|
||||
public Repairable deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
|
||||
String itemMaterial = value.getNode(ITEM).getValue(TypeToken.of(String.class));
|
||||
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));
|
||||
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)));
|
||||
|
||||
return new Repairable(itemMaterial, repairTransaction, minimumLevel, minRepairs, baseXP, strictMatching, new RawNBT(rawNBT));
|
||||
StringBuilder w;
|
||||
|
||||
if(value.getNode(MINIMUM_LEVEL).getValueType() != ValueType.NULL) {
|
||||
builder = builder.minLevel(value.getNode(MINIMUM_LEVEL).getValue(TypeToken.of(Integer.class)));
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(TypeToken<?> type, Repairable obj, ConfigurationNode value) {
|
||||
value.getNode(ITEM).setValue(obj.getItemMaterial().getKey().toString());
|
||||
value.getNode(ITEMS_USED_TO_REPAIR).setValue(obj.getRepairTransaction());
|
||||
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(FULL_REPAIR_TRANSACTIONS).setValue(obj.getRepairCount());
|
||||
value.getNode(STRICT_MATCHING).setValue(obj.useStrictMatching());
|
||||
value.getNode(RAW_NBT).setValue(obj.getRawNBT().getNbtContents());
|
||||
value.getNode(REPAIR_COUNT).setValue(obj.getRepairCount());
|
||||
|
||||
if(obj.getMinimumLevel() > 0)
|
||||
value.getNode(MINIMUM_LEVEL).setValue(obj.getMinimumLevel());
|
||||
|
||||
if(obj.hasNBT())
|
||||
value.getNode(NBT).setValue(obj.getRawNBT());
|
||||
|
||||
if(obj.hasPermission())
|
||||
value.getNode(PERMISSION).setValue(obj.getPermissionWrapper());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,9 +4,35 @@ import java.util.Objects;
|
||||
|
||||
public class PermissionWrapper {
|
||||
private String permissionAddress;
|
||||
private boolean playerDefault;
|
||||
private boolean operatorDefault;
|
||||
|
||||
public PermissionWrapper(String permissionAddress) {
|
||||
this.permissionAddress = permissionAddress;
|
||||
this.playerDefault = true;
|
||||
this.operatorDefault = true;
|
||||
}
|
||||
|
||||
public PermissionWrapper(String permissionAddress, boolean playerDefault, boolean operatorDefault) {
|
||||
this.permissionAddress = permissionAddress;
|
||||
this.playerDefault = playerDefault;
|
||||
this.operatorDefault = operatorDefault;
|
||||
}
|
||||
|
||||
public boolean isPlayerDefault() {
|
||||
return playerDefault;
|
||||
}
|
||||
|
||||
public void setPlayerDefault(boolean playerDefault) {
|
||||
this.playerDefault = playerDefault;
|
||||
}
|
||||
|
||||
public boolean isOperatorDefault() {
|
||||
return operatorDefault;
|
||||
}
|
||||
|
||||
public void setOperatorDefault(boolean operatorDefault) {
|
||||
this.operatorDefault = operatorDefault;
|
||||
}
|
||||
|
||||
public String getPermissionAddress() {
|
||||
|
@ -4,36 +4,100 @@ import com.gmail.nossr50.datatypes.permissions.PermissionWrapper;
|
||||
import com.gmail.nossr50.skills.repair.RepairTransaction;
|
||||
import com.gmail.nossr50.util.nbt.RawNBT;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class Repairable {
|
||||
private final Material itemMaterial;
|
||||
private final int minimumLevel;
|
||||
private final short maximumDurability;
|
||||
private final ItemStack item;
|
||||
private int minimumLevel = 0;
|
||||
private short maximumDurability;
|
||||
private RepairTransaction repairTransaction;
|
||||
private boolean strictMatching;
|
||||
private int baseXP;
|
||||
private boolean strictMatchingItem = false;
|
||||
private boolean strictMatchingRepairTransaction = false;
|
||||
private int baseXP = 0;
|
||||
private RawNBT rawNBT;
|
||||
private int repairCount;
|
||||
private int repairCount = 1;
|
||||
private PermissionWrapper permissionWrapper;
|
||||
private boolean hasPermission = false;
|
||||
private boolean hasNBT = false;
|
||||
|
||||
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, RepairTransaction repairTransaction, int minimumLevel, int repairCount, int baseXP) {
|
||||
this(itemMaterial.getKey().getKey(), repairTransaction, minimumLevel, repairCount, baseXP, false, null);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
this.maximumDurability = this.itemMaterial.getMaxDurability();
|
||||
this.repairCount = repairCount;
|
||||
public Repairable(ItemStack item, int minimumLevel, short maximumDurability, RepairTransaction repairTransaction, boolean strictMatchingItem, boolean strictMatchingRepairTransaction, int baseXP, int repairCount) {
|
||||
this.item = item;
|
||||
this.minimumLevel = minimumLevel;
|
||||
this.maximumDurability = maximumDurability;
|
||||
this.repairTransaction = repairTransaction;
|
||||
this.strictMatching = strictMatching;
|
||||
this.strictMatchingItem = strictMatchingItem;
|
||||
this.strictMatchingRepairTransaction = strictMatchingRepairTransaction;
|
||||
this.baseXP = baseXP;
|
||||
this.repairCount = repairCount;
|
||||
}
|
||||
|
||||
public ItemStack getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public int getMinimumLevel() {
|
||||
return minimumLevel;
|
||||
}
|
||||
|
||||
public void setMinimumLevel(int minimumLevel) {
|
||||
this.minimumLevel = minimumLevel;
|
||||
}
|
||||
|
||||
public short getMaximumDurability() {
|
||||
return maximumDurability;
|
||||
}
|
||||
|
||||
public void setMaximumDurability(short maximumDurability) {
|
||||
this.maximumDurability = maximumDurability;
|
||||
}
|
||||
|
||||
public RepairTransaction getRepairTransaction() {
|
||||
return repairTransaction;
|
||||
}
|
||||
|
||||
public void setRepairTransaction(RepairTransaction repairTransaction) {
|
||||
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;
|
||||
}
|
||||
|
||||
public void setBaseXP(int baseXP) {
|
||||
this.baseXP = baseXP;
|
||||
}
|
||||
|
||||
public RawNBT getRawNBT() {
|
||||
return rawNBT;
|
||||
}
|
||||
|
||||
public void setRawNBT(RawNBT rawNBT) {
|
||||
this.rawNBT = rawNBT;
|
||||
hasNBT = true;
|
||||
}
|
||||
|
||||
public int getRepairCount() {
|
||||
return repairCount;
|
||||
}
|
||||
|
||||
public void setRepairCount(int repairCount) {
|
||||
this.repairCount = repairCount;
|
||||
}
|
||||
|
||||
public PermissionWrapper getPermissionWrapper() {
|
||||
@ -42,41 +106,22 @@ public class Repairable {
|
||||
|
||||
public void setPermissionWrapper(PermissionWrapper permissionWrapper) {
|
||||
this.permissionWrapper = permissionWrapper;
|
||||
hasPermission = true;
|
||||
}
|
||||
|
||||
public RawNBT getRawNBT() {
|
||||
return rawNBT;
|
||||
public boolean hasPermission() {
|
||||
return hasPermission;
|
||||
}
|
||||
|
||||
public int getRepairCount() {
|
||||
return repairCount;
|
||||
public void setHasPermission(boolean hasPermission) {
|
||||
this.hasPermission = hasPermission;
|
||||
}
|
||||
|
||||
public Material getItemMaterial() {
|
||||
return itemMaterial;
|
||||
public boolean hasNBT() {
|
||||
return hasNBT;
|
||||
}
|
||||
|
||||
public RepairTransaction getRepairTransaction() {
|
||||
return repairTransaction;
|
||||
}
|
||||
|
||||
public boolean useStrictMatching() {
|
||||
return strictMatching;
|
||||
}
|
||||
|
||||
public int getBaseXP() {
|
||||
return baseXP;
|
||||
}
|
||||
|
||||
public short getMaximumDurability() {
|
||||
return maximumDurability;
|
||||
}
|
||||
|
||||
public short getBaseRepairDurability() {
|
||||
return (short) (maximumDurability / repairCount);
|
||||
}
|
||||
|
||||
public int getMinimumLevel() {
|
||||
return minimumLevel;
|
||||
public void setHasNBT(boolean hasNBT) {
|
||||
this.hasNBT = hasNBT;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,90 @@
|
||||
package com.gmail.nossr50.skills.repair.repairables;
|
||||
|
||||
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 RepairableBuilder {
|
||||
|
||||
private final ItemStack item;
|
||||
private int minimumLevel = 0;
|
||||
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 PermissionWrapper permissionWrapper;
|
||||
|
||||
public RepairableBuilder(ItemStack item) {
|
||||
this.item = item;
|
||||
this.maximumDurability = item.getType().getMaxDurability();
|
||||
}
|
||||
|
||||
public RepairableBuilder minLevel(Integer minimumLevel) {
|
||||
this.minimumLevel = minimumLevel;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RepairableBuilder maximumDurability(Short maximumDurability) {
|
||||
this.maximumDurability = maximumDurability;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RepairableBuilder repairTransaction(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) {
|
||||
this.baseXP = baseXP;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RepairableBuilder rawNBT(RawNBT rawNBT) {
|
||||
this.rawNBT = rawNBT;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RepairableBuilder repairCount(Integer repairCount) {
|
||||
this.repairCount = repairCount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RepairableBuilder permissionWrapper(PermissionWrapper permissionWrapper) {
|
||||
this.permissionWrapper = permissionWrapper;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Repairable build() {
|
||||
return makeRepairable();
|
||||
}
|
||||
|
||||
private Repairable makeRepairable() {
|
||||
Repairable repairable = new Repairable(item, minimumLevel, maximumDurability, repairTransaction,
|
||||
strictMatchingItem, strictMatchingRepairTransaction, baseXP, repairCount);
|
||||
|
||||
if(permissionWrapper != null) {
|
||||
repairable.setPermissionWrapper(permissionWrapper);
|
||||
}
|
||||
|
||||
if(rawNBT != null) {
|
||||
repairable.setRawNBT(rawNBT);
|
||||
}
|
||||
|
||||
return repairable;
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package com.gmail.nossr50.util.nbt;
|
||||
|
||||
|
||||
public class NBTUtils {
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user