Skip to content

Commit

Permalink
Proper fix for #143
Browse files Browse the repository at this point in the history
  • Loading branch information
KillerOfPie committed Nov 28, 2022
1 parent 9c37352 commit 753991f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private GuiElement numericalOption(ShopItemStackSettingKeys setting, ShopItemSta

if (setting.isUserEditable() && isScreenEditable) {
return new GuiStateElement('e',
String.valueOf(item.getShopSettingAsInteger(setting)),
String.valueOf(item.getShopSetting(setting).asInteger()),
new GuiStateElement.State(change -> {
item.setShopSettings(setting, new ObjectHolder<>(2));
},
Expand Down Expand Up @@ -237,13 +237,13 @@ private GuiElement numericalOption(ShopItemStackSettingKeys setting, ShopItemSta
));
}

return new StaticGuiElement('e', indexedTempItem[item.getShopSettingAsInteger(setting) + 1], setting.makeReadable(), item.getStateString(setting));
return new StaticGuiElement('e', indexedTempItem[item.getShopSetting(setting).asInteger() + 1], setting.makeReadable(), item.getStateString(setting));
}

private GuiElement booleanOption(ShopItemStackSettingKeys setting, ShopItemStack item, boolean isScreenEditable) {
if (setting.isUserEditable() && isScreenEditable) {
return new GuiStateElement('e',
String.valueOf(item.getShopSettingAsBoolean(setting)),
String.valueOf(item.getShopSetting(setting).asBoolean()),
new GuiStateElement.State(change -> {
item.setShopSettings(setting, new ObjectHolder<>(true));
},
Expand All @@ -262,7 +262,7 @@ private GuiElement booleanOption(ShopItemStackSettingKeys setting, ShopItemStack
));
}

return new StaticGuiElement('e', getBooleanItem(item.getShopSettingAsBoolean(setting)), setting.makeReadable(), item.getStateString(setting));
return new StaticGuiElement('e', getBooleanItem(item.getShopSetting(setting).asBoolean()), setting.makeReadable(), item.getStateString(setting));
}

private StaticGuiElement settingDisplayItem(ShopItemStackSettingKeys setting, ShopItemStack tempItem) {
Expand Down
58 changes: 22 additions & 36 deletions src/main/java/org/shanerx/tradeshop/item/ShopItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,14 @@ public String serialize() {
return new GsonProcessor().toJson(this);
}

public boolean getShopSettingAsBoolean(ShopItemStackSettingKeys key) {
if (key.isUserEditable()) {
try {
ObjectHolder<?> tempObj = itemSettings.get(key);
return itemSettings.containsKey(key) ? (Boolean) tempObj.getObject() : (Boolean) key.getDefaultValue().getObject();
} catch (ClassCastException | NullPointerException ignored) {
}
}

return (Boolean) key.getDefaultValue().getObject();
}

public int getShopSettingAsInteger(ShopItemStackSettingKeys key) {
if (key.isUserEditable()) {
try {
ObjectHolder<?> tempObj = itemSettings.get(key);
return itemSettings.containsKey(key) ? (Integer) tempObj.getObject() : (Integer) key.getDefaultValue().getObject();
} catch (ClassCastException | NullPointerException ignored) {
}
public ObjectHolder<?> getShopSetting(ShopItemStackSettingKeys key) {
if (key.isUserEditable() && itemSettings.containsKey(key)) {
ObjectHolder<?> tempObj = itemSettings.get(key);
if (tempObj != null && tempObj.getObject() != null)
return tempObj;
}

return (Integer) key.getDefaultValue().getObject();
return key.getDefaultValue();
}

private void buildMap() {
Expand Down Expand Up @@ -242,7 +228,7 @@ public boolean isSimilar(ItemStack toCompare) {

// If compareShulkerInventory is on
if (itemStack.getType().toString().endsWith("SHULKER_BOX") &&
getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_SHULKER_INVENTORY)) {
getShopSetting(ShopItemStackSettingKeys.COMPARE_SHULKER_INVENTORY).asBoolean()) {
try {
ArrayList<ItemStack> itemStackContents = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) toCompareMeta).getBlockState()).getInventory().getContents()),
toCompareContents = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) itemStackMeta).getBlockState()).getInventory().getContents());
Expand All @@ -269,7 +255,7 @@ public boolean isSimilar(ItemStack toCompare) {
// If compareBundleInventory is on and version is above 1.17 also check Bundles
if (new Utils().PLUGIN.getVersion().isAtLeast(1, 17) &&
itemStack.getType().equals(Material.BUNDLE) &&
getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_BUNDLE_INVENTORY)) {
getShopSetting(ShopItemStackSettingKeys.COMPARE_BUNDLE_INVENTORY).asBoolean()) {
try {
ArrayList<ItemStack> itemStackContents = Lists.newArrayList(((BundleMeta) toCompareMeta).getItems()),
toCompareContents = Lists.newArrayList(((BundleMeta) itemStackMeta).getItems());
Expand All @@ -294,7 +280,7 @@ public boolean isSimilar(ItemStack toCompare) {
}

// If compareDurability is on
int compareDurability = getShopSettingAsInteger(ShopItemStackSettingKeys.COMPARE_DURABILITY);
int compareDurability = getShopSetting(ShopItemStackSettingKeys.COMPARE_DURABILITY).asInteger();
if (compareDurability > -1 && compareDurability < 3 && useMeta) {

// Return False if Damageable is not equal (one has and one doesn't)
Expand Down Expand Up @@ -333,7 +319,7 @@ public boolean isSimilar(ItemStack toCompare) {
}

// If compareEnchantments is on
if (getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_ENCHANTMENTS) && useMeta) {
if (getShopSetting(ShopItemStackSettingKeys.COMPARE_ENCHANTMENTS).asBoolean() && useMeta) {
if (itemStackMeta instanceof EnchantmentStorageMeta && toCompareMeta instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta itemStackEnchantmentStorageMeta = (EnchantmentStorageMeta) itemStackMeta,
toCompareEnchantmentStorageMeta = (EnchantmentStorageMeta) toCompareMeta;
Expand Down Expand Up @@ -364,7 +350,7 @@ public boolean isSimilar(ItemStack toCompare) {
}

// If compareName is on
if (getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_NAME)) {
if (getShopSetting(ShopItemStackSettingKeys.COMPARE_NAME).asBoolean()) {
debugger.log("ShopItemStack > isSimilar > getDisplayName: " + itemStackMeta.getDisplayName() + " - " + toCompareMeta.getDisplayName(), DebugLevels.NAME_COMPARE);

// If ItemStack Meta are BookMeta then compare title, otherwise compare displayname
Expand All @@ -386,7 +372,7 @@ public boolean isSimilar(ItemStack toCompare) {
}

// If useBookMeta and compareBookAuthor are true
if (useBookMeta && getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_BOOK_AUTHOR)) {
if (useBookMeta && getShopSetting(ShopItemStackSettingKeys.COMPARE_BOOK_AUTHOR).asBoolean()) {
// Return False if hasAuthor differs (one has one doesn't)
debugger.log("itemStackBookMeta hasAuthor: " + itemStackBookMeta.hasAuthor(), DebugLevels.ITEM_COMPARE);
debugger.log("toCompareBookMeta hasAuthor: " + toCompareBookMeta.hasAuthor(), DebugLevels.ITEM_COMPARE);
Expand All @@ -403,7 +389,7 @@ public boolean isSimilar(ItemStack toCompare) {
}

// If useBookMeta and compareBookPages are true
if (useBookMeta && getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_BOOK_PAGES)) {
if (useBookMeta && getShopSetting(ShopItemStackSettingKeys.COMPARE_BOOK_PAGES).asBoolean()) {
// Return False if hasPages differs (one has one doesn't)
debugger.log("itemStackBookMeta hasPages: " + itemStackBookMeta.hasPages(), DebugLevels.ITEM_COMPARE);
debugger.log("toCompareBookMeta hasPages: " + toCompareBookMeta.hasPages(), DebugLevels.ITEM_COMPARE);
Expand All @@ -420,7 +406,7 @@ public boolean isSimilar(ItemStack toCompare) {
}

// If compareLore is on
if (getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_LORE) && useMeta) {
if (getShopSetting(ShopItemStackSettingKeys.COMPARE_LORE).asBoolean() && useMeta) {
// Return False if hasLore differs (one has one doesn't)
if (itemStackMeta.hasLore() != toCompareMeta.hasLore()) return false;

Expand All @@ -430,7 +416,7 @@ public boolean isSimilar(ItemStack toCompare) {
}

// If compareCustomModelData is on
if (getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_CUSTOM_MODEL_DATA) && useMeta) {
if (getShopSetting(ShopItemStackSettingKeys.COMPARE_CUSTOM_MODEL_DATA).asBoolean() && useMeta) {
// Return False if hasCustomModelData differs (one has one doesn't)
if (itemStackMeta.hasCustomModelData() != toCompareMeta.hasCustomModelData()) return false;

Expand All @@ -440,7 +426,7 @@ public boolean isSimilar(ItemStack toCompare) {
}

// If compareItemFlags is on
if (getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_ITEM_FLAGS) && useMeta) {
if (getShopSetting(ShopItemStackSettingKeys.COMPARE_ITEM_FLAGS).asBoolean() && useMeta) {
// Return False if getItemFlags sizes differs
if (itemStackMeta.getItemFlags().size() != toCompareMeta.getItemFlags().size()) return false;

Expand All @@ -449,7 +435,7 @@ public boolean isSimilar(ItemStack toCompare) {
}

// Return False if compareUnbreakable is on and isUnbreakable differs
if (getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_UNBREAKABLE) && useMeta && itemStackMeta.isUnbreakable() != toCompareMeta.isUnbreakable())
if (getShopSetting(ShopItemStackSettingKeys.COMPARE_UNBREAKABLE).asBoolean() && useMeta && itemStackMeta.isUnbreakable() != toCompareMeta.isUnbreakable())
return false;

// If item is firework rocket
Expand All @@ -458,13 +444,13 @@ public boolean isSimilar(ItemStack toCompare) {
FireworkMeta toCompareFireworkMeta = (FireworkMeta) toCompareMeta;

// If server compare firework duration is disabled local setting is ignores
if (getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_FIREWORK_DURATION)) {
if (getShopSetting(ShopItemStackSettingKeys.COMPARE_FIREWORK_DURATION).asBoolean()) {
if (fireworkMeta.getPower() != toCompareFireworkMeta.getPower()) {
return false;
}
}

if (getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_FIREWORK_EFFECTS)) {
if (getShopSetting(ShopItemStackSettingKeys.COMPARE_FIREWORK_EFFECTS).asBoolean()) {
if (fireworkMeta.hasEffects()) {
if (fireworkMeta.getEffects().size() != toCompareFireworkMeta.getEffects().size()) {
return false;
Expand All @@ -482,7 +468,7 @@ public boolean isSimilar(ItemStack toCompare) {
}

// If compareAttributeModifier is on
if (getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_ATTRIBUTE_MODIFIER) && useMeta) {
if (getShopSetting(ShopItemStackSettingKeys.COMPARE_ATTRIBUTE_MODIFIER).asBoolean() && useMeta) {
if (itemStackMeta.hasAttributeModifiers() != toCompareMeta.hasAttributeModifiers()) return false;

// Return False if itemStack hasAttributeModifiers && getAttributeModifiers are not equal
Expand Down Expand Up @@ -532,8 +518,8 @@ public String getStateString(ObjectHolder<?> stateSetting) {
} else {
ret = "False";
}
} else if (stateSetting.isInteger() || stateSetting.isDouble()) {
switch (stateSetting.isDouble() ? ((Double) stateSetting.getObject()).intValue() : (Integer) stateSetting.getObject()) {
} else if (stateSetting.isInteger()) {
switch (stateSetting.asInteger()) {
case 2:
ret = ">=";
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public boolean isBoolean() {
}

public boolean isInteger() {
return obj != null && obj instanceof Integer;
return obj != null && (obj instanceof Integer || (obj instanceof Double && Double.parseDouble(obj.toString()) % 1 == 0));
}

public boolean isDouble() {
return obj != null && obj instanceof Double;
return obj != null && (obj instanceof Double || obj instanceof Integer);
}

public boolean isString() {
Expand All @@ -61,7 +61,7 @@ public Boolean asBoolean() {
}

public Integer asInteger() {
return isInteger() ? Integer.parseInt(obj.toString()) : null;
return isInteger() ? (int) Double.parseDouble(obj.toString()) : null;
}

public Double asDouble() {
Expand Down

0 comments on commit 753991f

Please sign in to comment.