Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BattleSession events. #385

Merged
merged 3 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.gmail.goosius.siegewar.events;

import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

public class BattleSessionEndedEvent extends Event {

private static final HandlerList handlers = new HandlerList();

public BattleSessionEndedEvent() {
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.gmail.goosius.siegewar.events;

import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

public class BattleSessionPreStartEvent extends Event implements Cancellable {

private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private String cancellationMsg = "Battle Session prevented by another plugin.";

public BattleSessionPreStartEvent() {
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancel) {
cancelled = cancel;
}

public String getCancellationMsg() {
return cancellationMsg;
}

public void setCancellationMsg(String cancellationMsg) {
this.cancellationMsg = cancellationMsg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.gmail.goosius.siegewar.events;

import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

public class BattleSessionStartedEvent extends Event {

private static final HandlerList handlers = new HandlerList();

public BattleSessionStartedEvent() {
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.gmail.goosius.siegewar.SiegeWar;
import com.gmail.goosius.siegewar.enums.SiegeSide;
import com.gmail.goosius.siegewar.enums.SiegeStatus;
import com.gmail.goosius.siegewar.events.BattleSessionEndedEvent;
import com.gmail.goosius.siegewar.events.BattleSessionPreStartEvent;
import com.gmail.goosius.siegewar.events.BattleSessionStartedEvent;
import com.gmail.goosius.siegewar.objects.BattleSession;
import com.gmail.goosius.siegewar.objects.Siege;
import com.gmail.goosius.siegewar.settings.SiegeWarSettings;
Expand Down Expand Up @@ -41,6 +44,8 @@ public static void startBattleSession() {
battleSession.setScheduledEndTime(System.currentTimeMillis() + (SiegeWarSettings.getWarSiegeBattleSessionDurationMinutes() * 60000));
//Clear the scheduled start time
battleSession.setScheduledStartTime(null);
//Send up the Bukkit event for other plugins to listen for.
Bukkit.getPluginManager().callEvent(new BattleSessionStartedEvent());
//Send global message to let the server know that the battle session started
Messaging.sendGlobalMessage(Translation.of("msg_war_siege_battle_session_started"));
}
Expand Down Expand Up @@ -108,7 +113,9 @@ public void run() {
t.printStackTrace();
}
}


Bukkit.getPluginManager().callEvent(new BattleSessionEndedEvent());

//Send message
sendBattleSessionEndedMessage(battleResults);
}
Expand All @@ -125,7 +132,7 @@ public static void evaluateBattleSessions() {
}

} else {
//Battle session is inactive.
//Battle session is inactive.

//If there is no battle session scheduled, attempt to schedule session now.
if(battleSession.getScheduledStartTime() == null) {
Expand All @@ -135,6 +142,18 @@ public static void evaluateBattleSessions() {
//If a battle session is scheduled, start it if we hit the scheduled time
if(battleSession.getScheduledStartTime() != null) {
if (System.currentTimeMillis() > battleSession.getScheduledStartTime()) {

//Send up the Bukkit event for other plugins to listen for and potentially cancel.
BattleSessionPreStartEvent event = new BattleSessionPreStartEvent();
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
//Null the next scheduled time, so it can be reset on the next ShortTime.
battleSession.setScheduledStartTime(null);
//Broadcast a cancelled BatterlSession message.
Messaging.sendGlobalMessage(event.getCancellationMsg());
return;
}

//Activate the session
startBattleSession();
}
Expand Down