Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/main/java/de/themoep/entitydetection/ChunkLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/de/themoep/entitydetection/EntityDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public boolean stopSearch(String stopper) {
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);
customResults.put(searchedEntities.toArray(new String[0])[0], result);
} else {
results.put(result.getType(), result);
}
Expand Down Expand Up @@ -144,15 +144,16 @@ public void send(CommandSender sender, SearchResult<?> result, String resultType

Component entityCounts = Component.empty();
int entitiesListed = 0;
for(Entry<String, Integer> entityEntry : entry.getEntryCount()) {
for (Entry<String, Integer> 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);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/themoep/entitydetection/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 :(
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, SubCommand>());
}
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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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 :(
Expand All @@ -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 {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,32 @@ 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 <T> boolean teleport(Player sender, String page, SearchResult<T> 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 [<type>]");
return true;
}

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -53,19 +68,26 @@ public void teleport(Player sender, SearchResultEntry<ChunkLocation> 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;
}
}
}

Expand All @@ -84,7 +106,7 @@ public void teleport(Player sender, SearchResultEntry<ChunkLocation> entry, int
);
}));

} catch(IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public List<SearchResultEntry<T>> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ public enum SearchType {

List<Class> 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<? extends Entity> e = et.getEntityClass();
if(e == null) {
if (e == null) {
continue;
}
for (Class eClass : classList) {
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void teleport(Player sender, SearchResultEntry<WGSearchResult.ProtectedRe
ChatColor.GREEN + Utils.enumToHumanName(entry.getEntryCount().get(0).getKey()) + "[" +
ChatColor.WHITE + entry.getEntryCount().get(0).getValue() + ChatColor.GREEN + "]"
);
} catch(IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
}
}
Expand Down