Skip to content

Commit

Permalink
Add one in the chamber module
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Jul 18, 2024
1 parent cf62d75 commit 1bfcd6a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class ArenaEventManager {
}
};

private final Map<Class<? extends Event>, Function<Event, LiveCompetition<?>>> arenaEventResolvers = new PolymorphicHashMap<>();
private final Map<Class<? extends Event>, List<Function<Event, LiveCompetition<?>>>> arenaEventResolvers = new PolymorphicHashMap<>();
private final Map<Class<? extends ArenaEvent>, Function<ArenaEvent, Set<ArenaPlayer>>> capturedPlayerResolvers = new HashMap<>() {
{
this.put(ArenaVictoryEvent.class, event -> ((ArenaVictoryEvent) event).getVictors());
Expand All @@ -90,7 +90,7 @@ public ArenaEventManager(Arena arena) {
*/
@SuppressWarnings("unchecked")
public <E extends Event> void registerArenaResolver(Class<? extends E> eventClass, Function<E, LiveCompetition<?>> resolver) {
this.arenaEventResolvers.put(eventClass, (Function<Event, LiveCompetition<?>>) resolver);
this.arenaEventResolvers.computeIfAbsent(eventClass, key -> new ArrayList<>()).add((Function<Event, LiveCompetition<?>>) resolver);
}

/**
Expand Down Expand Up @@ -340,14 +340,18 @@ private Pair<Arena, Competition<?>> extractContext(Event event) {
return Pair.of(arenaPlayer.getArena(), arenaPlayer.getCompetition());
}

Function<Event, LiveCompetition<?>> eventArenaFunction = this.arenaEventResolvers.get(event.getClass());
if (eventArenaFunction != null) {
LiveCompetition<?> competition = eventArenaFunction.apply(event);
if (competition == null) {
return null;
}
List<Function<Event, LiveCompetition<?>>> resolvers = this.arenaEventResolvers.get(event.getClass());
if (resolvers != null) {
for (Function<Event, LiveCompetition<?>> 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;
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 1bfcd6a

Please sign in to comment.