-
-
Notifications
You must be signed in to change notification settings - Fork 21
GH-1297 Add fly mode in vanish. #1297
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
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,70 @@ | ||||||||||||||||||
| package com.eternalcode.core.feature.vanish.controller; | ||||||||||||||||||
|
|
||||||||||||||||||
| import com.eternalcode.core.feature.vanish.VanishSettings; | ||||||||||||||||||
| import com.eternalcode.core.feature.vanish.event.DisableVanishEvent; | ||||||||||||||||||
| import com.eternalcode.core.feature.vanish.event.EnableVanishEvent; | ||||||||||||||||||
| import com.eternalcode.core.injector.annotations.Inject; | ||||||||||||||||||
| import com.eternalcode.core.injector.annotations.component.Controller; | ||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||
| import java.util.UUID; | ||||||||||||||||||
| import java.util.concurrent.ConcurrentHashMap; | ||||||||||||||||||
| import org.bukkit.GameMode; | ||||||||||||||||||
| import org.bukkit.entity.Player; | ||||||||||||||||||
| import org.bukkit.event.EventHandler; | ||||||||||||||||||
| import org.bukkit.event.Listener; | ||||||||||||||||||
| import org.bukkit.event.player.PlayerQuitEvent; | ||||||||||||||||||
|
|
||||||||||||||||||
| @Controller | ||||||||||||||||||
| class FlyController implements Listener { | ||||||||||||||||||
|
|
||||||||||||||||||
| private final VanishSettings settings; | ||||||||||||||||||
| private final Map<UUID, FlightState> previousFlightStates = new ConcurrentHashMap<>(); | ||||||||||||||||||
|
|
||||||||||||||||||
| @Inject | ||||||||||||||||||
| FlyController(VanishSettings settings) { | ||||||||||||||||||
| this.settings = settings; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @EventHandler(ignoreCancelled = true) | ||||||||||||||||||
| void onEnable(EnableVanishEvent event) { | ||||||||||||||||||
| if (!this.settings.flyMode()) { | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| Player player = event.getPlayer(); | ||||||||||||||||||
|
|
||||||||||||||||||
| this.previousFlightStates.computeIfAbsent( | ||||||||||||||||||
| player.getUniqueId(), | ||||||||||||||||||
| id -> new FlightState(player.getAllowFlight(), player.isFlying()) | ||||||||||||||||||
| ); | ||||||||||||||||||
|
Comment on lines
+36
to
+39
Contributor
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. To fix a potential flight exploit, it's necessary to also store the player's You will also need to update the private record FlightState(boolean hadAllowFlight, boolean wasFlying, GameMode gameMode) {}
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| player.setAllowFlight(true); | ||||||||||||||||||
| player.setFlying(true); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @EventHandler(ignoreCancelled = true) | ||||||||||||||||||
| void onDisable(DisableVanishEvent event) { | ||||||||||||||||||
| Player player = event.getPlayer(); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) { | ||||||||||||||||||
| this.previousFlightStates.remove(player.getUniqueId()); | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| FlightState state = this.previousFlightStates.remove(player.getUniqueId()); | ||||||||||||||||||
| if (state == null) { | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| player.setAllowFlight(state.hadAllowFlight()); | ||||||||||||||||||
| player.setFlying(state.wasFlying()); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+45
to
+61
Contributor
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. The current logic allows for a flight exploit where a player can enable vanish in creative mode, switch to survival, and retain flight after disabling vanish. By using the @EventHandler(ignoreCancelled = true)
void onDisable(DisableVanishEvent event) {
Player player = event.getPlayer();
FlightState state = this.previousFlightStates.remove(player.getUniqueId());
if (state == null) {
return;
}
GameMode currentMode = player.getGameMode();
if (currentMode == GameMode.CREATIVE || currentMode == GameMode.SPECTATOR) {
return;
}
GameMode originalMode = state.gameMode();
if (originalMode == GameMode.CREATIVE || originalMode == GameMode.SPECTATOR) {
player.setAllowFlight(false);
player.setFlying(false);
return;
}
player.setAllowFlight(state.hadAllowFlight());
player.setFlying(state.wasFlying());
} |
||||||||||||||||||
|
|
||||||||||||||||||
| @EventHandler | ||||||||||||||||||
| void onQuit(PlayerQuitEvent event) { | ||||||||||||||||||
| this.previousFlightStates.remove(event.getPlayer().getUniqueId()); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| private record FlightState(boolean hadAllowFlight, boolean wasFlying) { | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
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.
move to separate class?