From 1bfcd6a24898790a62f6615b5bbdcd7394134583 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Wed, 17 Jul 2024 19:41:16 -0500 Subject: [PATCH] Add one in the chamber module --- module/one-in-the-chamber/build.gradle.kts | 0 .../arena/module/oitc/OneInTheChamber.java | 71 +++++++++++++++++++ .../arena/event/ArenaEventManager.java | 22 +++--- settings.gradle.kts | 1 + 4 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 module/one-in-the-chamber/build.gradle.kts create mode 100644 module/one-in-the-chamber/src/main/java/org/battleplugins/arena/module/oitc/OneInTheChamber.java diff --git a/module/one-in-the-chamber/build.gradle.kts b/module/one-in-the-chamber/build.gradle.kts new file mode 100644 index 00000000..e69de29b diff --git a/module/one-in-the-chamber/src/main/java/org/battleplugins/arena/module/oitc/OneInTheChamber.java b/module/one-in-the-chamber/src/main/java/org/battleplugins/arena/module/oitc/OneInTheChamber.java new file mode 100644 index 00000000..4871d25e --- /dev/null +++ b/module/one-in-the-chamber/src/main/java/org/battleplugins/arena/module/oitc/OneInTheChamber.java @@ -0,0 +1,71 @@ +package org.battleplugins.arena.module.oitc; + +import org.battleplugins.arena.ArenaPlayer; +import org.battleplugins.arena.event.ArenaEventHandler; +import org.battleplugins.arena.event.ArenaListener; +import org.battleplugins.arena.event.arena.ArenaInitializeEvent; +import org.battleplugins.arena.event.player.ArenaKillEvent; +import org.battleplugins.arena.module.ArenaModule; +import org.battleplugins.arena.module.ArenaModuleInitializer; +import org.bukkit.Material; +import org.bukkit.entity.AbstractArrow; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.ProjectileHitEvent; +import org.bukkit.inventory.ItemStack; + +/** + * A module that adds one in the chamber to BattleArena. + */ +@ArenaModule(id = OneInTheChamber.ID, name = "One in the Chamber", description = "Adds One in the Chamber to BattleArena.", authors = "BattlePlugins") +public class OneInTheChamber implements ArenaModuleInitializer, ArenaListener { + public static final String ID = "one-in-the-chamber"; + + @EventHandler + public void onArenaInitialize(ArenaInitializeEvent event) { + if (!event.getArena().isModuleEnabled(ID)) { + return; + } + + event.getArena().getEventManager().registerArenaResolver(ProjectileHitEvent.class, hitEvent -> { + if (!(hitEvent.getEntity() instanceof AbstractArrow arrow && arrow.getShooter() instanceof Player player)) { + return null; + } + + return ArenaPlayer.arenaPlayer(player) + .map(ArenaPlayer::getCompetition) + .orElse(null); + }); + + event.getArena().getEventManager().registerEvents(this); + } + + @ArenaEventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { + if (!(event.getDamager() instanceof AbstractArrow arrow && arrow.getShooter() instanceof Player shooter)) { + return; + } + + if (!(event.getEntity() instanceof Player victim)) { + return; + } + + event.setCancelled(true); + + victim.damage(10000, shooter); + } + + @ArenaEventHandler + public void onKill(ArenaKillEvent event) { + event.getKiller().getPlayer().getInventory().addItem(new ItemStack(Material.ARROW)); + } + + @ArenaEventHandler + public void onProjectileHit(ProjectileHitEvent event) { + if (event.getEntity() instanceof AbstractArrow arrow) { + arrow.remove(); + } + } +} diff --git a/plugin/src/main/java/org/battleplugins/arena/event/ArenaEventManager.java b/plugin/src/main/java/org/battleplugins/arena/event/ArenaEventManager.java index fa709fd0..68079ad6 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/ArenaEventManager.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/ArenaEventManager.java @@ -64,7 +64,7 @@ public class ArenaEventManager { } }; - private final Map, Function>> arenaEventResolvers = new PolymorphicHashMap<>(); + private final Map, List>>> arenaEventResolvers = new PolymorphicHashMap<>(); private final Map, Function>> capturedPlayerResolvers = new HashMap<>() { { this.put(ArenaVictoryEvent.class, event -> ((ArenaVictoryEvent) event).getVictors()); @@ -90,7 +90,7 @@ public ArenaEventManager(Arena arena) { */ @SuppressWarnings("unchecked") public void registerArenaResolver(Class eventClass, Function> resolver) { - this.arenaEventResolvers.put(eventClass, (Function>) resolver); + this.arenaEventResolvers.computeIfAbsent(eventClass, key -> new ArrayList<>()).add((Function>) resolver); } /** @@ -340,14 +340,18 @@ private Pair> extractContext(Event event) { return Pair.of(arenaPlayer.getArena(), arenaPlayer.getCompetition()); } - Function> eventArenaFunction = this.arenaEventResolvers.get(event.getClass()); - if (eventArenaFunction != null) { - LiveCompetition competition = eventArenaFunction.apply(event); - if (competition == null) { - return null; - } + List>> resolvers = this.arenaEventResolvers.get(event.getClass()); + if (resolvers != null) { + for (Function> resolver : resolvers) { + if (resolver != null) { + LiveCompetition competition = resolver.apply(event); + if (competition == null) { + return null; + } - return Pair.of(competition.getArena(), competition); + return Pair.of(competition.getArena(), competition); + } + } } return null; diff --git a/settings.gradle.kts b/settings.gradle.kts index 22e7e940..d2eb1f64 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -21,6 +21,7 @@ include("plugin") include("module:arena-restoration") include("module:boundary-enforcer") include("module:classes") +include("module:one-in-the-chamber") include("module:scoreboards") include("module:team-colors") include("module:team-heads")