-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: master
Are you sure you want to change the base?
Changes from all commits
58f43ef
3f14071
b3f2903
8ee5bdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -46,15 +47,24 @@ | |
*/ | ||
public class EntityDetection extends JavaPlugin { | ||
|
||
private static EntityDetection instance = null; | ||
|
||
private EntitySearch currentSearch; | ||
private TaskWrapper currentSearchTask; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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); | ||
|
@@ -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()); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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} | ||
), | ||
|
There was a problem hiding this comment.
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.