Skip to content

feat(folia): added folia support #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
<id>paper-mc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>enginehub-repo</id>
Expand All @@ -30,15 +30,15 @@

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21.4-R0.1-SNAPSHOT</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this break spigot 1.13.2 (and up) compatibility? If so then this might not be the best approach :/ I'd prefer to keep this available as a utility plugin for a wide range of people.

<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-bukkit</artifactId>
<version>7.0.2</version>
<version>7.0.13-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/de/themoep/entitydetection/EntityDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import de.themoep.entitydetection.searcher.SearchResult;
import de.themoep.entitydetection.searcher.SearchResultEntry;
import de.themoep.entitydetection.searcher.SearchType;
import de.themoep.entitydetection.util.folia.TaskWrapper;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
Expand Down Expand Up @@ -46,15 +47,24 @@
*/
public class EntityDetection extends JavaPlugin {

private static EntityDetection instance = null;

private EntitySearch currentSearch;
private TaskWrapper currentSearchTask;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda feel like storing the task as a field of the search instead of separate in the plugin could be cleaner. Having two fields representing the current running search feels weird (espcecially as one now needs to ensure they both stay in sync) and how that search is more a detail of the search itself and not the plugin.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the feedback to this quick fork, however, I'm sharing this code purely for your own plugin's supported platforms.

I need Folia support, because we will start using it on one of my client's server.
In this instance, it works on our end, which was my goal.

That does mean I haven't (and won't) tested it on older version, nor pre-folia builds etc.

Feel free to close the PR if you don't want to further add Folia support to this resource!

Cheers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I'm definitely open to adding support for it however this is a vital tool used by many so any addition needs to make sure that it keeps working for them. Publishing software for others to use unfortunately makes it hard to do radical changes like Folia support is when not implemented properly hence why tho current approach just wont work.


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;

public static EntityDetection getInstance() {
if (instance == null) throw new IllegalStateException("EntityDetection has not been initialized yet!");
return instance;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really a fan of introducing a singleton pattern where none existed before. (Especially as it's technically possible to create multiple instances of the same plugin so they aren't really singletons to begin with)

What benefit over simply using dependency injection does this offer?

}

public void onEnable() {
instance = this;
try {
Bukkit.class.getMethod("spigot");
} catch (NoSuchMethodException noSpigot) {
Expand All @@ -71,14 +81,19 @@ public boolean startSearch(EntitySearch search) {
if(currentSearch != null && currentSearch.isRunning()) {
return false;
}

currentSearch = search;
return search.start() != null;
currentSearchTask = search.start();

return currentSearchTask != null;
}

public boolean stopSearch(String stopper) {
if(currentSearch == null || !currentSearch.isRunning()) {
if(currentSearch == null || !currentSearch.isRunning() || currentSearchTask.isCancelled()) {
return false;
}

currentSearchTask.cancel();
currentSearch.stop(stopper);
clearCurrentSearch();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

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 de.themoep.entitydetection.util.folia.FoliaScheduler;
import org.bukkit.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use wildcard imports, they can lead to errors when a different class is used than expected.

import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
Expand All @@ -16,16 +14,6 @@ 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);
Expand All @@ -39,36 +27,48 @@ public void add(Location location, String type) {
@Override
public void teleport(Player sender, SearchResultEntry<ChunkLocation> entry, int i) {
try {
Chunk chunk = entry.getLocation().toBukkit(Bukkit.getServer());
final ChunkLocation location = entry.getLocation();
final World world = Bukkit.getWorld(location.getWorld());
if (world == null) return;

Location loc = null;
final Runnable runnable = () -> {
final Chunk chunk = world.getChunkAt(location.getX(), location.getZ());
Location loc = null;

for(Entity e : chunk.getEntities()) {
if(e.getType().toString().equals(entry.getEntryCount().get(0).getKey())) {
loc = e.getLocation();
break;
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;
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);
}
if (loc == null) {
loc = chunk.getWorld().getHighestBlockAt(chunk.getX() * 16 + 8, chunk.getZ() * 16 + 8).getLocation().add(0, 2, 0);
}

sender.teleportAsync(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
sender.sendMessage(
ChatColor.GREEN + "Teleported to entry " + ChatColor.WHITE + i + ": " +
ChatColor.YELLOW + location + " " + ChatColor.RED + entry.getSize() + " " +
ChatColor.GREEN + Utils.enumToHumanName(entry.getEntryCount().get(0).getKey()) + "[" +
ChatColor.WHITE + entry.getEntryCount().get(0).getValue() + ChatColor.GREEN + "]"
);
};

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 + "]"
);
if (FoliaScheduler.isFolia()) {
FoliaScheduler.getRegionScheduler().run(PLUGIN, world, location.getX(), location.getZ(),
$ -> runnable.run());
return;
}

runnable.run();
} catch(IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.themoep.entitydetection.searcher;

import de.themoep.entitydetection.EntityDetection;
import de.themoep.entitydetection.util.folia.FoliaScheduler;
import de.themoep.entitydetection.util.folia.TaskWrapper;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Chunk;
import org.bukkit.Material;
Expand All @@ -9,8 +11,6 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -34,7 +34,7 @@
* You should have received a copy of the Mozilla Public License v2.0
* along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
*/
public class EntitySearch extends BukkitRunnable {
public class EntitySearch implements Runnable {
private final EntityDetection plugin;
private final CommandSender owner;
private SearchType type = SearchType.CUSTOM;
Expand Down Expand Up @@ -117,7 +117,7 @@ public long getDuration() {
return (System.currentTimeMillis() - getStartTime()) / 1000;
}

public BukkitTask start() {
public TaskWrapper start() {
if (searchedEntities.size() > 0) {
for (World world : plugin.getServer().getWorlds()) {
entities.addAll(world.getEntities());
Expand All @@ -126,11 +126,18 @@ public BukkitTask start() {
if (searchedBlockStates.size() > 0 || searchedMaterial.size() > 0) {
for (World world : plugin.getServer().getWorlds()) {
for (Chunk chunk : world.getLoadedChunks()) {
if (FoliaScheduler.isFolia()) {
FoliaScheduler.getRegionScheduler().run(plugin, chunk.getWorld(), chunk.getX(), chunk.getZ(),
$ -> blockStates.addAll(Arrays.asList(chunk.getTileEntities())));
continue;
}

blockStates.addAll(Arrays.asList(chunk.getTileEntities()));
}
}
}
return runTaskAsynchronously(plugin);

return FoliaScheduler.getAsyncScheduler().runNow(plugin, $ -> this.run());
}

public boolean isRunning() {
Expand All @@ -139,7 +146,6 @@ public boolean isRunning() {

public void stop(String name) {
running = false;
cancel();
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
@@ -1,5 +1,7 @@
package de.themoep.entitydetection.searcher;

import de.themoep.entitydetection.EntityDetection;
import de.themoep.entitydetection.util.folia.FoliaScheduler;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
Expand All @@ -14,6 +16,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* Copyright 2016 Max Lee (https://github.com/Phoenix616/)
Expand All @@ -31,6 +34,8 @@
* along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
*/
public abstract class SearchResult<T> {
protected static final EntityDetection PLUGIN = EntityDetection.getInstance();

private SearchType type;
private Set<String> searched;
private long startTime;
Expand All @@ -39,7 +44,8 @@ public abstract class SearchResult<T> {
/**
* Working search map, use resultEntryList after sorting this result!
*/
protected Map<T, SearchResultEntry<T>> resultEntryMap = new HashMap<>();
protected Map<T, SearchResultEntry<T>> resultEntryMap = FoliaScheduler.isFolia() ?
new ConcurrentHashMap<>() : new HashMap<>();

/**
* Sorted, highest entity count per chunks first, only propagated after running .sort()
Expand All @@ -65,13 +71,17 @@ public SearchResult(EntitySearch search) {
* Add an entity to this result
* @param entity The entity to add
*/
public abstract void addEntity(Entity entity);
public void addEntity(Entity entity) {
add(entity.getLocation(), entity.getType().toString());
}

/**
* Add a BlockState to this result
* @param blockState The entity to add
*/
public abstract void addBlockState(BlockState blockState);
public void addBlockState(BlockState blockState) {
add(blockState.getLocation(), blockState.getType().toString());
}

public abstract void add(Location location, String type);

Expand Down
28 changes: 24 additions & 4 deletions src/main/java/de/themoep/entitydetection/searcher/SearchType.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Slime;
import org.bukkit.entity.WaterMob;
import org.bukkit.entity.boat.*;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -56,9 +57,28 @@ public enum SearchType {
),
MISC(
new EntityType[]{
EntityType.FIREWORK,
EntityType.ENDER_SIGNAL,
EntityType.BOAT
EntityType.FIREWORK_ROCKET,
EntityType.EYE_OF_ENDER,
EntityType.ACACIA_BOAT,
EntityType.ACACIA_CHEST_BOAT,
EntityType.BAMBOO_RAFT,
EntityType.BAMBOO_CHEST_RAFT,
EntityType.BIRCH_BOAT,
EntityType.BIRCH_CHEST_BOAT,
EntityType.CHERRY_BOAT,
EntityType.CHERRY_CHEST_BOAT,
EntityType.DARK_OAK_BOAT,
EntityType.DARK_OAK_CHEST_BOAT,
EntityType.JUNGLE_BOAT,
EntityType.JUNGLE_CHEST_BOAT,
EntityType.MANGROVE_BOAT,
EntityType.MANGROVE_CHEST_BOAT,
EntityType.OAK_BOAT,
EntityType.OAK_CHEST_BOAT,
EntityType.PALE_OAK_BOAT,
EntityType.PALE_OAK_CHEST_BOAT,
EntityType.SPRUCE_BOAT,
EntityType.SPRUCE_CHEST_BOAT
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break on older server versions. The plugin is at least at the current time meant to be compatible with basically all versions, at least with 1.13 and up (as stated in the plugin.yml) and I also don't think the changing of the search groups fits in this PR.

new Class[]{
Projectile.class,
Expand All @@ -70,7 +90,7 @@ public enum SearchType {
new EntityType[]{
EntityType.ARMOR_STAND,
EntityType.FALLING_BLOCK,
EntityType.ENDER_CRYSTAL
EntityType.END_CRYSTAL
},
new Class[]{Hanging.class}
),
Expand Down
Loading