Skip to content

Commit

Permalink
Better NullPointerHandling
Browse files Browse the repository at this point in the history
Also sort-of fixed a spigot-exclusive bug with item merging, however it still doesn't work for whatever reason and actually "fixing" this bug would require some hacky workarounds, one (registering a new enchantment) basically destroys a selling point that we currently have (that we don't register any enchantments and as such don't make use of high amounts of reflection (we still make use of some reflection, but it's not as extreme)).

Signed-off-by: Geolykt <[email protected]>
  • Loading branch information
Geolykt committed Nov 22, 2020
1 parent 49af4be commit e86f8bf
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 30 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
<artifactId>spigot-api</artifactId>
<version>1.16.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<type>javadoc</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains/annotations -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public CompatibilityAdapter(Plugin plugin) {
private EnumSet<Material> getMaterialSet(FileConfiguration config, String path) {
EnumSet<Material> es = EnumSet.noneOf(Material.class);
for (String materialName : config.getStringList(path)) {
Material material = Material.matchMaterial(materialName);
if (material != null) {
Material material = Material.matchMaterial(materialName);
if (material != null) {
es.add(material);
}
}
}
return es;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import de.geolykt.enchantments_plus.CustomEnchantment;

Expand Down Expand Up @@ -36,7 +37,7 @@ public AdvancedLoreGetter(EnumSet<Material> allowlist, boolean denylistToggle) {
}

@Override
public LinkedHashMap<CustomEnchantment, Integer> getEnchants(ItemStack stk, boolean acceptBooks, World world,
public LinkedHashMap<CustomEnchantment, Integer> getEnchants(@Nullable ItemStack stk, boolean acceptBooks, World world,
List<String> outExtraLore) {
if (stk != null) {
if (isGetterDenylist) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Nullable;

import de.geolykt.enchantments_plus.Config;
import de.geolykt.enchantments_plus.CustomEnchantment;
Expand All @@ -28,30 +29,29 @@
public class BasicLoreGetter implements IEnchGatherer {

@Override
public LinkedHashMap<CustomEnchantment, Integer> getEnchants(ItemStack stk, boolean acceptBooks, World world,
public LinkedHashMap<CustomEnchantment, Integer> getEnchants(@Nullable ItemStack stk, boolean acceptBooks, World world,
List<String> outExtraLore) {
LinkedHashMap<CustomEnchantment, Integer> map = new LinkedHashMap<>();
if (stk != null && (acceptBooks || stk.getType() != Material.ENCHANTED_BOOK)) {
if (stk.hasItemMeta()) {
if (stk.getItemMeta().hasLore()) {
Config cfg = Config.get(world);
List<String> lore = stk.getItemMeta().getLore();
for (String raw : lore) {
Map.Entry<CustomEnchantment, Integer> ench = getEnchant(raw, cfg);
if (ench != null) {
map.put(ench.getKey(), ench.getValue());
} else {
if (outExtraLore != null) {
outExtraLore.add(raw);
}
if (stk.getItemMeta() != null && stk.getItemMeta().hasLore() && stk.getItemMeta().getLore() != null) {
Config cfg = Config.get(world);
List<String> lore = stk.getItemMeta().getLore();
for (String raw : lore) {
Map.Entry<CustomEnchantment, Integer> ench = getEnchant(raw, cfg);
if (ench != null) {
map.put(ench.getKey(), ench.getValue());
} else {
if (outExtraLore != null) {
outExtraLore.add(raw);
}
}
}
}
}
return map;
}


@Nullable
private Map.Entry<CustomEnchantment, Integer> getEnchant(String raw, Config cfg) {
raw = raw.replaceAll("(" + ChatColor.COLOR_CHAR + ".)", "").trim();
switch (raw.split(" ").length) {
Expand Down Expand Up @@ -135,7 +135,7 @@ public void setEnchantment(ItemStack stk, CustomEnchantment ench, int level, Wor

@Override
public int getEnchantmentLevel(Config config, ItemStack stk, BaseEnchantments enchantment) {
if (stk != null && stk.hasItemMeta() && stk.getItemMeta().hasLore()) {
if (stk != null && stk.hasItemMeta() && stk.getItemMeta().getLore() != null) {
for (String raw : stk.getItemMeta().getLore()) {
Map.Entry<CustomEnchantment, Integer> ench = getEnchant(raw, config);
if (ench != null && ench.getKey().asEnum() == enchantment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public abstract LinkedHashMap<CustomEnchantment, Integer> getEnchants(@Nullable

/**
* Checks whether an Item has a given Enchantment. This should be used instead of a normal map.contains() as it can be
* Optimized by some getters to result in significantly better performance.
* Optimised by some getters to result in significantly better performance.
* @param config The configuration that should be used
* @param stk The stack that should be read
* @param ench The Enchantment that should be searched for.
Expand All @@ -87,7 +87,7 @@ public default boolean hasEnchantment(Config config, ItemStack stk, BaseEnchantm

/**
* Returns the level of a given enchantment. This should be used instead of a normal map.get() as it can be
* Optimized by some getters to result in significantly better performance. <br>
* Optimised by some getters to result in significantly better performance. <br>
* In case the enchantment is not found, 0 should be returned.
* @param config The configuration that should be used
* @param stk The stack that should be read
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ public LinkedHashMap<CustomEnchantment, Integer> getEnchants(ItemStack stk, bool
List<String> outExtraLore) {
LinkedHashMap<CustomEnchantment, Integer> map = new LinkedHashMap<>();
if ( (stk != null && stk.getType() != Material.AIR) && (acceptBooks || stk.getType() != Material.ENCHANTED_BOOK)) {
if (stk.hasItemMeta()) {
if (stk.getItemMeta() != null) {

final PersistentDataContainer cont = stk.getItemMeta().getPersistentDataContainer();

Set<NamespacedKey> keys = cont.getKeys();
if (keys == null) {
return map;
}

for (NamespacedKey key : keys) {
if (!key.getNamespace().toLowerCase(Locale.ROOT).equals("enchantments_plus")) {
if (!key.getNamespace().toLowerCase(Locale.ROOT).equals("enchantments_plus")) { // FIXME hardcoded string!
continue;
}
if (!key.getKey().split("\\.")[0].equals("ench")) {
Expand All @@ -66,7 +69,7 @@ public void setEnchantment(ItemStack stk, CustomEnchantment ench, int level, Wor
}
ItemMeta meta = stk.getItemMeta();
List<String> lore = new LinkedList<>();
if (meta.hasLore()) {
if (meta.getLore() != null) {
for (String loreStr : meta.getLore()) {
if (!loreStr.toLowerCase(Locale.ENGLISH).contains(ench.getLoreName().toLowerCase(Locale.ENGLISH))) {
lore.add(loreStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import de.geolykt.enchantments_plus.CustomEnchantment;

Expand Down Expand Up @@ -44,7 +45,7 @@ public PersistentDataGetter(EnumSet<Material> allowlist, boolean denylistToggle)
}

@Override
public LinkedHashMap<CustomEnchantment, Integer> getEnchants(ItemStack stk, boolean acceptBooks, World world,
public LinkedHashMap<CustomEnchantment, Integer> getEnchants(@Nullable ItemStack stk, boolean acceptBooks, World world,
List<String> outExtraLore) {
if (stk != null) {
if (isGetterDenylist) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import de.geolykt.enchantments_plus.CustomEnchantment;
import de.geolykt.enchantments_plus.compatibility.CompatibilityAdapter;
Expand All @@ -28,15 +29,16 @@ public class NewAnvilMerger implements Listener {

/**
* The remapping function used by {@link #mergeEnchantments(Map, Map)} to merge the levels of the same enchantments.
* Default behavior (v2.1.5) results in the following: if Integer A and Integer B are the same and non-0, then A+1 is returned.
* Otherwise the bigger of the two is returned.
* @param ench The enchantment that needs to be remapped, ignored per default
* Default behaviour (v2.1.5) results in the following: if Integer A and Integer B are the same and non-0, then the smaller of A+1 or the maximum level
* of the enchantment is returned.
* Otherwise the bigger of the two input levels is returned.
* @param ench The enchantment that needs to be remapped, used to retrieve the maximum enchantment level
* @param inputA The primary input level
* @param inputB The secondary input level
* @return Returns the new level of the Enchantment.
* @since 2.1.5
*/
public static Integer enchantmentLevelRemappingFunction(CustomEnchantment ench, Integer inputA, Integer inputB) {
public static int enchantmentLevelRemappingFunction(@NotNull CustomEnchantment ench, Integer inputA, Integer inputB) {
if (inputA != 0 && inputB != 0 && inputA == inputB) {
return Math.min(inputA + 1, ench.getMaxLevel());
} else {
Expand Down Expand Up @@ -91,7 +93,7 @@ public void prepareEvent(PrepareAnvilEvent evt) {
if (inv.getSize() < 3 || evt.getViewers().size() == 0 || inv.getItem(0) == null)
return;
List<String> nleftLore = new ArrayList<String>();
if (evt.getResult() == null) {
if (evt.getResult() == null || evt.getResult().getType() == Material.AIR) {
// Best guess merge
ItemStack stackA = inv.getItem(0).clone();
ItemStack stackB = inv.getItem(1);
Expand Down

0 comments on commit e86f8bf

Please sign in to comment.