Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.themoep</groupId>
<artifactId>entitydetection</artifactId>
<version>1.1.3</version>
<version>1.2.0</version>
<description>Bukkit plugin to find groups of mobs, animals or other (tile) entities.</description>
<name>EntityDetection</name>

Expand All @@ -22,13 +22,24 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>enginehub-repo</id>
<url>https://maven.enginehub.org/repo/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-bukkit</artifactId>
<version>7.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
31 changes: 15 additions & 16 deletions src/main/java/de/themoep/entitydetection/EntityDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -49,9 +48,9 @@ public class EntityDetection extends JavaPlugin {

private EntitySearch currentSearch;

private Map<SearchType, SearchResult> results = new HashMap<SearchType, SearchResult>();
private Map<String, SearchResult> customResults = new HashMap<String, SearchResult>();
private Map<String, SearchResult> lastResultViewed = new HashMap<String, SearchResult>();
private Map<SearchType, SearchResult<?>> results = new HashMap<>();
private Map<String, SearchResult<?>> customResults = new HashMap<>();
private Map<String, SearchResult<?>> lastResultViewed = new HashMap<>();

private boolean serverIsSpigot = true;

Expand Down Expand Up @@ -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<String> searchedEntities = result.getSearched();
customResults.put(searchedEntities.toArray(new String[searchedEntities.size()])[0], result);
Expand All @@ -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()));
Expand All @@ -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<SearchResultEntry> results = result.getSortedEntries();
List<? extends SearchResultEntry<?>> 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)
Expand All @@ -142,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);
Expand Down Expand Up @@ -171,12 +170,12 @@ public void send(CommandSender sender, SearchResult result, int page) {
List<String> msg = new ArrayList<String>();
msg.add(ChatColor.GREEN + Utils.enumToHumanName(result.getType()) + " search " + ChatColor.WHITE + "from " + dateStr + ":");

List<SearchResultEntry> chunkEntries = result.getSortedEntries();
List<? extends SearchResultEntry<?>> 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() + " ";
String lineText = ChatColor.WHITE + " " + (line + 1) + ": " + ChatColor.YELLOW + chunkEntry.getLocation() + " " + ChatColor.RED + chunkEntry.getSize() + " ";

int entitiesListed = 0;
for(Entry<String, Integer> entityEntry : chunkEntry.getEntryCount()) {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,61 +46,30 @@ 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 <T> boolean teleport(Player sender, String page, SearchResult<T> 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 [<type>]");
return true;
}

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);

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<T> entry = lastResult.getSortedEntries().get(i - 1);
lastResult.teleport(sender, entry, i);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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<ChunkLocation> {
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<ChunkLocation> entry, int i) {
try {
Chunk chunk = entry.getLocation().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.getLocation() + " " + 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class EntitySearch extends BukkitRunnable {
private List<Entity> entities = new ArrayList<Entity>();
private List<BlockState> blockStates = new ArrayList<BlockState>();

private boolean isWorldGuardRegion = false;

public EntitySearch(EntityDetection plugin, CommandSender sender) {
this.plugin = plugin;
owner = sender;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -159,7 +170,7 @@ public void run() {
}

result.sort();
plugin.addResult(result);;
plugin.addResult(result);
plugin.send(owner, result);
running = false;
}
Expand Down
Loading