From b4f4d54787c903dcffc0e0b027e40778278f226c Mon Sep 17 00:00:00 2001 From: JOO200 Date: Sun, 24 May 2020 23:09:01 +0200 Subject: [PATCH 1/3] Implemented WorldGuard 7.x region search, version is now 1.2.0 --- pom.xml | 11 +- .../entitydetection/EntityDetection.java | 27 +++-- .../commands/ListSubCommand.java | 2 +- .../commands/SearchSubCommand.java | 13 +++ .../commands/TpSubCommand.java | 40 +------ .../searcher/ChunkSearchResult.java | 77 ++++++++++++++ .../searcher/EntitySearch.java | 15 ++- .../searcher/SearchResult.java | 31 +++--- .../searcher/SearchResultEntry.java | 22 ++-- .../searcher/WGSearchResult.java | 100 ++++++++++++++++++ src/main/resources/plugin.yml | 1 + 11 files changed, 251 insertions(+), 88 deletions(-) create mode 100644 src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java create mode 100644 src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java diff --git a/pom.xml b/pom.xml index 29c9764..85631f4 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.themoep entitydetection - 1.1.3 + 1.2.0 Bukkit plugin to find groups of mobs, animals or other (tile) entities. EntityDetection @@ -22,6 +22,10 @@ spigot-repo https://hub.spigotmc.org/nexus/content/groups/public/ + + enginehub-repo + https://maven.enginehub.org/repo/ + @@ -30,6 +34,11 @@ spigot-api 1.13.2-R0.1-SNAPSHOT + + com.sk89q.worldguard + worldguard-bukkit + 7.0.2 + diff --git a/src/main/java/de/themoep/entitydetection/EntityDetection.java b/src/main/java/de/themoep/entitydetection/EntityDetection.java index f3915e8..b29bb8f 100644 --- a/src/main/java/de/themoep/entitydetection/EntityDetection.java +++ b/src/main/java/de/themoep/entitydetection/EntityDetection.java @@ -16,7 +16,6 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; -import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -49,9 +48,9 @@ public class EntityDetection extends JavaPlugin { private EntitySearch currentSearch; - private Map results = new HashMap(); - private Map customResults = new HashMap(); - private Map lastResultViewed = new HashMap(); + private Map> results = new HashMap<>(); + private Map> customResults = new HashMap<>(); + private Map> lastResultViewed = new HashMap<>(); private boolean serverIsSpigot = true; @@ -84,7 +83,7 @@ public boolean stopSearch(String stopper) { return true; } - public void addResult(SearchResult result) { + 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); @@ -97,12 +96,12 @@ public EntitySearch getCurrentSearch() { return currentSearch; } - public void send(CommandSender sender, SearchResult result) { + public void send(CommandSender sender, SearchResult result) { send(sender, result, 0); } - public void send(CommandSender sender, SearchResult result, int page) { + public void send(CommandSender sender, SearchResult result, int page) { lastResultViewed.put(sender.getName(), result); String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(result.getEndTime())); @@ -124,10 +123,10 @@ public void send(CommandSender sender, SearchResult result, int page) { .append("from " + dateStr + ":") .color(net.md_5.bungee.api.ChatColor.WHITE); - List results = result.getSortedEntries(); + List> results = result.getSortedEntries(); if(results.size() > 0) { for(int line = start; line < start + 10 && line < results.size(); line++) { - SearchResultEntry entry = results.get(line); + SearchResultEntry entry = results.get(line); builder.append("\n") .retain(ComponentBuilder.FormatRetention.NONE) @@ -171,10 +170,10 @@ public void send(CommandSender sender, SearchResult result, int page) { List msg = new ArrayList(); msg.add(ChatColor.GREEN + Utils.enumToHumanName(result.getType()) + " search " + ChatColor.WHITE + "from " + dateStr + ":"); - List chunkEntries = result.getSortedEntries(); + List> chunkEntries = result.getSortedEntries(); if(chunkEntries.size() > 0) { for(int line = start; line < start + 10 && line < chunkEntries.size(); line++) { - SearchResultEntry chunkEntry = chunkEntries.get(line); + SearchResultEntry chunkEntry = chunkEntries.get(line); String lineText = ChatColor.WHITE + " " + (line + 1) + ": " + ChatColor.YELLOW + chunkEntry.getChunk() + " " + ChatColor.RED + chunkEntry.getSize() + " "; @@ -195,15 +194,15 @@ public void send(CommandSender sender, SearchResult result, int page) { } } - public SearchResult getResult(CommandSender sender) { + public SearchResult getResult(CommandSender sender) { return lastResultViewed.get(sender.getName()); } - public SearchResult getResult(String type) { + public SearchResult getResult(String type) { return customResults.get(type); } - public SearchResult getResult(SearchType type) { + public SearchResult getResult(SearchType type) { return results.get(type); } } diff --git a/src/main/java/de/themoep/entitydetection/commands/ListSubCommand.java b/src/main/java/de/themoep/entitydetection/commands/ListSubCommand.java index c9a8206..8e99e90 100644 --- a/src/main/java/de/themoep/entitydetection/commands/ListSubCommand.java +++ b/src/main/java/de/themoep/entitydetection/commands/ListSubCommand.java @@ -39,7 +39,7 @@ public ListSubCommand(EntityDetection plugin) { @Override public boolean execute(CommandSender sender, String[] args) { - SearchResult result = getPlugin().getResult(sender); + SearchResult result = getPlugin().getResult(sender); int page = 1; String lastName = sender.getName(); if(args.length > 0) { diff --git a/src/main/java/de/themoep/entitydetection/commands/SearchSubCommand.java b/src/main/java/de/themoep/entitydetection/commands/SearchSubCommand.java index 58f6be2..f99e807 100644 --- a/src/main/java/de/themoep/entitydetection/commands/SearchSubCommand.java +++ b/src/main/java/de/themoep/entitydetection/commands/SearchSubCommand.java @@ -3,10 +3,12 @@ import de.themoep.entitydetection.EntityDetection; import de.themoep.entitydetection.searcher.EntitySearch; import de.themoep.entitydetection.searcher.SearchType; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.entity.EntityType; +import org.bukkit.plugin.Plugin; /** * Copyright 2016 Max Lee (https://github.com/Phoenix616/) @@ -36,6 +38,17 @@ public boolean execute(CommandSender sender, String[] args) { EntitySearch search = new EntitySearch(getPlugin(), sender); 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")) + search.setWorldGuardRegion(true); + else { + sender.sendMessage(ChatColor.RED + "Unable to start WorldGuard search. WorldGuard not enabled or outdated!"); + return true; + } + if (args.length == 1) search.setType(SearchType.MONSTER); + continue; + } if (arg.endsWith("s")) { arg = arg.substring(0, arg.length() - 1); } diff --git a/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java b/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java index 90adeb9..ba92fec 100644 --- a/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java +++ b/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java @@ -46,7 +46,7 @@ public boolean execute(CommandSender sender, String[] args) { if(args.length == 0) { return false; } - SearchResult lastResult = getPlugin().getResult(sender); + SearchResult lastResult = getPlugin().getResult(sender); 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; @@ -65,42 +65,8 @@ public boolean execute(CommandSender sender, String[] args) { return true; } - SearchResultEntry entry = lastResult.getSortedEntries().get(i - 1); - - try { - Chunk chunk = entry.getChunk().toBukkit(getPlugin().getServer()); - - Location loc = null; - - for(Entity e : chunk.getEntities()) { - if(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) { - loc = chunk.getWorld().getHighestBlockAt(chunk.getX() * 16 + 8, chunk.getZ() * 16 + 8).getLocation().add(0, 2, 0); - } - - ((Player) sender).teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN); - sender.sendMessage( - ChatColor.GREEN + "Teleported to entry " + ChatColor.WHITE + i + ": " + - ChatColor.YELLOW + entry.getChunk() + " " + ChatColor.RED + entry.getSize() + " " + - ChatColor.GREEN + Utils.enumToHumanName(entry.getEntryCount().get(0).getKey()) + "[" + - ChatColor.WHITE + entry.getEntryCount().get(0).getValue() + ChatColor.GREEN + "]" - ); - - } catch(IllegalArgumentException e) { - sender.sendMessage(ChatColor.RED + e.getMessage()); - } + SearchResultEntry entry = lastResult.getSortedEntries().get(i - 1); + lastResult.teleport((Player)sender, entry, i); return true; } } diff --git a/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java b/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java new file mode 100644 index 0000000..96599eb --- /dev/null +++ b/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java @@ -0,0 +1,77 @@ +package de.themoep.entitydetection.searcher; + +import de.themoep.entitydetection.ChunkLocation; +import de.themoep.entitydetection.Utils; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.block.BlockState; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTeleportEvent; + +public class ChunkSearchResult extends SearchResult { + public ChunkSearchResult(EntitySearch search) { + super(search); + } + + @Override + public void addEntity(Entity entity) { + add(entity.getLocation(), entity.getType().toString()); + } + + @Override + public void addBlockState(BlockState blockState) { + add(blockState.getLocation(), blockState.getType().toString()); + } + + @Override + public void add(Location location, String type) { + ChunkLocation chunkLocation = new ChunkLocation(location); + + if(!resultEntryMap.containsKey(chunkLocation)) { + resultEntryMap.put(chunkLocation, new SearchResultEntry<>(chunkLocation)); + } + resultEntryMap.get(chunkLocation).increment(type); + } + + @Override + public void teleport(Player sender, SearchResultEntry oldEntry, int i) { + SearchResultEntry entry = (SearchResultEntry)oldEntry; + try { + Chunk chunk = entry.getChunk().toBukkit(Bukkit.getServer()); + + Location loc = null; + + for(Entity e : chunk.getEntities()) { + if(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) { + loc = chunk.getWorld().getHighestBlockAt(chunk.getX() * 16 + 8, chunk.getZ() * 16 + 8).getLocation().add(0, 2, 0); + } + + sender.teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN); + sender.sendMessage( + ChatColor.GREEN + "Teleported to entry " + ChatColor.WHITE + i + ": " + + ChatColor.YELLOW + entry.getChunk() + " " + ChatColor.RED + entry.getSize() + " " + + ChatColor.GREEN + Utils.enumToHumanName(entry.getEntryCount().get(0).getKey()) + "[" + + ChatColor.WHITE + entry.getEntryCount().get(0).getValue() + ChatColor.GREEN + "]" + ); + + } 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 cd4a2c4..0c54061 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/EntitySearch.java +++ b/src/main/java/de/themoep/entitydetection/searcher/EntitySearch.java @@ -46,6 +46,8 @@ public class EntitySearch extends BukkitRunnable { private List entities = new ArrayList(); private List blockStates = new ArrayList(); + private boolean isWorldGuardRegion = false; + public EntitySearch(EntityDetection plugin, CommandSender sender) { this.plugin = plugin; owner = sender; @@ -100,6 +102,13 @@ public long getStartTime() { return startTime; } + public boolean isWorldGuardRegion() { + return this.isWorldGuardRegion; + } + + public void setWorldGuardRegion(boolean value) { + this.isWorldGuardRegion = value; + } /** * Get the duration since this search started * @return The duration in seconds @@ -138,7 +147,9 @@ public void stop(String name) { public void run() { startTime = System.currentTimeMillis(); - SearchResult result = new SearchResult(this); + SearchResult result; + if(isWorldGuardRegion) result = new WGSearchResult(this); + else result = new ChunkSearchResult(this); for(Entity e : entities) { if(!running) { @@ -159,7 +170,7 @@ public void run() { } result.sort(); - plugin.addResult(result);; + plugin.addResult(result); plugin.send(owner, result); running = false; } diff --git a/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java b/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java index ee0d6e3..2f54bd8 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java +++ b/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java @@ -1,10 +1,11 @@ package de.themoep.entitydetection.searcher; -import de.themoep.entitydetection.ChunkLocation; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.BlockState; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.Collections; @@ -29,7 +30,7 @@ * You should have received a copy of the Mozilla Public License v2.0 * along with this program. If not, see . */ -public class SearchResult { +public abstract class SearchResult { private SearchType type; private Set searched; private long startTime; @@ -38,12 +39,12 @@ public class SearchResult { /** * Working search map, use resultEntryList after sorting this result! */ - private Map resultEntryMap = new HashMap(); + protected Map> resultEntryMap = new HashMap<>(); /** * Sorted, highest entity count per chunks first, only propagated after running .sort() */ - private List resultEntryList = new ArrayList(); + protected List> resultEntryList = new ArrayList<>(); public SearchResult(EntitySearch search) { type = search.getType(); @@ -64,24 +65,15 @@ public SearchResult(EntitySearch search) { * Add an entity to this result * @param entity The entity to add */ - public void addEntity(Entity entity) { - add(new ChunkLocation(entity.getLocation()), entity.getType().toString()); - } + public abstract void addEntity(Entity entity); /** * Add a BlockState to this result * @param blockState The entity to add */ - public void addBlockState(BlockState blockState) { - add(new ChunkLocation(blockState.getLocation()), blockState.getType().toString()); - } + public abstract void addBlockState(BlockState blockState); - public void add(ChunkLocation chunkLoc, String type) { - if(!resultEntryMap.containsKey(chunkLoc)) { - resultEntryMap.put(chunkLoc, new SearchResultEntry(chunkLoc)); - } - resultEntryMap.get(chunkLoc).increment(type); - } + public abstract void add(Location location, String type); public SearchType getType() { return type; @@ -107,7 +99,7 @@ public Set getSearched() { * Get a list of entries for every chunk, only propagated after calling sort() * @return An ArrayList of the chunks sorted from the highest */ - public List getSortedEntries() { + public List> getSortedEntries() { return resultEntryList; } @@ -115,13 +107,14 @@ 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()); + resultEntryList = new ArrayList<>(resultEntryMap.values()); Collections.sort(resultEntryList, Collections.reverseOrder()); endTime = System.currentTimeMillis(); } + public abstract void teleport(Player sender, SearchResultEntry entry, int i); } diff --git a/src/main/java/de/themoep/entitydetection/searcher/SearchResultEntry.java b/src/main/java/de/themoep/entitydetection/searcher/SearchResultEntry.java index 47b75c5..9ef2d97 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/SearchResultEntry.java +++ b/src/main/java/de/themoep/entitydetection/searcher/SearchResultEntry.java @@ -1,7 +1,5 @@ package de.themoep.entitydetection.searcher; -import de.themoep.entitydetection.ChunkLocation; - import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -24,14 +22,14 @@ * You should have received a copy of the Mozilla Public License v2.0 * along with this program. If not, see . */ -public class SearchResultEntry implements Comparable { - private ChunkLocation chunk; +public class SearchResultEntry implements Comparable> { + private T location; private Map entryCount = new HashMap(); private List> finalList = new ArrayList>(); private int size = 0; - SearchResultEntry(ChunkLocation chunk) { - this.chunk = chunk; + SearchResultEntry(T location) { + this.location = location; } public void increment(String type) { @@ -47,8 +45,8 @@ public int getSize() { return size; } - public ChunkLocation getChunk() { - return chunk; + public T getChunk() { + return location; } public List> getEntryCount() { @@ -56,12 +54,8 @@ public List> getEntryCount() { } public void sort() { - finalList = new ArrayList>(entryCount.entrySet()); - Collections.sort(finalList, Collections.reverseOrder(new Comparator>() { - public int compare(Map.Entry o1, Map.Entry o2) { - return Integer.compare(o1.getValue(), o2.getValue()); - } - })); + finalList = new ArrayList<>(entryCount.entrySet()); + finalList.sort(Collections.reverseOrder(Comparator.comparingInt(Map.Entry::getValue))); } public int compareTo(SearchResultEntry o) { diff --git a/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java b/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java new file mode 100644 index 0000000..9dbd590 --- /dev/null +++ b/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java @@ -0,0 +1,100 @@ +package de.themoep.entitydetection.searcher; + +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldguard.WorldGuard; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.flags.Flags; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.sk89q.worldguard.protection.regions.RegionQuery; +import de.themoep.entitydetection.Utils; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.BlockState; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTeleportEvent; + +import java.util.Objects; + +public class WGSearchResult extends SearchResult { + public WGSearchResult(EntitySearch search) { + super(search); + } + + @Override + public void addEntity(Entity entity) { + add(entity.getLocation(), entity.getType().toString()); + } + + @Override + public void addBlockState(BlockState blockState) { + add(blockState.getLocation(), blockState.getType().toString()); + } + + @Override + public void add(Location location, String type) { + RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery(); + ApplicableRegionSet applicableRegions = query.getApplicableRegions(BukkitAdapter.adapt(location)); + + applicableRegions.forEach(region -> { + ProtectedRegionEntry protectedRegionEntry = new ProtectedRegionEntry(location.getWorld(), region); + if(!resultEntryMap.containsKey(protectedRegionEntry)) { + resultEntryMap.put(protectedRegionEntry, new SearchResultEntry<>(protectedRegionEntry)); + } + resultEntryMap.get(protectedRegionEntry).increment(type); + }); + } + + @Override + public void teleport(Player sender, SearchResultEntry oldEntry, int i) { + SearchResultEntry entry = (SearchResultEntry) oldEntry; + com.sk89q.worldedit.util.Location wgLocation = entry.getChunk().region.getFlag(Flags.TELE_LOC); + try { + Location loc = wgLocation != null ? BukkitAdapter.adapt(wgLocation) : null; + if(loc == null) { + loc = BukkitAdapter.adapt(entry.getChunk().world, entry.getChunk().region.getMinimumPoint().add( + entry.getChunk().region.getMaximumPoint().subtract(entry.getChunk().region.getMinimumPoint()).divide(2))); + } + + sender.teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN); + sender.sendMessage( + ChatColor.GREEN + "Teleported to entry " + ChatColor.WHITE + i + ": " + + ChatColor.YELLOW + entry.getChunk().region.getId() + " " + ChatColor.RED + entry.getSize() + " " + + ChatColor.GREEN + Utils.enumToHumanName(entry.getEntryCount().get(0).getKey()) + "[" + + ChatColor.WHITE + entry.getEntryCount().get(0).getValue() + ChatColor.GREEN + "]" + ); + } catch(IllegalArgumentException e) { + sender.sendMessage(ChatColor.RED + e.getMessage()); + } + } + + public static class ProtectedRegionEntry { + World world; + ProtectedRegion region; + + public ProtectedRegionEntry(World world, ProtectedRegion region) { + this.world = world; + this.region = region; + } + + @Override + public String toString() { + return "w: " + world.getName() + ", r: " + region.getId(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ProtectedRegionEntry that = (ProtectedRegionEntry) o; + return Objects.equals(world, that.world) && + Objects.equals(region, that.region); + } + + @Override + public int hashCode() { + return Objects.hash(world, region); + } + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 43bc67c..0da981f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,6 +3,7 @@ main: de.themoep.entitydetection.EntityDetection version: '${minecraft.plugin.version}' api-version: 1.13 description: '${project.description}' +softdepend: ["WorldGuard"] authors: [Phoenix616] commands: entitydetection: From 48e5627432907156733df7b638737c2596013bf9 Mon Sep 17 00:00:00 2001 From: JOO200 Date: Sun, 24 May 2020 23:53:31 +0200 Subject: [PATCH 2/3] Cleanup Generic usage, don't make unsafe casts --- .../entitydetection/commands/TpSubCommand.java | 15 +++++++++------ .../searcher/ChunkSearchResult.java | 3 +-- .../entitydetection/searcher/SearchResult.java | 2 +- .../entitydetection/searcher/WGSearchResult.java | 3 +-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java b/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java index ba92fec..4200c79 100644 --- a/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java +++ b/src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java @@ -46,7 +46,10 @@ public boolean execute(CommandSender sender, String[] args) { if(args.length == 0) { return false; } - SearchResult lastResult = getPlugin().getResult(sender); + return teleport((Player)sender, args[0], getPlugin().getResult(sender)); + } + + private boolean teleport(Player sender, String page, SearchResult lastResult) { 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; @@ -54,19 +57,19 @@ public boolean execute(CommandSender sender, String[] args) { int i; try { - i = Integer.parseInt(args[0]); + i = Integer.parseInt(page); } catch(NumberFormatException e) { - sender.sendMessage(ChatColor.YELLOW + args[0] + ChatColor.RED + " is not a proper number input!"); + sender.sendMessage(ChatColor.YELLOW + page + ChatColor.RED + " is not a proper number input!"); return false; } if(i == 0 || lastResult.getSortedEntries().size() < i) { - sender.sendMessage(ChatColor.RED + "Result " + ChatColor.YELLOW + args[0] + ChatColor.RED + " is not in the list!"); + sender.sendMessage(ChatColor.RED + "Result " + ChatColor.YELLOW + page + ChatColor.RED + " is not in the list!"); return true; } - SearchResultEntry entry = lastResult.getSortedEntries().get(i - 1); - lastResult.teleport((Player)sender, entry, i); + SearchResultEntry entry = lastResult.getSortedEntries().get(i - 1); + lastResult.teleport(sender, entry, i); 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 96599eb..ba0a023 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java +++ b/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java @@ -37,8 +37,7 @@ public void add(Location location, String type) { } @Override - public void teleport(Player sender, SearchResultEntry oldEntry, int i) { - SearchResultEntry entry = (SearchResultEntry)oldEntry; + public void teleport(Player sender, SearchResultEntry entry, int i) { try { Chunk chunk = entry.getChunk().toBukkit(Bukkit.getServer()); diff --git a/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java b/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java index 2f54bd8..a61648c 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java +++ b/src/main/java/de/themoep/entitydetection/searcher/SearchResult.java @@ -116,5 +116,5 @@ public void sort() { endTime = System.currentTimeMillis(); } - public abstract void teleport(Player sender, SearchResultEntry entry, int i); + public abstract void teleport(Player sender, SearchResultEntry entry, int i); } diff --git a/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java b/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java index 9dbd590..438394a 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java +++ b/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java @@ -47,8 +47,7 @@ public void add(Location location, String type) { } @Override - public void teleport(Player sender, SearchResultEntry oldEntry, int i) { - SearchResultEntry entry = (SearchResultEntry) oldEntry; + public void teleport(Player sender, SearchResultEntry entry, int i) { com.sk89q.worldedit.util.Location wgLocation = entry.getChunk().region.getFlag(Flags.TELE_LOC); try { Location loc = wgLocation != null ? BukkitAdapter.adapt(wgLocation) : null; From 1ca428dc48c6a3c0e6cd333efa4ce6d0b3e81c5e Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Sun, 24 May 2020 23:24:34 +0100 Subject: [PATCH 3/3] Fix a couple of small issues - Don't keep World reference around when world gets unloaded - Rename entry's getChunk to more general getLocation - Add dependency scopes --- pom.xml | 2 ++ .../entitydetection/EntityDetection.java | 4 ++-- .../searcher/ChunkSearchResult.java | 4 ++-- .../searcher/SearchResultEntry.java | 2 +- .../searcher/WGSearchResult.java | 23 +++++++++++++------ 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 85631f4..8d4fc4e 100644 --- a/pom.xml +++ b/pom.xml @@ -33,11 +33,13 @@ org.spigotmc spigot-api 1.13.2-R0.1-SNAPSHOT + provided com.sk89q.worldguard worldguard-bukkit 7.0.2 + provided diff --git a/src/main/java/de/themoep/entitydetection/EntityDetection.java b/src/main/java/de/themoep/entitydetection/EntityDetection.java index b29bb8f..8038bf1 100644 --- a/src/main/java/de/themoep/entitydetection/EntityDetection.java +++ b/src/main/java/de/themoep/entitydetection/EntityDetection.java @@ -141,7 +141,7 @@ public void send(CommandSender sender, SearchResult result, int page) { ) ) .event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/detect tp " + (line + 1))) - .append(entry.getChunk() + " ") + .append(entry.getLocation() + " ") .color(net.md_5.bungee.api.ChatColor.YELLOW) .append(entry.getSize() + " ") .color(net.md_5.bungee.api.ChatColor.RED); @@ -175,7 +175,7 @@ public void send(CommandSender sender, SearchResult result, int page) { for(int line = start; line < start + 10 && line < chunkEntries.size(); line++) { SearchResultEntry chunkEntry = chunkEntries.get(line); - String lineText = ChatColor.WHITE + " " + (line + 1) + ": " + ChatColor.YELLOW + chunkEntry.getChunk() + " " + ChatColor.RED + chunkEntry.getSize() + " "; + String lineText = ChatColor.WHITE + " " + (line + 1) + ": " + ChatColor.YELLOW + chunkEntry.getLocation() + " " + ChatColor.RED + chunkEntry.getSize() + " "; int entitiesListed = 0; for(Entry entityEntry : chunkEntry.getEntryCount()) { diff --git a/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java b/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java index ba0a023..07ef724 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java +++ b/src/main/java/de/themoep/entitydetection/searcher/ChunkSearchResult.java @@ -39,7 +39,7 @@ public void add(Location location, String type) { @Override public void teleport(Player sender, SearchResultEntry entry, int i) { try { - Chunk chunk = entry.getChunk().toBukkit(Bukkit.getServer()); + Chunk chunk = entry.getLocation().toBukkit(Bukkit.getServer()); Location loc = null; @@ -64,7 +64,7 @@ public void teleport(Player sender, SearchResultEntry entry, int sender.teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN); sender.sendMessage( ChatColor.GREEN + "Teleported to entry " + ChatColor.WHITE + i + ": " + - ChatColor.YELLOW + entry.getChunk() + " " + ChatColor.RED + entry.getSize() + " " + + ChatColor.YELLOW + entry.getLocation() + " " + ChatColor.RED + entry.getSize() + " " + ChatColor.GREEN + Utils.enumToHumanName(entry.getEntryCount().get(0).getKey()) + "[" + ChatColor.WHITE + entry.getEntryCount().get(0).getValue() + ChatColor.GREEN + "]" ); diff --git a/src/main/java/de/themoep/entitydetection/searcher/SearchResultEntry.java b/src/main/java/de/themoep/entitydetection/searcher/SearchResultEntry.java index 9ef2d97..2c9da1d 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/SearchResultEntry.java +++ b/src/main/java/de/themoep/entitydetection/searcher/SearchResultEntry.java @@ -45,7 +45,7 @@ public int getSize() { return size; } - public T getChunk() { + public T getLocation() { return location; } diff --git a/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java b/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java index 438394a..dea4bcb 100644 --- a/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java +++ b/src/main/java/de/themoep/entitydetection/searcher/WGSearchResult.java @@ -15,6 +15,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent; +import java.lang.ref.WeakReference; import java.util.Objects; public class WGSearchResult extends SearchResult { @@ -48,18 +49,23 @@ public void add(Location location, String type) { @Override public void teleport(Player sender, SearchResultEntry entry, int i) { - com.sk89q.worldedit.util.Location wgLocation = entry.getChunk().region.getFlag(Flags.TELE_LOC); + com.sk89q.worldedit.util.Location wgLocation = entry.getLocation().region.getFlag(Flags.TELE_LOC); try { + World world = entry.getLocation().world.get(); + if(world == null) { + sender.sendMessage(ChatColor.RED + "World " + ChatColor.WHITE + entry.getLocation().worldName + ChatColor.RED + " is not loaded anymore."); + return; + } Location loc = wgLocation != null ? BukkitAdapter.adapt(wgLocation) : null; if(loc == null) { - loc = BukkitAdapter.adapt(entry.getChunk().world, entry.getChunk().region.getMinimumPoint().add( - entry.getChunk().region.getMaximumPoint().subtract(entry.getChunk().region.getMinimumPoint()).divide(2))); + loc = BukkitAdapter.adapt(world, entry.getLocation().region.getMinimumPoint().add( + entry.getLocation().region.getMaximumPoint().subtract(entry.getLocation().region.getMinimumPoint()).divide(2))); } sender.teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN); sender.sendMessage( ChatColor.GREEN + "Teleported to entry " + ChatColor.WHITE + i + ": " + - ChatColor.YELLOW + entry.getChunk().region.getId() + " " + ChatColor.RED + entry.getSize() + " " + + ChatColor.YELLOW + entry.getLocation().region.getId() + " " + ChatColor.RED + entry.getSize() + " " + ChatColor.GREEN + Utils.enumToHumanName(entry.getEntryCount().get(0).getKey()) + "[" + ChatColor.WHITE + entry.getEntryCount().get(0).getValue() + ChatColor.GREEN + "]" ); @@ -69,17 +75,20 @@ public void teleport(Player sender, SearchResultEntry world; ProtectedRegion region; public ProtectedRegionEntry(World world, ProtectedRegion region) { - this.world = world; + this.worldName = world.getName(); + this.world = new WeakReference<>(world); this.region = region; } @Override public String toString() { - return "w: " + world.getName() + ", r: " + region.getId(); + World w = world.get(); + return "w: " + worldName + (w == null ? " (unloaded)" : "") + ", r: " + region.getId(); } @Override