Skip to content

Commit

Permalink
Merge branch 'flight'
Browse files Browse the repository at this point in the history
  • Loading branch information
Stonley890 committed Jan 25, 2025
2 parents 88205a2 + ce3bbff commit 8a1a735
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
Expand Down Expand Up @@ -426,22 +427,25 @@ private void checkConfig() throws InvalidConfigurationException {
}

private void registerListeners() {
getServer().getPluginManager().registerEvents(new ListenEntityDamage(), this);
getServer().getPluginManager().registerEvents(new ListenPlayerChat(), this);
getServer().getPluginManager().registerEvents(new ListenPlayerCmdPreprocess(), this);
getServer().getPluginManager().registerEvents(new ListenPlayerDeath(), this);
getServer().getPluginManager().registerEvents(new ListenPlayerJoin(), this);
getServer().getPluginManager().registerEvents(new ListenPlayerLogin(), this);
getServer().getPluginManager().registerEvents(new ListenPlayerQuit(), this);
getServer().getPluginManager().registerEvents(new ItemBanList(), this);
getServer().getPluginManager().registerEvents(new ListenPlayerGameModeChange(), this);
getServer().getPluginManager().registerEvents(new ListenServerPing(), this);
getServer().getPluginManager().registerEvents(new Sandbox(), this);
getServer().getPluginManager().registerEvents(new ListenTimeSkip(), this);
getServer().getPluginManager().registerEvents(new ListenSignChangeEvent(), this);
getServer().getPluginManager().registerEvents(new ListenPlayerToggleFlightEvent(), this);
getServer().getPluginManager().registerEvents(new ListenPlayerMoveEvent(), this);
getServer().getPluginManager().registerEvents(new ListenEntityToggleGlideEvent(), this);
PluginManager pluginManager = getServer().getPluginManager();
pluginManager.registerEvents(new ListenEntityDamage(), this);
pluginManager.registerEvents(new ListenPlayerChat(), this);
pluginManager.registerEvents(new ListenPlayerCmdPreprocess(), this);
pluginManager.registerEvents(new ListenPlayerDeath(), this);
pluginManager.registerEvents(new ListenPlayerJoin(), this);
pluginManager.registerEvents(new ListenPlayerLogin(), this);
pluginManager.registerEvents(new ListenPlayerQuit(), this);
pluginManager.registerEvents(new ItemBanList(), this);
pluginManager.registerEvents(new ListenPlayerGameModeChange(), this);
pluginManager.registerEvents(new ListenServerPing(), this);
pluginManager.registerEvents(new Sandbox(), this);
pluginManager.registerEvents(new ListenTimeSkip(), this);
pluginManager.registerEvents(new ListenSignChangeEvent(), this);
pluginManager.registerEvents(new ListenPlayerToggleFlightEvent(), this);
pluginManager.registerEvents(new ListenPlayerMoveEvent(), this);
pluginManager.registerEvents(new ListenEntityToggleGlideEvent(), this);
pluginManager.registerEvents(new ListenPlayerChangedWorld(), this);
pluginManager.registerEvents(new ListenPlayerRespawn(), this);
}

private void registerCommands(@NotNull List<DVCommand> commands) throws NullPointerException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class Flight {
public static double energyCapacity = Dreamvisitor.getPlugin().getConfig().getInt("flightEnergyCapacity");
public static double reactivationPoint = Dreamvisitor.getPlugin().getConfig().getInt("flightRegenerationPoint");;
public static double reactivationPoint = Dreamvisitor.getPlugin().getConfig().getInt("flightRegenerationPoint");
public static final Map<Player, Double> energy = new HashMap<>();
private static final Map<Player, Boolean> energyDepletion = new HashMap<>();
private static final Map<Player, Boolean> flightRestricted = new HashMap<>();
Expand All @@ -27,7 +27,7 @@ public static void init() {
for (Player player : Bukkit.getOnlinePlayers()) {

// If player does not have the dreamvisitor.fly permission, disable flight if not in creative
if ((!player.hasPermission("dreamvisitor.fly") || isFlightRestricted(player) && player.getGameMode() != GameMode.CREATIVE)) {
if ((!player.hasPermission("dreamvisitor.fly") || isFlightRestricted(player) && player.getGameMode() != GameMode.CREATIVE && player.getGameMode() != GameMode.SPECTATOR)) {
player.setAllowFlight(false);
}

Expand All @@ -40,7 +40,7 @@ public static void init() {

if (energy.get(player) < energyCapacity) {

if (!player.isFlying() || player.getGameMode() == GameMode.CREATIVE) {
if (!player.isFlying() || player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
// Regenerate energy if not flying or in creative mode
try {
energy.compute(player, (k, i) -> i + 1);
Expand Down Expand Up @@ -104,7 +104,7 @@ public static boolean isFlightRestricted(Player player) {

public static void setFlightRestricted(@NotNull Player player, boolean restricted) {
flightRestricted.put(player, restricted);
if (player.getGameMode() != GameMode.CREATIVE) {
if (player.getGameMode() != GameMode.CREATIVE && player.getGameMode() != GameMode.SPECTATOR) {
player.setAllowFlight(!restricted && !isPlayerDepleted(player));
if (restricted && player.isFlying()) {
player.setFlying(false);
Expand All @@ -113,4 +113,11 @@ public static void setFlightRestricted(@NotNull Player player, boolean restricte
}

}

public static void setupFlight(Player player) {
// Re-enable flight if it gets disabled by game mode change
if (!isFlightRestricted(player) && !isPlayerDepleted(player)) {
Bukkit.getScheduler().runTaskLater(Dreamvisitor.getPlugin(), () -> player.setAllowFlight(true), 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.stonley890.dreamvisitor.listeners;

import io.github.stonley890.dreamvisitor.functions.Flight;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.jetbrains.annotations.NotNull;

public class ListenPlayerChangedWorld implements Listener {

@EventHandler
public void onPlayerChangedWorld(@NotNull PlayerChangedWorldEvent event) {
Flight.setupFlight(event.getPlayer());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.stonley890.dreamvisitor.listeners;

import io.github.stonley890.dreamvisitor.Dreamvisitor;
import io.github.stonley890.dreamvisitor.data.PlayerMemory;
import io.github.stonley890.dreamvisitor.data.PlayerUtility;
import io.github.stonley890.dreamvisitor.functions.Flight;
Expand All @@ -20,10 +19,7 @@ public void onPlayerGameModeChangeEvent(@NotNull PlayerGameModeChangeEvent event
Player player = event.getPlayer();
PlayerMemory memory = PlayerUtility.getPlayerMemory(player.getUniqueId());

// Re-enable flight if it gets disabled by game mode change
if (!Flight.isFlightRestricted(player) && !Flight.isPlayerDepleted(player)) {
Bukkit.getScheduler().runTaskLater(Dreamvisitor.getPlugin(), () -> player.setAllowFlight(true), 1);
}
Flight.setupFlight(player);

if (memory.autoinvswap && ((player.getGameMode().equals(GameMode.SURVIVAL) && event.getNewGameMode().equals(GameMode.CREATIVE)) || (player.getGameMode().equals(GameMode.CREATIVE) && event.getNewGameMode().equals(GameMode.SURVIVAL)))) Bukkit.dispatchCommand(player, "invswap");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.stonley890.dreamvisitor.listeners;

import io.github.stonley890.dreamvisitor.functions.Flight;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.jetbrains.annotations.NotNull;

public class ListenPlayerRespawn implements Listener {

@EventHandler
public void onPlayerRespawn(@NotNull PlayerRespawnEvent event) {
Flight.setupFlight(event.getPlayer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,34 @@ public class ListenPlayerToggleFlightEvent implements Listener {
@EventHandler
public void onToggleFlight(@NotNull PlayerToggleFlightEvent event) {

if (event.getPlayer().getGameMode() != GameMode.CREATIVE) {
Player player = event.getPlayer();
if (player.getGameMode() != GameMode.CREATIVE && player.getGameMode() != GameMode.SPECTATOR) {
if (event.isFlying()) {
event.getPlayer().setGliding(false);
event.getPlayer().setFlySpeed(0.05f);
event.getPlayer().setFlying(true);
player.setGliding(false);
player.setFlySpeed(0.05f);
player.setFlying(true);

wingFlapSoundTask.put(event.getPlayer(), Bukkit.getScheduler().runTaskTimer(Dreamvisitor.getPlugin(), () -> {
if (!event.getPlayer().isOnline() || !event.getPlayer().isFlying()) {
wingFlapSoundTask.put(player, Bukkit.getScheduler().runTaskTimer(Dreamvisitor.getPlugin(), () -> {
if (!player.isOnline() || !player.isFlying()) {
// remove this task
cancelWingFlapSoundTask(event.getPlayer());
cancelWingFlapSoundTask(player);

} else {
event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.ENTITY_ENDER_DRAGON_FLAP, SoundCategory.PLAYERS, 0.5f, 1.2f);
player.playSound(player.getLocation(), Sound.ENTITY_ENDER_DRAGON_FLAP, SoundCategory.PLAYERS, 0.5f, 1.2f);
}
}, 1, 15));


} else {
event.getPlayer().setFlying(false);
event.getPlayer().setGliding(true);
player.setFlying(false);
player.setGliding(true);
}
} else {
if (event.isFlying()) {
event.getPlayer().setFlySpeed(0.1f);
event.getPlayer().setFlying(true);
player.setFlySpeed(0.1f);
player.setFlying(true);
} else {
event.getPlayer().setFlying(false);
player.setFlying(false);
}
}

Expand Down

0 comments on commit 8a1a735

Please sign in to comment.