From 9d3016430844ad4041d52eba38fd2a29328f5ae7 Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Tue, 13 Aug 2019 12:23:24 -0500 Subject: [PATCH] fix message store overwrite on spawn, other NPE errors --- .../listeners/pre/BossSpawnListener.java | 18 ++++++++++-------- .../epicbosses/managers/BossEntityManager.java | 6 +++--- .../epicbosses/panel/MainMenuPanel.java | 1 - .../bosses/commands/OnDeathCommandEditor.java | 2 +- .../bosses/commands/OnSpawnCommandEditor.java | 2 +- .../utils/itemstack/ItemStackUtils.java | 15 +++++++++++---- .../utils/panel/base/IPanelHandler.java | 1 - 7 files changed, 26 insertions(+), 19 deletions(-) diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/listeners/pre/BossSpawnListener.java b/plugin-modules/Core/src/com/songoda/epicbosses/listeners/pre/BossSpawnListener.java index 2243f5e..6471278 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/listeners/pre/BossSpawnListener.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/listeners/pre/BossSpawnListener.java @@ -17,6 +17,7 @@ import com.songoda.epicbosses.utils.ServerUtils; import com.songoda.epicbosses.utils.StringUtils; import com.songoda.epicbosses.utils.itemstack.ItemStackUtils; import com.songoda.epicbosses.utils.version.VersionHandler; +import java.util.ArrayList; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; @@ -117,8 +118,8 @@ public class BossSpawnListener implements Listener { BossEntity bossEntity = activeBossHolder.getBossEntity(); Location location = activeBossHolder.getLocation(); - List commands = this.bossEntityManager.getOnSpawnCommands(bossEntity); - List messages = this.bossEntityManager.getOnSpawnMessage(bossEntity); + List commands = new ArrayList(this.bossEntityManager.getOnSpawnCommands(bossEntity)); + List messages = new ArrayList(this.bossEntityManager.getOnSpawnMessage(bossEntity)); int messageRadius = this.bossEntityManager.getOnSpawnMessageRadius(bossEntity); ServerUtils serverUtils = ServerUtils.get(); @@ -133,22 +134,23 @@ public class BossSpawnListener implements Listener { player.updateInventory(); } - if (commands != null) + if (!commands.isEmpty()) commands.replaceAll(s -> s.replaceAll("%player%", player.getName())); - if (messages != null && !activeBossHolder.isCustomSpawnMessage()) + if (!messages.isEmpty() && !activeBossHolder.isCustomSpawnMessage()) messages.replaceAll(s -> s.replace("{name}", player.getName())); } else { - if (messages != null && !activeBossHolder.isCustomSpawnMessage()) + if (!messages.isEmpty() && !activeBossHolder.isCustomSpawnMessage()) messages.replaceAll(s -> s.replace("{name}", "Console")); } - if (commands != null) + if (!commands.isEmpty()) commands.forEach(serverUtils::sendConsoleCommand); - if(messages != null && !activeBossHolder.isCustomSpawnMessage()) { + if(!messages.isEmpty() && !activeBossHolder.isCustomSpawnMessage()) { if(activeBossHolder.getName() != null) messages.replaceAll(s -> s.replace("{boss}", activeBossHolder.getName())); - messages.replaceAll(s -> s.replace("{location}", StringUtils.get().translateLocation(location))); + final String locationString = StringUtils.get().translateLocation(location); + messages.replaceAll(s -> s.replace("{location}", locationString)); MessageUtils.get().sendMessage(location, NumberUtils.get().getSquared(messageRadius), messages); } diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/managers/BossEntityManager.java b/plugin-modules/Core/src/com/songoda/epicbosses/managers/BossEntityManager.java index a653794..e4adeed 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/managers/BossEntityManager.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/managers/BossEntityManager.java @@ -183,7 +183,7 @@ public class BossEntityManager { if(messages == null) { Debug.FAILED_TO_LOAD_MESSAGES.debug(id); - return null; + return Collections.EMPTY_LIST; } return messages; @@ -203,7 +203,7 @@ public class BossEntityManager { if(commands == null) { Debug.FAILED_TO_LOAD_COMMANDS.debug(id); - return null; + return Collections.EMPTY_LIST; } return commands; @@ -215,7 +215,7 @@ public class BossEntityManager { if(messages == null) { Debug.FAILED_TO_LOAD_MESSAGES.debug(id); - return null; + return Collections.EMPTY_LIST; } return messages; diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/panel/MainMenuPanel.java b/plugin-modules/Core/src/com/songoda/epicbosses/panel/MainMenuPanel.java index 77e374d..3c4420d 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/panel/MainMenuPanel.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/panel/MainMenuPanel.java @@ -7,7 +7,6 @@ import com.songoda.epicbosses.utils.panel.base.ClickAction; import com.songoda.epicbosses.utils.panel.base.PanelHandler; import com.songoda.epicbosses.utils.panel.builder.PanelBuilder; import com.songoda.epicbosses.utils.panel.builder.PanelBuilderCounter; -import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/panel/bosses/commands/OnDeathCommandEditor.java b/plugin-modules/Core/src/com/songoda/epicbosses/panel/bosses/commands/OnDeathCommandEditor.java index 113168e..e0327c3 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/panel/bosses/commands/OnDeathCommandEditor.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/panel/bosses/commands/OnDeathCommandEditor.java @@ -97,7 +97,7 @@ public class OnDeathCommandEditor extends VariablePanelHandler { replaceMap.put("{name}", name); - if(bossEntity.getCommands().getOnDeath().equalsIgnoreCase(name)) { + if(bossEntity.getCommands().getOnDeath() != null && bossEntity.getCommands().getOnDeath().equalsIgnoreCase(name)) { ItemStackUtils.applyDisplayName(itemStack, this.plugin.getConfig().getString("Display.Boss.Commands.selectedName"), replaceMap); } else { ItemStackUtils.applyDisplayName(itemStack, this.plugin.getConfig().getString("Display.Boss.Commands.name"), replaceMap); diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/panel/bosses/commands/OnSpawnCommandEditor.java b/plugin-modules/Core/src/com/songoda/epicbosses/panel/bosses/commands/OnSpawnCommandEditor.java index f877e9f..35addcd 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/panel/bosses/commands/OnSpawnCommandEditor.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/panel/bosses/commands/OnSpawnCommandEditor.java @@ -97,7 +97,7 @@ public class OnSpawnCommandEditor extends VariablePanelHandler { replaceMap.put("{name}", name); - if(bossEntity.getCommands().getOnSpawn().equalsIgnoreCase(name)) { + if(bossEntity.getCommands().getOnSpawn() != null && bossEntity.getCommands().getOnSpawn().equalsIgnoreCase(name)) { ItemStackUtils.applyDisplayName(itemStack, this.plugin.getConfig().getString("Display.Boss.Commands.selectedName"), replaceMap); } else { ItemStackUtils.applyDisplayName(itemStack, this.plugin.getConfig().getString("Display.Boss.Commands.name"), replaceMap); diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/utils/itemstack/ItemStackUtils.java b/plugin-modules/Core/src/com/songoda/epicbosses/utils/itemstack/ItemStackUtils.java index fb113cf..6ccbacb 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/utils/itemstack/ItemStackUtils.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/utils/itemstack/ItemStackUtils.java @@ -153,15 +153,15 @@ public class ItemStackUtils { } } - if(lore != null) { + if(lore != null && !lore.isEmpty()) { for(String z : lore) { String y = z; if(replacedMap != null) { for(String x : replacedMap.keySet()) { - if(!y.contains(x)) continue; + if(x == null || !y.contains(x)) continue; - if (!replacedMap.containsKey(x)) { + if (replacedMap.get(x) == null) { ServerUtils.get().logDebug("Failed to apply replaced lore: [y=" + y + "x=" + x + "]"); continue; } @@ -258,7 +258,14 @@ public class ItemStackUtils { } public static void applyDisplayLore(ItemStack itemStack, List lore, Map replaceMap) { - ItemMeta itemMeta = itemStack.getItemMeta(); + ItemMeta itemMeta; + if (itemStack == null || (itemMeta = itemStack.getItemMeta()) == null) + return; + if (lore == null || lore.isEmpty()) { + itemMeta.setLore(Collections.EMPTY_LIST); + itemStack.setItemMeta(itemMeta); + return; + } if(replaceMap != null) { for(String s : replaceMap.keySet()) { diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/utils/panel/base/IPanelHandler.java b/plugin-modules/Core/src/com/songoda/epicbosses/utils/panel/base/IPanelHandler.java index 1d95bc5..1dc660c 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/utils/panel/base/IPanelHandler.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/utils/panel/base/IPanelHandler.java @@ -1,7 +1,6 @@ package com.songoda.epicbosses.utils.panel.base; import com.songoda.epicbosses.utils.panel.Panel; -import com.songoda.epicbosses.utils.panel.builder.PanelBuilder; import org.bukkit.entity.Player; /**