-
-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Add filter registration option for random teleport locations #1292
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
Changes from 1 commit
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.eternalcode.core.feature.randomteleport; | ||
|
|
||
| import org.bukkit.Location; | ||
|
|
||
| /** | ||
| * Filter for validating random teleport locations. | ||
| * <p> | ||
| * Allows external plugins to add custom validation logic for random teleport locations. | ||
| * For example, a plot plugin could implement this to prevent teleporting into claimed plots. | ||
| * <p> | ||
| * Example usage: | ||
| * <pre>{@code | ||
| * public class PlotFilter implements RandomTeleportLocationFilter { | ||
| * @Override | ||
| * public boolean isValid(Location location) { | ||
| * // Check if location is inside a claimed plot | ||
| * return !plotAPI.isPlotClaimed(location); | ||
| * } | ||
| * | ||
| * @Override | ||
| * public String getFilterName() { | ||
| * return "PlotFilter"; | ||
| * } | ||
| * } | ||
| * | ||
| * // Register the filter | ||
| * EternalCoreApi api = EternalCoreApiProvider.provide(); | ||
| * api.getRandomTeleportService().registerFilter(new PlotFilter()); | ||
| * }</pre> | ||
| */ | ||
| public interface RandomTeleportLocationFilter { | ||
|
|
||
| /** | ||
| * Checks if the given location is valid for random teleportation. | ||
| * | ||
| * @param location The location to validate | ||
| * @return true if the location is valid, false otherwise | ||
| */ | ||
| boolean isValid(Location location); | ||
|
|
||
| /** | ||
| * Gets the name of this filter for debugging and logging purposes. | ||
| * | ||
| * @return The filter name | ||
| */ | ||
| String getFilterName(); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,10 +9,12 @@ | |||||||||||||||||
| import com.eternalcode.core.injector.annotations.component.Service; | ||||||||||||||||||
| import io.papermc.lib.PaperLib; | ||||||||||||||||||
| import java.util.Collection; | ||||||||||||||||||
| import java.util.Collections; | ||||||||||||||||||
| import java.util.List; | ||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||
| import java.util.Map.Entry; | ||||||||||||||||||
| import java.util.concurrent.CompletableFuture; | ||||||||||||||||||
| import java.util.concurrent.ConcurrentHashMap; | ||||||||||||||||||
| import java.util.stream.Collectors; | ||||||||||||||||||
| import org.bukkit.Location; | ||||||||||||||||||
| import org.bukkit.World; | ||||||||||||||||||
|
|
@@ -25,6 +27,7 @@ class RandomTeleportServiceImpl implements RandomTeleportService { | |||||||||||||||||
| private final RandomTeleportSettings randomTeleportSettings; | ||||||||||||||||||
| private final RandomTeleportSafeLocationService safeLocationService; | ||||||||||||||||||
| private final EventCaller eventCaller; | ||||||||||||||||||
| private final Map<String, RandomTeleportLocationFilter> registeredFilters; | ||||||||||||||||||
|
|
||||||||||||||||||
| @Inject | ||||||||||||||||||
| RandomTeleportServiceImpl( | ||||||||||||||||||
|
|
@@ -34,6 +37,7 @@ class RandomTeleportServiceImpl implements RandomTeleportService { | |||||||||||||||||
| this.randomTeleportSettings = randomTeleportSettings; | ||||||||||||||||||
| this.safeLocationService = safeLocationService; | ||||||||||||||||||
| this.eventCaller = eventCaller; | ||||||||||||||||||
| this.registeredFilters = new ConcurrentHashMap<>(); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
|
|
@@ -150,4 +154,40 @@ private RandomTeleportRadius getWorldBorderRadius(World world) { | |||||||||||||||||
| int borderRadius = (int) (worldBorder.getSize() / 2); | ||||||||||||||||||
| return RandomTeleportRadius.of(-borderRadius, borderRadius, -borderRadius, borderRadius); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
| public void registerFilter(RandomTeleportLocationFilter filter) { | ||||||||||||||||||
| if (filter == null) { | ||||||||||||||||||
| throw new IllegalArgumentException("Filter cannot be null"); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| String filterName = filter.getFilterName(); | ||||||||||||||||||
| if (filterName == null || filterName.isEmpty()) { | ||||||||||||||||||
| throw new IllegalArgumentException("Filter name cannot be null or empty"); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if (this.registeredFilters.containsKey(filterName)) { | ||||||||||||||||||
| throw new IllegalArgumentException("Filter with name '" + filterName + "' is already registered"); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| this.registeredFilters.put(filterName, filter); | ||||||||||||||||||
|
||||||||||||||||||
| if (this.registeredFilters.containsKey(filterName)) { | |
| throw new IllegalArgumentException("Filter with name '" + filterName + "' is already registered"); | |
| } | |
| this.registeredFilters.put(filterName, filter); | |
| if (this.registeredFilters.putIfAbsent(filterName, filter) != null) { | |
| throw new IllegalArgumentException("Filter with name '" + filterName + "' is already registered"); | |
| } |
Outdated
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.
The unregisterFilter method allows any caller with access to the RandomTeleportService to remove any registered filter by its name. Since filter names are discoverable via getRegisteredFilters(), a malicious or poorly-behaved plugin can unregister filters registered by other plugins. This can lead to a bypass of critical security checks, such as those preventing teleportation into protected regions or claimed plots. It is recommended to modify the API to require the RandomTeleportLocationFilter instance itself for unregistration, ensuring that only a caller with a reference to the filter object can remove it.
Uh oh!
There was an error while loading. Please reload this page.