From b5fc523a8f276ccfaa71b24b48703b15a4473a2f Mon Sep 17 00:00:00 2001 From: RootBeer Date: Tue, 2 Dec 2025 15:43:55 -0500 Subject: [PATCH] Resolve null location check (possibly specifically on Folia) --- .../entitydetection/ChunkLocation.java | 6 +-- .../entitydetection/EntityDetection.java | 7 ++-- .../de/themoep/entitydetection/Utils.java | 4 +- .../commands/ListSubCommand.java | 24 ++++++------ .../commands/PluginCommandExecutor.java | 6 +-- .../commands/SearchSubCommand.java | 18 ++++----- .../commands/StopSubCommand.java | 2 +- .../commands/TpSubCommand.java | 10 ++--- .../searcher/ChunkSearchResult.java | 38 +++++++++++++++---- .../searcher/EntitySearch.java | 4 +- .../searcher/SearchResult.java | 2 +- .../entitydetection/searcher/SearchType.java | 14 +++---- .../searcher/WGSearchResult.java | 2 +- 13 files changed, 78 insertions(+), 59 deletions(-) diff --git a/src/main/java/de/themoep/entitydetection/ChunkLocation.java b/src/main/java/de/themoep/entitydetection/ChunkLocation.java index cda9303..7a77433 100644 --- a/src/main/java/de/themoep/entitydetection/ChunkLocation.java +++ b/src/main/java/de/themoep/entitydetection/ChunkLocation.java @@ -49,7 +49,7 @@ public String toString() { public Chunk toBukkit(Server server) { World world = server.getWorld(getWorld()); - if(world == null) { + if (world == null) { throw new IllegalArgumentException("No world with the name " + getWorld() + " found on the server for this chunk entry?"); } return world.getChunkAt(x, z); @@ -61,11 +61,11 @@ public int hashCode() { @Override public boolean equals(Object o) { - if(!(o instanceof ChunkLocation)) { + if (!(o instanceof ChunkLocation)) { return false; } - if(o == this) { + if (o == this) { return true; } diff --git a/src/main/java/de/themoep/entitydetection/EntityDetection.java b/src/main/java/de/themoep/entitydetection/EntityDetection.java index eb9f09c..9b9af71 100644 --- a/src/main/java/de/themoep/entitydetection/EntityDetection.java +++ b/src/main/java/de/themoep/entitydetection/EntityDetection.java @@ -97,7 +97,7 @@ public boolean stopSearch(String stopper) { public void addResult(SearchResult result) { if (result.getType() == SearchType.CUSTOM && result.getSearched().size() == 1) { Set searchedEntities = result.getSearched(); - customResults.put(searchedEntities.toArray(new String[searchedEntities.size()])[0], result); + customResults.put(searchedEntities.toArray(new String[0])[0], result); } else { results.put(result.getType(), result); } @@ -144,15 +144,16 @@ public void send(CommandSender sender, SearchResult result, String resultType Component entityCounts = Component.empty(); int entitiesListed = 0; - for(Entry entityEntry : entry.getEntryCount()) { + for (Entry entityEntry : entry.getEntryCount()) { entityCounts = entityCounts.append(getMessage(sender, "result.entity-count", "type", Utils.enumToHumanName(entityEntry.getKey()), "count", String.valueOf(entityEntry.getValue()) )); entitiesListed++; - if(entitiesListed >= 3) + if (entitiesListed >= 3) { break; + } } resultLine = Replacer.replaceIn(resultLine, "entitycounts", entityCounts); diff --git a/src/main/java/de/themoep/entitydetection/Utils.java b/src/main/java/de/themoep/entitydetection/Utils.java index f0bf553..b9d9343 100644 --- a/src/main/java/de/themoep/entitydetection/Utils.java +++ b/src/main/java/de/themoep/entitydetection/Utils.java @@ -42,11 +42,11 @@ public static String enumToHumanName(Enum convert) { */ public static String enumToHumanName(String convert) { String[] parts = convert.split("_"); - if(parts.length == 0) { + if (parts.length == 0) { return ""; } String human = capitalize(parts[0]); - for(int i = 1; i < parts.length; i++) { + for (int i = 1; i < parts.length; i++) { human += " " + capitalize(parts[i]); } return human; diff --git a/src/main/java/de/themoep/entitydetection/commands/ListSubCommand.java b/src/main/java/de/themoep/entitydetection/commands/ListSubCommand.java index fd8f282..1944795 100644 --- a/src/main/java/de/themoep/entitydetection/commands/ListSubCommand.java +++ b/src/main/java/de/themoep/entitydetection/commands/ListSubCommand.java @@ -36,29 +36,27 @@ public boolean execute(CommandSender sender, String[] args) { int page = 1; String lastName = sender.getName(); String resultType = ""; - if(args.length > 0) { + if (args.length > 0) { try { page = Integer.parseInt(args[0]); - } catch(NumberFormatException e) { + } catch (NumberFormatException e) { sender.sendMessage(ChatColor.YELLOW + args[0] + ChatColor.RED + " is not a proper number input!"); return false; } - if(args.length > 1) { + if (args.length > 1) { lastName = args[1]; String arg = args[1]; - if(arg.endsWith("s")) { + if (arg.endsWith("s")) { arg = arg.substring(0, arg.length() - 1); } boolean found = false; - if (!found) { - try { - result = getPlugin().getResult(EntityType.valueOf(arg.toUpperCase()).toString()); - found = true; - } catch (IllegalArgumentException ignored) {} - } + try { + result = getPlugin().getResult(EntityType.valueOf(arg.toUpperCase()).toString()); + found = true; + } catch (IllegalArgumentException ignored) {} if (!found) { try { result = getPlugin().getResult(Class.forName("org.bukkit.block." + arg, false, getPlugin().getServer().getClass().getClassLoader()).getSimpleName()); //TODO: This is case sensitive :( @@ -75,7 +73,7 @@ public boolean execute(CommandSender sender, String[] args) { try { result = getPlugin().getResult(SearchType.getByAlias(arg.toUpperCase())); found = true; - } catch(IllegalArgumentException ignored) {} + } catch (IllegalArgumentException ignored) {} } if (!found) { try { @@ -91,12 +89,12 @@ public boolean execute(CommandSender sender, String[] args) { } } - if(result == null) { + if (result == null) { sender.sendMessage(ChatColor.RED + "No previous result for " + lastName + " found!"); return true; } - if(page == 0 || result.getSortedEntries().size() < (page - 1) * 10) { + if (page == 0 || result.getSortedEntries().size() < (page - 1) * 10) { sender.sendMessage(ChatColor.RED + "There is no page " + page + " in the last " + lastName + " result!"); return true; } diff --git a/src/main/java/de/themoep/entitydetection/commands/PluginCommandExecutor.java b/src/main/java/de/themoep/entitydetection/commands/PluginCommandExecutor.java index ec3eee2..0c25c57 100644 --- a/src/main/java/de/themoep/entitydetection/commands/PluginCommandExecutor.java +++ b/src/main/java/de/themoep/entitydetection/commands/PluginCommandExecutor.java @@ -44,16 +44,16 @@ public PluginCommandExecutor(EntityDetection plugin) { } public void register(SubCommand sub) { - if(!subCommands.containsKey(sub.getCommand())) { + if (!subCommands.containsKey(sub.getCommand())) { subCommands.put(sub.getCommand(), new LinkedHashMap()); } - if(subCommands.get(sub.getCommand()).containsKey(sub.getPath())) { + if (subCommands.get(sub.getCommand()).containsKey(sub.getPath())) { throw new IllegalArgumentException("A sub command with the path '" + sub.getPath() + "' is already defined for command '" + sub.getCommand() + "'!"); } subCommands.get(sub.getCommand()).put(sub.getPath(), sub); try { plugin.getServer().getPluginManager().addPermission(sub.getPermission()); - } catch(IllegalArgumentException ignore) { + } catch (IllegalArgumentException ignore) { // Permission was already defined correctly in the plugin.yml } } diff --git a/src/main/java/de/themoep/entitydetection/commands/SearchSubCommand.java b/src/main/java/de/themoep/entitydetection/commands/SearchSubCommand.java index 29f0d31..210220d 100644 --- a/src/main/java/de/themoep/entitydetection/commands/SearchSubCommand.java +++ b/src/main/java/de/themoep/entitydetection/commands/SearchSubCommand.java @@ -35,8 +35,8 @@ public SearchSubCommand(EntityDetection plugin) { @Override public boolean execute(CommandSender sender, String[] args) { EntitySearch search = new EntitySearch(getPlugin(), sender); - if(args.length > 0) { - for(String arg : args) { + if (args.length > 0) { + for (String arg : args) { if ("--regions".equalsIgnoreCase(arg)) { Plugin plugin = Bukkit.getPluginManager().getPlugin("WorldGuard"); if (plugin != null && plugin.isEnabled() && plugin.getDescription().getVersion().startsWith("7")) @@ -52,12 +52,10 @@ public boolean execute(CommandSender sender, String[] args) { arg = arg.substring(0, arg.length() - 1); } boolean found = false; - if (!found) { - try { - search.addSearchedType(EntityType.valueOf(arg.toUpperCase())); - found = true; - } catch (IllegalArgumentException ignored) {} - } + try { + search.addSearchedType(EntityType.valueOf(arg.toUpperCase())); + found = true; + } catch (IllegalArgumentException ignored) {} if (!found) { try { search.addSearchedBlockState(Class.forName("org.bukkit.block." + arg, false, getPlugin().getServer().getClass().getClassLoader())); //TODO: This is case sensitive :( @@ -74,7 +72,7 @@ public boolean execute(CommandSender sender, String[] args) { try { search.setType(SearchType.getByAlias(arg.toUpperCase())); found = true; - } catch(IllegalArgumentException ignored) {} + } catch (IllegalArgumentException ignored) {} } if (!found) { try { @@ -89,7 +87,7 @@ public boolean execute(CommandSender sender, String[] args) { } else { search.setType(SearchType.MONSTER); } - if(!getPlugin().startSearch(search)) { + if (!getPlugin().startSearch(search)) { sender.sendMessage(ChatColor.YELLOW + search.getOwner() + ChatColor.RED + " already started a search!"); } return true; diff --git a/src/main/java/de/themoep/entitydetection/commands/StopSubCommand.java b/src/main/java/de/themoep/entitydetection/commands/StopSubCommand.java index bc76dff..03dd5b2 100644 --- a/src/main/java/de/themoep/entitydetection/commands/StopSubCommand.java +++ b/src/main/java/de/themoep/entitydetection/commands/StopSubCommand.java @@ -29,7 +29,7 @@ public StopSubCommand(EntityDetection plugin) { @Override public boolean execute(CommandSender sender, String[] args) { - if(getPlugin().stopSearch(sender.getName())) { + if (getPlugin().stopSearch(sender.getName())) { EntitySearch search = getPlugin().getCurrentSearch(); sender.sendMessage(ChatColor.YELLOW + "Stopped " + search.getType().toString() + " search by " + search.getOwner() + " after " + search.getDuration() + "s!" ); } else { diff --git a/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java b/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java index 3c5ff8b..727bdf0 100644 --- a/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java +++ b/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java @@ -31,19 +31,19 @@ public TpSubCommand(EntityDetection plugin) { @Override public boolean execute(CommandSender sender, String[] args) { - if(!(sender instanceof Player)) { + if (!(sender instanceof Player)) { sender.sendMessage(ChatColor.RED + "This sub command can only be run by a player!"); return true; } - if(args.length == 0) { + if (args.length == 0) { return false; } return teleport((Player)sender, args[0], getPlugin().getResult(sender)); } private boolean teleport(Player sender, String page, SearchResult lastResult) { - if(lastResult == null) { + if (lastResult == null) { sender.sendMessage(ChatColor.RED + "You have to view a search result before teleporting to an entry! Use /detect search or /detect list []"); return true; } @@ -51,12 +51,12 @@ private boolean teleport(Player sender, String page, SearchResult lastRes int i; try { i = Integer.parseInt(page); - } catch(NumberFormatException e) { + } catch (NumberFormatException e) { sender.sendMessage(ChatColor.YELLOW + page + ChatColor.RED + " is not a proper number input!"); return false; } - if(i == 0 || lastResult.getSortedEntries().size() < i) { + if (i == 0 || lastResult.getSortedEntries().size() < i) { sender.sendMessage(ChatColor.RED + "Result " + ChatColor.YELLOW + page + ChatColor.RED + " is not in the list!"); return true; } diff --git a/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java b/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java index 446c69d..8866a26 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java +++ b/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java @@ -19,16 +19,31 @@ public ChunkSearchResult(EntitySearch search) { @Override public void addEntity(Entity entity) { - add(entity.getLocation(), entity.getType().toString()); + if (entity == null || !entity.isValid()) { + return; + } + Location loc = entity.getLocation(); + if (loc.getWorld() != null) { + add(loc, entity.getType().toString()); + } } @Override public void addBlockState(BlockState blockState) { - add(blockState.getLocation(), blockState.getType().toString()); + if (blockState == null) { + return; + } + Location loc = blockState.getLocation(); + if (loc.getWorld() != null) { + add(loc, blockState.getType().toString()); + } } @Override public void add(Location location, String type) { + if (location == null || location.getWorld() == null) { + return; + } ChunkLocation chunkLocation = new ChunkLocation(location); if (!resultEntryMap.containsKey(chunkLocation)) { @@ -53,19 +68,26 @@ public void teleport(Player sender, SearchResultEntry entry, int Location location = new Location(targetWorld, anchorX, 64, anchorZ); scheduler.runAtLocation(location, task -> targetWorld.getChunkAtAsync(cx, cz, false, chunk -> { + if (chunk == null) { + sender.sendMessage(ChatColor.RED + "Chunk " + ChatColor.WHITE + cx + ", " + cz + ChatColor.RED + " could not be loaded."); + return; + } Location loc = null; for (Entity e : chunk.getEntities()) { - if (e.getType().toString().equals(entry.getEntryCount().get(0).getKey())) { + if (e.isValid() && e.getType().toString().equals(entry.getEntryCount().get(0).getKey())) { loc = e.getLocation(); break; } } - for (BlockState b : chunk.getTileEntities()) { - if (b.getType().toString().equals(entry.getEntryCount().get(0).getKey())) { - loc = b.getLocation().add(0, 1, 0); - break; + if (loc == null) { + for (BlockState b : chunk.getTileEntities()) { + if (b.getType().toString().equals(entry.getEntryCount().get(0).getKey())) { + Location blockLoc = b.getLocation(); + loc = blockLoc.add(0, 1, 0); + break; + } } } @@ -84,7 +106,7 @@ public void teleport(Player sender, SearchResultEntry entry, int ); })); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { sender.sendMessage(ChatColor.RED + e.getMessage()); } } diff --git a/src/main/java/de/themoep/entitydetection/searcher/EntitySearch.java b/src/main/java/de/themoep/entitydetection/searcher/EntitySearch.java index c399a89..e1b0638 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/EntitySearch.java +++ b/src/main/java/de/themoep/entitydetection/searcher/EntitySearch.java @@ -69,7 +69,7 @@ public SearchType getType() { } public void setType(SearchType type) { - if(getSearchedEntities().size() == 0 && getSearchedBlockStates().size() == 0 && getSearchedMaterial().size() == 0) { + if (getSearchedEntities().size() == 0 && getSearchedBlockStates().size() == 0 && getSearchedMaterial().size() == 0) { this.type = type; } else { this.type = SearchType.CUSTOM; @@ -183,7 +183,7 @@ public boolean isRunning() { public void stop(String name) { running = false; - if(!owner.getName().equals(name)) { + if (!owner.getName().equals(name)) { owner.sendMessage(ChatColor.YELLOW + name + ChatColor.RED + " stopped your " + getType() + " search after " + getDuration() + "s!"); } } diff --git a/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java b/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java index df59500..9094f10 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java +++ b/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java @@ -114,7 +114,7 @@ public List> getSortedEntries() { * Sort the results and set the end time */ public void sort() { - for(SearchResultEntry chunkEntry : resultEntryMap.values()) { + for (SearchResultEntry chunkEntry : resultEntryMap.values()) { chunkEntry.sort(); } resultEntryList = new ArrayList<>(resultEntryMap.values()); diff --git a/src/main/java/de/themoep/entitydetection/searcher/SearchType.java b/src/main/java/de/themoep/entitydetection/searcher/SearchType.java index 63536a1..c889898 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/SearchType.java +++ b/src/main/java/de/themoep/entitydetection/searcher/SearchType.java @@ -100,13 +100,13 @@ public enum SearchType { List classList = new LinkedList<>(Arrays.asList(classes)); - if(classes.length > 0) { - for(EntityType et : EntityType.values()) { - if(typeSet.contains(et)) { + if (classes.length > 0) { + for (EntityType et : EntityType.values()) { + if (typeSet.contains(et)) { continue; } Class e = et.getEntityClass(); - if(e == null) { + if (e == null) { continue; } for (Class eClass : classList) { @@ -152,9 +152,9 @@ public enum SearchType { * @throws IllegalArgumentException Thrown when there is no sub command with this alias */ public static SearchType getByAlias(String alias) throws IllegalArgumentException{ - for(SearchType type : SearchType.values()) { - for(String a : type.aliases) { - if(a.equals(alias)) { + for (SearchType type : SearchType.values()) { + for (String a : type.aliases) { + if (a.equals(alias)) { return type; } } diff --git a/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java b/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java index c9c5fa5..99b54a1 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java +++ b/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java @@ -71,7 +71,7 @@ public void teleport(Player sender, SearchResultEntry