Skip to content

Commit

Permalink
add Custom_Model_Data option to the weapon/ammo converters
Browse files Browse the repository at this point in the history
  • Loading branch information
CJCrafter committed Nov 20, 2023
1 parent e74ca51 commit ab316c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.deecaad.core.file.SerializeData;
import me.deecaad.core.file.Serializer;
import me.deecaad.core.file.SerializerException;
import me.deecaad.core.utils.ReflectionUtil;
import me.deecaad.weaponmechanics.WeaponMechanics;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
Expand All @@ -20,18 +21,20 @@ public class WeaponConverter implements Serializer<WeaponConverter> {
private boolean name;
private boolean lore;
private boolean enchantments;
private boolean cmd;

/**
* Default constructor for serializer
*/
public WeaponConverter() {
}

public WeaponConverter(boolean type, boolean name, boolean lore, boolean enchantments) {
public WeaponConverter(boolean type, boolean name, boolean lore, boolean enchantments, boolean cmd) {
this.type = type;
this.name = name;
this.lore = lore;
this.enchantments = enchantments;
this.cmd = cmd;
}

/**
Expand All @@ -54,6 +57,9 @@ public boolean isMatch(ItemStack weaponStack, ItemStack other) {
}
ItemMeta weaponMeta = weaponStack.getItemMeta();
ItemMeta otherMeta = other.getItemMeta();
if (weaponMeta == null || otherMeta == null)
throw new IllegalArgumentException("Tried to convert an item that was air");

if (this.name) {
if (weaponMeta.hasDisplayName() != otherMeta.hasDisplayName()
|| (weaponMeta.hasDisplayName() && !weaponMeta.getDisplayName().equals(otherMeta.getDisplayName()))) {
Expand All @@ -66,6 +72,12 @@ public boolean isMatch(ItemStack weaponStack, ItemStack other) {
return false;
}
}
if (this.cmd && ReflectionUtil.getMCVersion() >= 14) {
if (weaponMeta.getCustomModelData() != otherMeta.getCustomModelData()) {
return false;
}
}

if (this.enchantments) {
// If weapon would have enchantments, but other doesn't
// OR
Expand Down Expand Up @@ -110,15 +122,20 @@ public String getKeyword() {
boolean name = data.of("Name").getBool(false);
boolean lore = data.of("Lore").getBool(false);
boolean enchantments = data.of("Enchantments").getBool(false);
boolean cmd = data.of("Custom_Model_Data").getBool(false);

if (!type && !name && !lore && !enchantments) {
throw data.exception(null, "'Type', 'Name', 'Lore', and 'Enchantments' are all 'false'",
if (!type && !name && !lore && !enchantments && !cmd) {
throw data.exception(null, "'Type', 'Name', 'Lore', 'Enchantments', 'Custom_Model_Data' are all 'false'",
"One of them should be 'true' to allow weapon conversion",
"If you want to remove the weapon conversion feature, remove the '" + getKeyword() + "' option from config");
}

if (cmd && ReflectionUtil.getMCVersion() < 14) {
throw data.exception("Custom_Model_Data", "Custom_Model_Data is only available for 1.14+");
}

WeaponMechanics.getWeaponHandler().getInfoHandler().addWeaponWithConvert(data.key.split("\\.")[0]);

return new WeaponConverter(type, name, lore, enchantments);
return new WeaponConverter(type, name, lore, enchantments, cmd);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.deecaad.core.file.SerializeData;
import me.deecaad.core.file.SerializerException;
import me.deecaad.core.utils.ReflectionUtil;
import me.deecaad.weaponmechanics.weapon.info.WeaponConverter;
import org.jetbrains.annotations.NotNull;

Expand All @@ -14,8 +15,8 @@ public AmmoConverter() {
super();
}

public AmmoConverter(boolean type, boolean name, boolean lore, boolean enchantments) {
super(type, name, lore, enchantments);
public AmmoConverter(boolean type, boolean name, boolean lore, boolean enchantments, boolean cmd) {
super(type, name, lore, enchantments, cmd);
}

@Override
Expand All @@ -31,13 +32,18 @@ public String getKeyword() {
boolean name = data.of("Name").getBool(false);
boolean lore = data.of("Lore").getBool(false);
boolean enchantments = data.of("Enchantments").getBool(false);
boolean cmd = data.of("Custom_Model_Data").getBool(false);

if (!type && !name && !lore && !enchantments) {
throw data.exception(null, "'Type', 'Name', 'Lore', and 'Enchantments' are all 'false'",
if (!type && !name && !lore && !enchantments && !cmd) {
throw data.exception(null, "'Type', 'Name', 'Lore', 'Enchantments', 'Custom_Model_Data' are all 'false'",
"One of them should be 'true' to allow ammo conversion",
"If you want to remove the ammo conversion feature, remove the 'Ammo_Converter' option from config");
"If you want to remove the ammo conversion feature, remove the 'Ammo_Converter_Check' option from config");
}

return new AmmoConverter(type, name, lore, enchantments);
if (cmd && ReflectionUtil.getMCVersion() < 14) {
throw data.exception("Custom_Model_Data", "Custom_Model_Data is only available for 1.14+");
}

return new AmmoConverter(type, name, lore, enchantments, cmd);
}
}

0 comments on commit ab316c5

Please sign in to comment.