NEWS: Welcome to my new homepage! <3

Merge pull request 'feat/fast-travel' (#9) from feat/fast-travel into main - poppy - A feature-rich Minecraft plugin which enhances gaming experience

poppy

A feature-rich Minecraft plugin which enhances gaming experience
git clone git://192.168.2.2/poppy
Log | Files | Refs | README

commit 2d88ce8ede52f742863ec32b6db91f7a64b878cf
parent 25c63b760ee268fab2d91f76c7e77d343b202cee
Author: typable <contact@typable.dev>
Date:   Sun,  8 Sep 2024 16:09:45 +0200

Merge pull request 'feat/fast-travel' (#9) from feat/fast-travel into main

Reviewed-on: https://git.typable.dev/typable/poppy/pulls/9

Diffstat:
M.gitignore | 5+++--
Mbuild.sh | 20+++++++++++++++++---
Msrc/poppy/Main.java | 6++++++
Asrc/poppy/modules/FastTravelModule.java | 113+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 139 insertions(+), 5 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,5 @@ poppy.jar +.project .gradle/ -build/ -\ No newline at end of file +build/ +.settings/ diff --git a/build.sh b/build.sh @@ -6,7 +6,21 @@ else cpdl=";" fi +if [[ "$1" == "--debug" ]]; then + javac -cp "./libs/bungeecord-chat-1.20-R0.2.jar${cpdl}./libs/spigot-api-1.21.1-R0.1-20240831.215322-16.jar" src/poppy/*.java src/poppy/modules/*.java -d build 2>&1 + if [ $? -ne 0 ]; then + rm -rf ./build + exit 0 + fi + jar cvfm poppy.jar res/MANIFEST.MF -C res plugin.yml -C build poppy + rm -rf ./build +else + javac -cp "./libs/bungeecord-chat-1.20-R0.2.jar${cpdl}./libs/spigot-api-1.21.1-R0.1-20240831.215322-16.jar" src/poppy/*.java src/poppy/modules/*.java -d build + if [ $? -ne 0 ]; then + rm -rf ./build + exit 1 + fi + jar cvfm poppy.jar res/MANIFEST.MF -C res plugin.yml -C build poppy + rm -rf ./build +fi -javac -cp "./libs/bungeecord-chat-1.20-R0.2.jar${cpdl}./libs/spigot-api-1.21.1-R0.1-20240831.215322-16.jar" src/poppy/*.java src/poppy/modules/*.java -d build -jar cvfm poppy.jar res/MANIFEST.MF -C res plugin.yml -C build poppy -rm -rf ./build diff --git a/src/poppy/Main.java b/src/poppy/Main.java @@ -25,6 +25,7 @@ import poppy.modules.CommonModule; import poppy.modules.LeavesDecayModule; import poppy.modules.SpawnerModule; import poppy.modules.PlayerMountModule; +import poppy.modules.FastTravelModule; public class Main extends JavaPlugin { @@ -44,6 +45,7 @@ public class Main extends JavaPlugin { private AutoBreakerModule autoBreakerModule; private PlayerMountModule playerMountModule; private BlockDetectorModule blockDetectorModule; + private FastTravelModule fastTravelModule; @Override public void onEnable() { @@ -86,11 +88,15 @@ public class Main extends JavaPlugin { blockDetectorModule = new BlockDetectorModule(); pluginManager.registerEvents(blockDetectorModule, this); + + fastTravelModule = new FastTravelModule(this); + pluginManager.registerEvents(fastTravelModule, this); } @Override public void onDisable() { chairModule.onDisable(); + fastTravelModule.onDisable(); } @Override diff --git a/src/poppy/modules/FastTravelModule.java b/src/poppy/modules/FastTravelModule.java @@ -0,0 +1,113 @@ +package poppy.modules; + +import java.util.List; +import java.util.ArrayList; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.plugin.Plugin; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.bukkit.event.EventHandler; +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 + public void onPlayerInteract(final PlayerInteractEvent event) { + 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; + } + final Block block = event.getClickedBlock(); + if (block != null && block.getType() == Material.LODESTONE) { + return; + } + final CompassMeta meta = (CompassMeta) item.getItemMeta(); + if (!meta.hasLodestone()) { + return; + } + 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); + } + if (timer == 0) { + this.cancel(); + location.add(0.5, 1.0, 0.5); + location.setYaw(playerLocation.getYaw()); + 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); + } + } +}