Skip to content

Commit 52b6179

Browse files
Joo200Phoenix616
andauthored
Implement WorldGuard region search (#1)
Co-authored-by: Phoenix616 <mail@moep.tv>
1 parent c3ac941 commit 52b6179

11 files changed

Lines changed: 268 additions & 93 deletions

File tree

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>de.themoep</groupId>
88
<artifactId>entitydetection</artifactId>
9-
<version>1.1.3</version>
9+
<version>1.2.0</version>
1010
<description>Bukkit plugin to find groups of mobs, animals or other (tile) entities.</description>
1111
<name>EntityDetection</name>
1212

@@ -22,13 +22,24 @@
2222
<id>spigot-repo</id>
2323
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
2424
</repository>
25+
<repository>
26+
<id>enginehub-repo</id>
27+
<url>https://maven.enginehub.org/repo/</url>
28+
</repository>
2529
</repositories>
2630

2731
<dependencies>
2832
<dependency>
2933
<groupId>org.spigotmc</groupId>
3034
<artifactId>spigot-api</artifactId>
3135
<version>1.13.2-R0.1-SNAPSHOT</version>
36+
<scope>provided</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.sk89q.worldguard</groupId>
40+
<artifactId>worldguard-bukkit</artifactId>
41+
<version>7.0.2</version>
42+
<scope>provided</scope>
3243
</dependency>
3344
</dependencies>
3445

src/main/java/de/themoep/entitydetection/EntityDetection.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.bukkit.Bukkit;
1717
import org.bukkit.ChatColor;
1818
import org.bukkit.command.CommandSender;
19-
import org.bukkit.entity.EntityType;
2019
import org.bukkit.entity.Player;
2120
import org.bukkit.plugin.java.JavaPlugin;
2221

@@ -49,9 +48,9 @@ public class EntityDetection extends JavaPlugin {
4948

5049
private EntitySearch currentSearch;
5150

52-
private Map<SearchType, SearchResult> results = new HashMap<SearchType, SearchResult>();
53-
private Map<String, SearchResult> customResults = new HashMap<String, SearchResult>();
54-
private Map<String, SearchResult> lastResultViewed = new HashMap<String, SearchResult>();
51+
private Map<SearchType, SearchResult<?>> results = new HashMap<>();
52+
private Map<String, SearchResult<?>> customResults = new HashMap<>();
53+
private Map<String, SearchResult<?>> lastResultViewed = new HashMap<>();
5554

5655
private boolean serverIsSpigot = true;
5756

@@ -84,7 +83,7 @@ public boolean stopSearch(String stopper) {
8483
return true;
8584
}
8685

87-
public void addResult(SearchResult result) {
86+
public void addResult(SearchResult<?> result) {
8887
if(result.getType() == SearchType.CUSTOM && result.getSearched().size() == 1) {
8988
Set<String> searchedEntities = result.getSearched();
9089
customResults.put(searchedEntities.toArray(new String[searchedEntities.size()])[0], result);
@@ -97,12 +96,12 @@ public EntitySearch getCurrentSearch() {
9796
return currentSearch;
9897
}
9998

100-
public void send(CommandSender sender, SearchResult result) {
99+
public void send(CommandSender sender, SearchResult<?> result) {
101100
send(sender, result, 0);
102101
}
103102

104103

105-
public void send(CommandSender sender, SearchResult result, int page) {
104+
public void send(CommandSender sender, SearchResult<?> result, int page) {
106105
lastResultViewed.put(sender.getName(), result);
107106

108107
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) {
124123
.append("from " + dateStr + ":")
125124
.color(net.md_5.bungee.api.ChatColor.WHITE);
126125

127-
List<SearchResultEntry> results = result.getSortedEntries();
126+
List<? extends SearchResultEntry<?>> results = result.getSortedEntries();
128127
if(results.size() > 0) {
129128
for(int line = start; line < start + 10 && line < results.size(); line++) {
130-
SearchResultEntry entry = results.get(line);
129+
SearchResultEntry<?> entry = results.get(line);
131130

132131
builder.append("\n")
133132
.retain(ComponentBuilder.FormatRetention.NONE)
@@ -142,7 +141,7 @@ public void send(CommandSender sender, SearchResult result, int page) {
142141
)
143142
)
144143
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/detect tp " + (line + 1)))
145-
.append(entry.getChunk() + " ")
144+
.append(entry.getLocation() + " ")
146145
.color(net.md_5.bungee.api.ChatColor.YELLOW)
147146
.append(entry.getSize() + " ")
148147
.color(net.md_5.bungee.api.ChatColor.RED);
@@ -171,12 +170,12 @@ public void send(CommandSender sender, SearchResult result, int page) {
171170
List<String> msg = new ArrayList<String>();
172171
msg.add(ChatColor.GREEN + Utils.enumToHumanName(result.getType()) + " search " + ChatColor.WHITE + "from " + dateStr + ":");
173172

174-
List<SearchResultEntry> chunkEntries = result.getSortedEntries();
173+
List<? extends SearchResultEntry<?>> chunkEntries = result.getSortedEntries();
175174
if(chunkEntries.size() > 0) {
176175
for(int line = start; line < start + 10 && line < chunkEntries.size(); line++) {
177-
SearchResultEntry chunkEntry = chunkEntries.get(line);
176+
SearchResultEntry<?> chunkEntry = chunkEntries.get(line);
178177

179-
String lineText = ChatColor.WHITE + " " + (line + 1) + ": " + ChatColor.YELLOW + chunkEntry.getChunk() + " " + ChatColor.RED + chunkEntry.getSize() + " ";
178+
String lineText = ChatColor.WHITE + " " + (line + 1) + ": " + ChatColor.YELLOW + chunkEntry.getLocation() + " " + ChatColor.RED + chunkEntry.getSize() + " ";
180179

181180
int entitiesListed = 0;
182181
for(Entry<String, Integer> entityEntry : chunkEntry.getEntryCount()) {
@@ -195,15 +194,15 @@ public void send(CommandSender sender, SearchResult result, int page) {
195194
}
196195
}
197196

198-
public SearchResult getResult(CommandSender sender) {
197+
public SearchResult<?> getResult(CommandSender sender) {
199198
return lastResultViewed.get(sender.getName());
200199
}
201200

202-
public SearchResult getResult(String type) {
201+
public SearchResult<?> getResult(String type) {
203202
return customResults.get(type);
204203
}
205204

206-
public SearchResult getResult(SearchType type) {
205+
public SearchResult<?> getResult(SearchType type) {
207206
return results.get(type);
208207
}
209208
}

src/main/java/de/themoep/entitydetection/commands/ListSubCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public ListSubCommand(EntityDetection plugin) {
3939

4040
@Override
4141
public boolean execute(CommandSender sender, String[] args) {
42-
SearchResult result = getPlugin().getResult(sender);
42+
SearchResult<?> result = getPlugin().getResult(sender);
4343
int page = 1;
4444
String lastName = sender.getName();
4545
if(args.length > 0) {

src/main/java/de/themoep/entitydetection/commands/SearchSubCommand.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import de.themoep.entitydetection.EntityDetection;
44
import de.themoep.entitydetection.searcher.EntitySearch;
55
import de.themoep.entitydetection.searcher.SearchType;
6+
import org.bukkit.Bukkit;
67
import org.bukkit.ChatColor;
78
import org.bukkit.Material;
89
import org.bukkit.command.CommandSender;
910
import org.bukkit.entity.EntityType;
11+
import org.bukkit.plugin.Plugin;
1012

1113
/**
1214
* Copyright 2016 Max Lee (https://github.com/Phoenix616/)
@@ -36,6 +38,17 @@ public boolean execute(CommandSender sender, String[] args) {
3638
EntitySearch search = new EntitySearch(getPlugin(), sender);
3739
if(args.length > 0) {
3840
for(String arg : args) {
41+
if ("--regions".equalsIgnoreCase(arg)) {
42+
Plugin plugin = Bukkit.getPluginManager().getPlugin("WorldGuard");
43+
if (plugin != null && plugin.isEnabled() && plugin.getDescription().getVersion().startsWith("7"))
44+
search.setWorldGuardRegion(true);
45+
else {
46+
sender.sendMessage(ChatColor.RED + "Unable to start WorldGuard search. WorldGuard not enabled or outdated!");
47+
return true;
48+
}
49+
if (args.length == 1) search.setType(SearchType.MONSTER);
50+
continue;
51+
}
3952
if (arg.endsWith("s")) {
4053
arg = arg.substring(0, arg.length() - 1);
4154
}

src/main/java/de/themoep/entitydetection/commands/TpSubCommand.java

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -46,61 +46,30 @@ public boolean execute(CommandSender sender, String[] args) {
4646
if(args.length == 0) {
4747
return false;
4848
}
49-
SearchResult lastResult = getPlugin().getResult(sender);
49+
return teleport((Player)sender, args[0], getPlugin().getResult(sender));
50+
}
51+
52+
private <T> boolean teleport(Player sender, String page, SearchResult<T> lastResult) {
5053
if(lastResult == null) {
5154
sender.sendMessage(ChatColor.RED + "You have to view a search result before teleporting to an entry! Use /detect search or /detect list [<type>]");
5255
return true;
5356
}
5457

5558
int i;
5659
try {
57-
i = Integer.parseInt(args[0]);
60+
i = Integer.parseInt(page);
5861
} catch(NumberFormatException e) {
59-
sender.sendMessage(ChatColor.YELLOW + args[0] + ChatColor.RED + " is not a proper number input!");
62+
sender.sendMessage(ChatColor.YELLOW + page + ChatColor.RED + " is not a proper number input!");
6063
return false;
6164
}
6265

6366
if(i == 0 || lastResult.getSortedEntries().size() < i) {
64-
sender.sendMessage(ChatColor.RED + "Result " + ChatColor.YELLOW + args[0] + ChatColor.RED + " is not in the list!");
67+
sender.sendMessage(ChatColor.RED + "Result " + ChatColor.YELLOW + page + ChatColor.RED + " is not in the list!");
6568
return true;
6669
}
6770

68-
SearchResultEntry entry = lastResult.getSortedEntries().get(i - 1);
69-
70-
try {
71-
Chunk chunk = entry.getChunk().toBukkit(getPlugin().getServer());
72-
73-
Location loc = null;
74-
75-
for(Entity e : chunk.getEntities()) {
76-
if(e.getType().toString().equals(entry.getEntryCount().get(0).getKey())) {
77-
loc = e.getLocation();
78-
break;
79-
}
80-
}
81-
82-
for (BlockState b : chunk.getTileEntities()) {
83-
if(b.getType().toString().equals(entry.getEntryCount().get(0).getKey())) {
84-
loc = b.getLocation().add(0, 1, 0);
85-
break;
86-
}
87-
}
88-
89-
if (loc == null) {
90-
loc = chunk.getWorld().getHighestBlockAt(chunk.getX() * 16 + 8, chunk.getZ() * 16 + 8).getLocation().add(0, 2, 0);
91-
}
92-
93-
((Player) sender).teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
94-
sender.sendMessage(
95-
ChatColor.GREEN + "Teleported to entry " + ChatColor.WHITE + i + ": " +
96-
ChatColor.YELLOW + entry.getChunk() + " " + ChatColor.RED + entry.getSize() + " " +
97-
ChatColor.GREEN + Utils.enumToHumanName(entry.getEntryCount().get(0).getKey()) + "[" +
98-
ChatColor.WHITE + entry.getEntryCount().get(0).getValue() + ChatColor.GREEN + "]"
99-
);
100-
101-
} catch(IllegalArgumentException e) {
102-
sender.sendMessage(ChatColor.RED + e.getMessage());
103-
}
71+
SearchResultEntry<T> entry = lastResult.getSortedEntries().get(i - 1);
72+
lastResult.teleport(sender, entry, i);
10473
return true;
10574
}
10675
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package de.themoep.entitydetection.searcher;
2+
3+
import de.themoep.entitydetection.ChunkLocation;
4+
import de.themoep.entitydetection.Utils;
5+
import org.bukkit.Bukkit;
6+
import org.bukkit.ChatColor;
7+
import org.bukkit.Chunk;
8+
import org.bukkit.Location;
9+
import org.bukkit.block.BlockState;
10+
import org.bukkit.entity.Entity;
11+
import org.bukkit.entity.Player;
12+
import org.bukkit.event.player.PlayerTeleportEvent;
13+
14+
public class ChunkSearchResult extends SearchResult<ChunkLocation> {
15+
public ChunkSearchResult(EntitySearch search) {
16+
super(search);
17+
}
18+
19+
@Override
20+
public void addEntity(Entity entity) {
21+
add(entity.getLocation(), entity.getType().toString());
22+
}
23+
24+
@Override
25+
public void addBlockState(BlockState blockState) {
26+
add(blockState.getLocation(), blockState.getType().toString());
27+
}
28+
29+
@Override
30+
public void add(Location location, String type) {
31+
ChunkLocation chunkLocation = new ChunkLocation(location);
32+
33+
if(!resultEntryMap.containsKey(chunkLocation)) {
34+
resultEntryMap.put(chunkLocation, new SearchResultEntry<>(chunkLocation));
35+
}
36+
resultEntryMap.get(chunkLocation).increment(type);
37+
}
38+
39+
@Override
40+
public void teleport(Player sender, SearchResultEntry<ChunkLocation> entry, int i) {
41+
try {
42+
Chunk chunk = entry.getLocation().toBukkit(Bukkit.getServer());
43+
44+
Location loc = null;
45+
46+
for(Entity e : chunk.getEntities()) {
47+
if(e.getType().toString().equals(entry.getEntryCount().get(0).getKey())) {
48+
loc = e.getLocation();
49+
break;
50+
}
51+
}
52+
53+
for (BlockState b : chunk.getTileEntities()) {
54+
if(b.getType().toString().equals(entry.getEntryCount().get(0).getKey())) {
55+
loc = b.getLocation().add(0, 1, 0);
56+
break;
57+
}
58+
}
59+
60+
if (loc == null) {
61+
loc = chunk.getWorld().getHighestBlockAt(chunk.getX() * 16 + 8, chunk.getZ() * 16 + 8).getLocation().add(0, 2, 0);
62+
}
63+
64+
sender.teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
65+
sender.sendMessage(
66+
ChatColor.GREEN + "Teleported to entry " + ChatColor.WHITE + i + ": " +
67+
ChatColor.YELLOW + entry.getLocation() + " " + ChatColor.RED + entry.getSize() + " " +
68+
ChatColor.GREEN + Utils.enumToHumanName(entry.getEntryCount().get(0).getKey()) + "[" +
69+
ChatColor.WHITE + entry.getEntryCount().get(0).getValue() + ChatColor.GREEN + "]"
70+
);
71+
72+
} catch(IllegalArgumentException e) {
73+
sender.sendMessage(ChatColor.RED + e.getMessage());
74+
}
75+
}
76+
}

src/main/java/de/themoep/entitydetection/searcher/EntitySearch.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public class EntitySearch extends BukkitRunnable {
4646
private List<Entity> entities = new ArrayList<Entity>();
4747
private List<BlockState> blockStates = new ArrayList<BlockState>();
4848

49+
private boolean isWorldGuardRegion = false;
50+
4951
public EntitySearch(EntityDetection plugin, CommandSender sender) {
5052
this.plugin = plugin;
5153
owner = sender;
@@ -100,6 +102,13 @@ public long getStartTime() {
100102
return startTime;
101103
}
102104

105+
public boolean isWorldGuardRegion() {
106+
return this.isWorldGuardRegion;
107+
}
108+
109+
public void setWorldGuardRegion(boolean value) {
110+
this.isWorldGuardRegion = value;
111+
}
103112
/**
104113
* Get the duration since this search started
105114
* @return The duration in seconds
@@ -138,7 +147,9 @@ public void stop(String name) {
138147

139148
public void run() {
140149
startTime = System.currentTimeMillis();
141-
SearchResult result = new SearchResult(this);
150+
SearchResult<?> result;
151+
if(isWorldGuardRegion) result = new WGSearchResult(this);
152+
else result = new ChunkSearchResult(this);
142153

143154
for(Entity e : entities) {
144155
if(!running) {
@@ -159,7 +170,7 @@ public void run() {
159170
}
160171

161172
result.sort();
162-
plugin.addResult(result);;
173+
plugin.addResult(result);
163174
plugin.send(owner, result);
164175
running = false;
165176
}

0 commit comments

Comments
 (0)