commit 33ae417a7bdd4fc2e0447619a04a5f840862e79f
parent 1898299ce66fc08fe522e9ebb4171cde1d735ae6
Author: typable <contact@typable.dev>
Date: Sun, 8 Sep 2024 16:06:10 +0200
feat: Improved fast-travel implementation
Diffstat:
2 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/src/poppy/Main.java b/src/poppy/Main.java
@@ -96,6 +96,7 @@ public class Main extends JavaPlugin {
@Override
public void onDisable() {
chairModule.onDisable();
+ fastTravelModule.onDisable();
}
@Override
diff --git a/src/poppy/modules/FastTravelModule.java b/src/poppy/modules/FastTravelModule.java
@@ -1,5 +1,8 @@
package poppy.modules;
+import java.util.List;
+import java.util.ArrayList;
+
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
@@ -8,20 +11,30 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.EventHandler;
-import org.bukkit.event.player.PlayerInteractEvent;
+import org.bukkit.event.block.Action;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.meta.CompassMeta;
import org.bukkit.scheduler.BukkitRunnable;
+import org.bukkit.event.player.PlayerInteractEvent;
+import org.bukkit.event.player.PlayerMoveEvent;
+import org.bukkit.event.player.PlayerQuitEvent;
public class FastTravelModule implements Listener {
public static final int COUNTDOWN_TIME = 5;
+ public static final double MOVEMENT_THRESHOLD = 0.1D;
private Plugin plugin;
+ private List<Player> playersQueue;
public FastTravelModule(final Plugin plugin) {
this.plugin = plugin;
+ this.playersQueue = new ArrayList<>();
+ }
+
+ public void onDisable() {
+ this.playersQueue.clear();
}
@EventHandler
@@ -29,6 +42,9 @@ public class FastTravelModule implements Listener {
if (event.getHand() != EquipmentSlot.HAND) {
return;
}
+ if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) {
+ return;
+ }
final ItemStack item = event.getItem();
if (item == null || item.getType() != Material.COMPASS) {
return;
@@ -43,10 +59,18 @@ public class FastTravelModule implements Listener {
}
final Player player = event.getPlayer();
final Location location = meta.getLodestone();
+ if (playersQueue.contains(player)) {
+ return;
+ }
+ playersQueue.add(player);
new BukkitRunnable() {
int timer = COUNTDOWN_TIME;
@Override
public void run() {
+ if (!playersQueue.contains(player)) {
+ this.cancel();
+ return;
+ }
final Location playerLocation = player.getLocation();
if ( timer > 0) {
player.playSound(playerLocation, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 100.0F, 1.0F);
@@ -58,9 +82,32 @@ public class FastTravelModule implements Listener {
location.setPitch(playerLocation.getPitch());
player.teleport(location);
player.playSound(playerLocation, Sound.ENTITY_PLAYER_TELEPORT, 100.0F, 1.0F);
+ playersQueue.remove(player);
+ return;
}
timer--;
}
}.runTaskTimer(this.plugin, 0L, 20L);
}
+
+ @EventHandler
+ public void onPlayerMove(final PlayerMoveEvent event) {
+ final Player player = event.getPlayer();
+ if (playersQueue.contains(player)) {
+ final Location fromLocation = event.getFrom();
+ final Location toLocation = event.getTo();
+ if (fromLocation.distance(toLocation) > MOVEMENT_THRESHOLD) {
+ player.playSound(toLocation, Sound.BLOCK_GLASS_BREAK, 100.0F, 1.0F);
+ playersQueue.remove(player);
+ }
+ }
+ }
+
+ @EventHandler
+ public void onPlayerQuit(final PlayerQuitEvent event) {
+ final Player player = event.getPlayer();
+ if (playersQueue.contains(player)) {
+ playersQueue.remove(player);
+ }
+ }
}