Skip to content

Commit

Permalink
More bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Jul 3, 2024
1 parent 0a8dc5e commit 0a983b8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
1 change: 0 additions & 1 deletion plugin/src/main/java/org/battleplugins/arena/Arena.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public class Arena implements ArenaLike, ArenaListener {

public Arena() {
this.eventManager = new ArenaEventManager(this);
this.eventManager.registerEvents(this);
}

// API methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ public void load() {
try {
ArenaType arenaType = this.battleArena.arenaTypes.get(this.mode);
if (arenaType == null) {
this.battleArena.info("Arena {} does not have a valid mode! Recognized modes: {}", this.arenaPath.getFileName(), this.battleArena.arenaTypes.keySet());
this.battleArena.info("Arena {} specified the mode as {}, which is not valid! Recognized modes: {}", this.arenaPath.getFileName(), this.mode, this.battleArena.arenaTypes.keySet());
return;
}

Arena arena = ArenaConfigParser.newInstance(this.arenaPath, arenaType.arenaClass(), this.configuration, this.battleArena);

// Need to call this here rather than in the Arena constructor
// so third party arenas can register their own event resolvers
arena.getEventManager().registerEvents(arena);

Bukkit.getPluginManager().registerEvents(arena, this.battleArena);

this.battleArena.arenas.put(arena.getName(), arena);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public ArenaEventManager(Arena arena) {
* @param eventClass the event class
* @param resolver the resolver for the event class
*/
public void registerArenaResolver(Class<? extends Event> eventClass, Function<Event, LiveCompetition<?>> resolver) {
this.arenaEventResolvers.put(eventClass, resolver);
@SuppressWarnings("unchecked")
public <E extends Event> void registerArenaResolver(Class<? extends E> eventClass, Function<E, LiveCompetition<?>> resolver) {
this.arenaEventResolvers.put(eventClass, (Function<Event, LiveCompetition<?>>) resolver);
}

/**
Expand Down Expand Up @@ -203,12 +204,14 @@ public void registerEvents(ArenaListener listener) {

ArenaEventHandler eventHandler = method.getAnnotation(ArenaEventHandler.class);
if (method.getParameterCount() == 0) {
this.arena.getPlugin().warn("Event method {} in {} has no parameters. Not registering.", method.getName(), listener.getClass());
continue;
}

// The first argument should be an ArenaEvent or a Player event
Class<?> eventClass = method.getParameterTypes()[0];
if (!ArenaEvent.class.isAssignableFrom(eventClass) && PLAYER_EVENT_RESOLVERS.get(eventClass) == null && this.arenaEventResolvers.get(eventClass) == null) {
this.arena.getPlugin().warn("Event method {} ({}) in {} was not an ArenaEvent or a Player event. Custom resolvers can be added using the ArenaEventManager#registerArenaResolver.", method.getName(), eventClass.getSimpleName(), listener.getClass());
continue;
}

Expand Down Expand Up @@ -321,15 +324,17 @@ private Pair<Arena, Competition<?>> extractContext(Event event) {
}

Function<Event, Player> eventPlayerFunction = PLAYER_EVENT_RESOLVERS.get(event.getClass());

playerEventResolver:
if (eventPlayerFunction != null) {
Player player = eventPlayerFunction.apply(event);
if (player == null) {
return null;
break playerEventResolver;
}

ArenaPlayer arenaPlayer = ArenaPlayer.getArenaPlayer(player);
if (arenaPlayer == null) {
return null;
break playerEventResolver;
}

return Pair.of(arenaPlayer.getArena(), arenaPlayer.getCompetition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void onInteract(PlayerInteractEvent event) {
return;
}

if (event.getInteractionPoint() == null) {
if (event.getClickedBlock() == null) {
return;
}

Expand All @@ -163,7 +163,7 @@ public void onInteract(PlayerInteractEvent event) {

// Need to run a tick later so Bukkit can handle the event cancellation
Bukkit.getScheduler().runTaskLater(BattleArena.getInstance(), () -> {
onPositionInteract(event.getInteractionPoint());
onPositionInteract(event.getClickedBlock().getLocation());
}, 1);

event.setCancelled(true);
Expand Down

0 comments on commit 0a983b8

Please sign in to comment.