Skip to content

Commit 4a83247

Browse files
authored
Reconstructed Folia Support (#7)
1 parent 17d9258 commit 4a83247

10 files changed

Lines changed: 131 additions & 49 deletions

File tree

pom.xml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
<id>papermc-repo</id>
2727
<url>https://repo.papermc.io/repository/maven-public/</url>
2828
</repository>
29+
<repository>
30+
<id>tcoded-releases</id>
31+
<url>https://repo.tcoded.com/releases</url>
32+
</repository>
2933
<repository>
3034
<id>enginehub-repo</id>
3135
<url>https://maven.enginehub.org/repo/</url>
@@ -39,6 +43,12 @@
3943
<version>1.17.1-R0.1-SNAPSHOT</version>
4044
<scope>provided</scope>
4145
</dependency>
46+
<dependency>
47+
<groupId>com.tcoded</groupId>
48+
<artifactId>FoliaLib</artifactId>
49+
<version>0.5.1</version>
50+
<scope>compile</scope>
51+
</dependency>
4252
<dependency>
4353
<groupId>com.sk89q.worldguard</groupId>
4454
<artifactId>worldguard-bukkit</artifactId>
@@ -93,7 +103,7 @@
93103
</profiles>
94104

95105
<build>
96-
<finalName>${project.name}</finalName>
106+
<finalName>${project.name}-${project.version}</finalName>
97107
<resources>
98108
<resource>
99109
<filtering>true</filtering>
@@ -120,7 +130,7 @@
120130
<plugin>
121131
<groupId>org.apache.maven.plugins</groupId>
122132
<artifactId>maven-shade-plugin</artifactId>
123-
<version>3.6.0</version>
133+
<version>3.6.1</version>
124134
<executions>
125135
<execution>
126136
<phase>package</phase>
@@ -132,9 +142,14 @@
132142
<includes>
133143
<include>de.themoep:*</include>
134144
<include>de.themoep.utils:*</include>
145+
<include>com.tcoded:FoliaLib</include>
135146
</includes>
136147
</artifactSet>
137148
<relocations>
149+
<relocation>
150+
<pattern>com.tcoded.folialib</pattern>
151+
<shadedPattern>de.themoep.entitydetection.libs.folialib</shadedPattern>
152+
</relocation>
138153
<relocation>
139154
<pattern>de.themoep.utils.lang</pattern>
140155
<shadedPattern>de.themoep.entitydetection.libs.lang</shadedPattern>

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.themoep.entitydetection;
22

3+
import com.tcoded.folialib.FoliaLib;
4+
import com.tcoded.folialib.impl.PlatformScheduler;
35
import de.themoep.entitydetection.commands.ListSubCommand;
46
import de.themoep.entitydetection.commands.PluginCommandExecutor;
57
import de.themoep.entitydetection.commands.SearchSubCommand;
@@ -41,6 +43,8 @@
4143
* along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
4244
*/
4345
public class EntityDetection extends JavaPlugin {
46+
private PlatformScheduler scheduler;
47+
4448
private LanguageManager lang;
4549

4650
private EntitySearch currentSearch;
@@ -49,7 +53,13 @@ public class EntityDetection extends JavaPlugin {
4953
private Map<String, SearchResult<?>> customResults = new HashMap<>();
5054
private Map<String, SearchResult<?>> lastResultViewed = new HashMap<>();
5155

56+
public PlatformScheduler getScheduler() {
57+
return scheduler;
58+
}
59+
5260
public void onEnable() {
61+
FoliaLib foliaLib = new FoliaLib(this);
62+
scheduler = foliaLib.getScheduler();
5363
lang = new LanguageManager(this, System.getProperty("de.themoep.entitydetection.default-language", "en"));
5464
PluginCommandExecutor cmdEx = new PluginCommandExecutor(this);
5565
cmdEx.register(new SearchSubCommand(this));
@@ -71,7 +81,8 @@ public boolean startSearch(EntitySearch search) {
7181
return false;
7282
}
7383
currentSearch = search;
74-
return search.start() != null;
84+
search.start();
85+
return true;
7586
}
7687

7788
public boolean stopSearch(String stopper) {
@@ -104,7 +115,6 @@ public void send(CommandSender sender, SearchResult<?> result) {
104115
send(sender, result, 0);
105116
}
106117

107-
108118
public void send(CommandSender sender, SearchResult<?> result, int page) {
109119
lastResultViewed.put(sender.getName(), result);
110120

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static String capitalize(String string) {
2727
}
2828

2929
/**
30-
* Converts an uppercase enum to a human readable string
30+
* Converts an uppercase enum to a human-readable string
3131
* @param convert The Enum to convert
3232
* @return The converted name; capitalizes each word and replaces underscores with spaces
3333
*/
@@ -36,7 +36,7 @@ public static String enumToHumanName(Enum convert) {
3636
}
3737

3838
/**
39-
* Converts an uppercase enum to a human readable string
39+
* Converts an uppercase enum to a human-readable string
4040
* @param convert The name of the Enum to convert
4141
* @return The converted name; capitalizes each word and replaces underscores with spaces
4242
*/

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

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package de.themoep.entitydetection.searcher;
22

3+
import com.tcoded.folialib.impl.PlatformScheduler;
34
import de.themoep.entitydetection.ChunkLocation;
45
import de.themoep.entitydetection.Utils;
56
import org.bukkit.Bukkit;
67
import org.bukkit.ChatColor;
7-
import org.bukkit.Chunk;
88
import org.bukkit.Location;
9+
import org.bukkit.World;
910
import org.bukkit.block.BlockState;
1011
import org.bukkit.entity.Entity;
1112
import org.bukkit.entity.Player;
1213
import org.bukkit.event.player.PlayerTeleportEvent;
1314

1415
public class ChunkSearchResult extends SearchResult<ChunkLocation> {
16+
17+
private final PlatformScheduler scheduler;
18+
1519
public ChunkSearchResult(EntitySearch search) {
1620
super(search);
21+
this.scheduler = search.getScheduler();
1722
}
1823

1924
@Override
@@ -39,35 +44,49 @@ public void add(Location location, String type) {
3944
@Override
4045
public void teleport(Player sender, SearchResultEntry<ChunkLocation> entry, int i) {
4146
try {
42-
Chunk chunk = entry.getLocation().toBukkit(Bukkit.getServer());
47+
World targetWorld = Bukkit.getWorld(entry.getLocation().getWorld());
48+
if (targetWorld == null) {
49+
sender.sendMessage(ChatColor.RED + "World " + ChatColor.WHITE + entry.getLocation().getWorld() + ChatColor.RED + " is not loaded anymore.");
50+
return;
51+
}
4352

44-
Location loc = null;
53+
int cx = entry.getLocation().getX();
54+
int cz = entry.getLocation().getZ();
55+
int anchorX = (cx << 4) + 8;
56+
int anchorZ = (cz << 4) + 8;
57+
Location location = new Location(targetWorld, anchorX, 64, anchorZ);
4558

46-
for(Entity e : chunk.getEntities()) {
47-
if(e.getType().toString().equals(entry.getEntryCount().get(0).getKey())) {
48-
loc = e.getLocation();
49-
break;
59+
targetWorld.getChunkAtAsync(cx, cz, true, chunk -> scheduler.runAtLocation(location, task -> {
60+
Location loc = null;
61+
62+
for(Entity e : chunk.getEntities()) {
63+
if(e.getType().toString().equals(entry.getEntryCount().get(0).getKey())) {
64+
loc = e.getLocation();
65+
break;
66+
}
5067
}
51-
}
5268

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;
69+
for (BlockState b : chunk.getTileEntities()) {
70+
if(b.getType().toString().equals(entry.getEntryCount().get(0).getKey())) {
71+
loc = b.getLocation().add(0, 1, 0);
72+
break;
73+
}
5774
}
58-
}
5975

60-
if (loc == null) {
61-
loc = chunk.getWorld().getHighestBlockAt(chunk.getX() * 16 + 8, chunk.getZ() * 16 + 8).getLocation().add(0, 2, 0);
62-
}
76+
if (loc == null) {
77+
loc = chunk.getWorld().getHighestBlockAt(anchorX, anchorZ).getLocation().add(0, 2, 0);
78+
}
79+
80+
Location finalLoc = loc;
81+
scheduler.teleportAsync(sender, finalLoc, PlayerTeleportEvent.TeleportCause.PLUGIN);
6382

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-
);
83+
sender.sendMessage(
84+
ChatColor.GREEN + "Teleported to entry " + ChatColor.WHITE + i + ": " +
85+
ChatColor.YELLOW + entry.getLocation() + " " + ChatColor.RED + entry.getSize() + " " +
86+
ChatColor.GREEN + Utils.enumToHumanName(entry.getEntryCount().get(0).getKey()) + "[" +
87+
ChatColor.WHITE + entry.getEntryCount().get(0).getValue() + ChatColor.GREEN + "]"
88+
);
89+
}));
7190

7291
} catch(IllegalArgumentException e) {
7392
sender.sendMessage(ChatColor.RED + e.getMessage());

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

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.themoep.entitydetection.searcher;
22

3+
import com.tcoded.folialib.impl.PlatformScheduler;
4+
import com.tcoded.folialib.wrapper.task.WrappedTask;
35
import de.themoep.entitydetection.EntityDetection;
46
import net.md_5.bungee.api.ChatColor;
57
import org.bukkit.Chunk;
@@ -9,15 +11,14 @@
911
import org.bukkit.command.CommandSender;
1012
import org.bukkit.entity.Entity;
1113
import org.bukkit.entity.EntityType;
12-
import org.bukkit.scheduler.BukkitRunnable;
13-
import org.bukkit.scheduler.BukkitTask;
1414

1515
import java.util.ArrayList;
1616
import java.util.Arrays;
1717
import java.util.Collections;
1818
import java.util.HashSet;
1919
import java.util.List;
2020
import java.util.Set;
21+
import java.util.concurrent.atomic.AtomicInteger;
2122

2223
/**
2324
* Copyright 2016 Max Lee (https://github.com/Phoenix616/)
@@ -34,25 +35,32 @@
3435
* You should have received a copy of the Mozilla Public License v2.0
3536
* along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
3637
*/
37-
public class EntitySearch extends BukkitRunnable {
38+
public class EntitySearch implements Runnable {
3839
private final EntityDetection plugin;
40+
private final PlatformScheduler scheduler;
3941
private final CommandSender owner;
4042
private SearchType type = SearchType.CUSTOM;
41-
private Set<EntityType> searchedEntities = new HashSet<EntityType>();
42-
private Set<Class<?>> searchedBlockStates = new HashSet<Class<?>>();
43-
private Set<Material> searchedMaterial = new HashSet<Material>();
43+
private Set<EntityType> searchedEntities = new HashSet<>();
44+
private Set<Class<?>> searchedBlockStates = new HashSet<>();
45+
private Set<Material> searchedMaterial = new HashSet<>();
4446
private long startTime;
4547
private boolean running = true;
46-
private List<Entity> entities = new ArrayList<Entity>();
47-
private List<BlockState> blockStates = new ArrayList<BlockState>();
48+
private List<Entity> entities = new ArrayList<>();
49+
private List<BlockState> blockStates = new ArrayList<>();
4850

4951
private boolean isWorldGuardRegion = false;
52+
private final AtomicInteger pending = new AtomicInteger(0);
5053

5154
public EntitySearch(EntityDetection plugin, CommandSender sender) {
5255
this.plugin = plugin;
56+
this.scheduler = plugin.getScheduler();
5357
owner = sender;
5458
}
5559

60+
public PlatformScheduler getScheduler() {
61+
return scheduler;
62+
}
63+
5664
public SearchType getType() {
5765
return type;
5866
}
@@ -117,20 +125,44 @@ public long getDuration() {
117125
return (System.currentTimeMillis() - getStartTime()) / 1000;
118126
}
119127

120-
public BukkitTask start() {
128+
public WrappedTask start() {
129+
int scheduled = 0;
121130
if (searchedEntities.size() > 0) {
122131
for (World world : plugin.getServer().getWorlds()) {
123-
entities.addAll(world.getEntities());
132+
pending.incrementAndGet();
133+
scheduled++;
134+
scheduler.runAtLocation(world.getSpawnLocation(), task -> {
135+
try {
136+
entities.addAll(world.getEntities());
137+
} finally {
138+
if (pending.decrementAndGet() == 0) {
139+
scheduler.runLaterAsync(this, 1L);
140+
}
141+
}
142+
});
124143
}
125144
}
126145
if (searchedBlockStates.size() > 0 || searchedMaterial.size() > 0) {
127146
for (World world : plugin.getServer().getWorlds()) {
128147
for (Chunk chunk : world.getLoadedChunks()) {
129-
blockStates.addAll(Arrays.asList(chunk.getTileEntities()));
148+
pending.incrementAndGet();
149+
scheduled++;
150+
scheduler.runAtLocation(chunk.getBlock(0, 0, 0).getLocation(), task -> {
151+
try {
152+
blockStates.addAll(Arrays.asList(chunk.getTileEntities()));
153+
} finally {
154+
if (pending.decrementAndGet() == 0) {
155+
scheduler.runLaterAsync(this, 1L);
156+
}
157+
}
158+
});
130159
}
131160
}
132161
}
133-
return runTaskAsynchronously(plugin);
162+
if (scheduled == 0) {
163+
return scheduler.runLaterAsync(this, 1L);
164+
}
165+
return null;
134166
}
135167

136168
public boolean isRunning() {
@@ -139,7 +171,6 @@ public boolean isRunning() {
139171

140172
public void stop(String name) {
141173
running = false;
142-
cancel();
143174
if(!owner.getName().equals(name)) {
144175
owner.sendMessage(ChatColor.YELLOW + name + ChatColor.RED + " stopped your " + getType() + " search after " + getDuration() + "s!");
145176
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public abstract class SearchResult<T> {
4848

4949
public SearchResult(EntitySearch search) {
5050
type = search.getType();
51-
searched = new HashSet<String>();
51+
searched = new HashSet<>();
5252
for (EntityType e : search.getSearchedEntities()) {
5353
searched.add(e.toString());
5454
}
@@ -111,7 +111,7 @@ public void sort() {
111111
chunkEntry.sort();
112112
}
113113
resultEntryList = new ArrayList<>(resultEntryMap.values());
114-
Collections.sort(resultEntryList, Collections.reverseOrder());
114+
resultEntryList.sort(Collections.reverseOrder());
115115

116116
endTime = System.currentTimeMillis();
117117
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
*/
2525
public class SearchResultEntry<T> implements Comparable<SearchResultEntry<T>> {
2626
private T location;
27-
private Map<String, Integer> entryCount = new HashMap<String, Integer>();
28-
private List<Map.Entry<String, Integer>> finalList = new ArrayList<Map.Entry<String, Integer>>();
27+
private Map<String, Integer> entryCount = new HashMap<>();
28+
private List<Map.Entry<String, Integer>> finalList = new ArrayList<>();
2929
private int size = 0;
3030

3131
SearchResultEntry(T location) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public enum SearchType {
9595

9696
SearchType(String[] aliases, EntityType[] eTypes, Class[] classes) {
9797
this.aliases = aliases;
98-
Set<EntityType> typeSet = new HashSet<EntityType>();
98+
Set<EntityType> typeSet = new HashSet<>();
9999
Collections.addAll(typeSet, eTypes);
100100

101-
List<Class> classList = new LinkedList<Class>(Arrays.asList(classes));
101+
List<Class> classList = new LinkedList<>(Arrays.asList(classes));
102102

103103
if(classes.length > 0) {
104104
for(EntityType et : EntityType.values()) {

0 commit comments

Comments
 (0)