-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathChunkSearchResult.java
More file actions
91 lines (75 loc) · 3.46 KB
/
ChunkSearchResult.java
File metadata and controls
91 lines (75 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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.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;
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 {
World targetWorld = Bukkit.getWorld(entry.getLocation().getWorld());
if (targetWorld == null) {
sender.sendMessage(ChatColor.RED + "World " + ChatColor.WHITE + entry.getLocation().getWorld() + ChatColor.RED + " is not loaded anymore.");
return;
}
int cx = entry.getLocation().getX();
int cz = entry.getLocation().getZ();
int anchorX = (cx << 4) + 8;
int anchorZ = (cz << 4) + 8;
Location location = new Location(targetWorld, anchorX, 64, anchorZ);
scheduler.runAtLocation(location, task -> targetWorld.getChunkAtAsync(cx, cz, false, chunk -> {
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(false)) {
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(anchorX, anchorZ).getLocation().add(0, 2, 0);
}
Location finalLoc = loc;
scheduler.teleportAsync(sender, finalLoc, 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());
}
}
}