From caf338c6331aab2fa6f68dfa8ad351fdf003c2c5 Mon Sep 17 00:00:00 2001 From: JustDoom <61824552+JustDoom@users.noreply.github.com> Date: Sat, 1 Jun 2024 20:42:25 +1000 Subject: [PATCH] Add feature based off Stop Drop N Roll mod (#72) * Add Stop Drop N Roll * Nearly forgot the chance * Fix * twerk-to-reduce-burn-time --- .../modules/StopDropNRollModule.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/main/java/org/purpurmc/purpurextras/modules/StopDropNRollModule.java diff --git a/src/main/java/org/purpurmc/purpurextras/modules/StopDropNRollModule.java b/src/main/java/org/purpurmc/purpurextras/modules/StopDropNRollModule.java new file mode 100644 index 0000000..bfac94f --- /dev/null +++ b/src/main/java/org/purpurmc/purpurextras/modules/StopDropNRollModule.java @@ -0,0 +1,60 @@ +package org.purpurmc.purpurextras.modules; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerToggleSneakEvent; +import org.purpurmc.purpurextras.PurpurExtras; + +import java.util.HashMap; +import java.util.Map; + +/** + * Allows the player to Stop Drop N Roll to get extinguished from any flames. + */ +public class StopDropNRollModule implements PurpurExtrasModule, Listener { + + private final Map playerLastSneakMap = new HashMap<>(); + private double chance; + private double amount; + + protected StopDropNRollModule() {} + + @Override + public void enable() { + PurpurExtras plugin = PurpurExtras.getInstance(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + chance = PurpurExtras.getPurpurConfig().getDouble("settings.twerk-to-reduce-burn-time.chance", 0); + amount = PurpurExtras.getPurpurConfig().getDouble("settings.twerk-to-reduce-burn-time.amount", 0.5); + } + + @Override + public boolean shouldEnable() { + return PurpurExtras.getPurpurConfig().getDouble("settings.twerk-to-reduce-burn-time.chance", 0) != 0; + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onPlayerToggleSneak(PlayerToggleSneakEvent event){ + Player player = event.getPlayer(); + boolean isSneaking = event.isSneaking(); + + if (player.getFireTicks() > 0 && isSneaking && !playerLastSneakMap.get(player) && Math.random() < chance) { + player.setFireTicks((int) (player.getFireTicks() * (1f - amount))); + } + + playerLastSneakMap.put(player, isSneaking); + } + + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerJoin(PlayerJoinEvent event) { + playerLastSneakMap.put(event.getPlayer(), false); + } + + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerQuit(PlayerQuitEvent event) { + playerLastSneakMap.remove(event.getPlayer()); + } +}