From 390d58cc8bf6b2c31202b55dcfda5876917e1280 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Thu, 28 Dec 2017 15:29:32 +0100 Subject: [PATCH] AddonsAPI & Events changes Moved PremadeEvent to api/events Made use of PremadeEvent for all existing events Renamed TeamReason to Reason in TeamEvent Made the addon events follow the used builder pattern Renamed #loadAddons() to #enableAddons() in AddonsManager Added #disableAddons() in AddonsManager --- .../us/tastybento/bskyblock/BSkyBlock.java | 21 +----- .../api/addons/event/AddonDisableEvent.java | 25 ------- .../api/addons/event/AddonEnableEvent.java | 25 ------- .../api/addons/event/AddonLoadEvent.java | 25 ------- .../bskyblock/api/events/BSBReadyEvent.java | 16 +---- .../bskyblock/api/events/IslandBaseEvent.java | 14 +--- .../event => events}/PremadeEvent.java | 4 +- .../api/events/addon/AddonBaseEvent.java | 22 ++++++ .../api/events/addon/AddonEvent.java | 71 +++++++++++++++++++ .../api/events/command/CommandEvent.java | 17 +---- .../api/events/purge/PurgeStartEvent.java | 15 +--- .../bskyblock/api/events/team/TeamEvent.java | 8 +-- .../island/teams/IslandTeamCommand.java | 3 +- .../teams/IslandTeamInviteAcceptCommand.java | 3 +- .../island/teams/IslandTeamInviteCommand.java | 3 +- .../teams/IslandTeamInviteRejectCommand.java | 3 +- .../teams/IslandTeamSetownerCommand.java | 3 +- .../bskyblock/managers/AddonsManager.java | 29 ++++++-- src/test/java/bskyblock/TestBSkyBlock.java | 3 +- 19 files changed, 138 insertions(+), 172 deletions(-) delete mode 100644 src/main/java/us/tastybento/bskyblock/api/addons/event/AddonDisableEvent.java delete mode 100644 src/main/java/us/tastybento/bskyblock/api/addons/event/AddonEnableEvent.java delete mode 100644 src/main/java/us/tastybento/bskyblock/api/addons/event/AddonLoadEvent.java rename src/main/java/us/tastybento/bskyblock/api/{addons/event => events}/PremadeEvent.java (73%) create mode 100644 src/main/java/us/tastybento/bskyblock/api/events/addon/AddonBaseEvent.java create mode 100644 src/main/java/us/tastybento/bskyblock/api/events/addon/AddonEvent.java diff --git a/src/main/java/us/tastybento/bskyblock/BSkyBlock.java b/src/main/java/us/tastybento/bskyblock/BSkyBlock.java index c7ffd5d4a..0b44850cb 100755 --- a/src/main/java/us/tastybento/bskyblock/BSkyBlock.java +++ b/src/main/java/us/tastybento/bskyblock/BSkyBlock.java @@ -1,7 +1,6 @@ package us.tastybento.bskyblock; import java.io.File; -import java.io.IOException; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; @@ -9,7 +8,6 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import us.tastybento.bskyblock.api.BSBModule; -import us.tastybento.bskyblock.api.addons.event.AddonDisableEvent; import us.tastybento.bskyblock.commands.AdminCommand; import us.tastybento.bskyblock.commands.IslandCommand; import us.tastybento.bskyblock.config.PluginConfig; @@ -107,7 +105,7 @@ public class BSkyBlock extends JavaPlugin implements BSBModule { // Load addons addonsManager = new AddonsManager(); - addonsManager.loadAddons(); + addonsManager.enableAddons(); /* *DEBUG CODE @@ -145,21 +143,8 @@ public class BSkyBlock extends JavaPlugin implements BSBModule { } @Override - public void onDisable(){ - // Unload addons - addonsManager.getAddons().forEach(addon -> { - addon.onDisable(); - getServer().getPluginManager().callEvent(new AddonDisableEvent(addon)); - System.out.println("Disabling " + addon.getDescription().getName() + "..."); - }); - - addonsManager.getLoader().forEach(loader -> { - try { - loader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - }); + public void onDisable() { + addonsManager.disableAddons(); // Save data playersManager.shutdown(); islandsManager.shutdown(); diff --git a/src/main/java/us/tastybento/bskyblock/api/addons/event/AddonDisableEvent.java b/src/main/java/us/tastybento/bskyblock/api/addons/event/AddonDisableEvent.java deleted file mode 100644 index 76e29830a..000000000 --- a/src/main/java/us/tastybento/bskyblock/api/addons/event/AddonDisableEvent.java +++ /dev/null @@ -1,25 +0,0 @@ -package us.tastybento.bskyblock.api.addons.event; - -import us.tastybento.bskyblock.api.addons.Addon; - -/** - * This event is run when an addon is getting disabled. - * - * @author ComminQ - */ -public class AddonDisableEvent extends PremadeEvent { - private Addon addon; - - public AddonDisableEvent(Addon addon){ - this.addon = addon; - } - - public Addon getAddon() { - return addon; - } - - public void setAddon(Addon addon) { - this.addon = addon; - } - -} diff --git a/src/main/java/us/tastybento/bskyblock/api/addons/event/AddonEnableEvent.java b/src/main/java/us/tastybento/bskyblock/api/addons/event/AddonEnableEvent.java deleted file mode 100644 index ab6516fca..000000000 --- a/src/main/java/us/tastybento/bskyblock/api/addons/event/AddonEnableEvent.java +++ /dev/null @@ -1,25 +0,0 @@ -package us.tastybento.bskyblock.api.addons.event; - -import us.tastybento.bskyblock.api.addons.Addon; - -/** - * This event is run when an addon is getting enabled. - * - * @author ComminQ - */ -public class AddonEnableEvent extends PremadeEvent { - private Addon addon; - - public AddonEnableEvent(Addon addon){ - this.addon = addon; - } - - public Addon getAddon() { - return addon; - } - - public void setAddon(Addon addon) { - this.addon = addon; - } - -} diff --git a/src/main/java/us/tastybento/bskyblock/api/addons/event/AddonLoadEvent.java b/src/main/java/us/tastybento/bskyblock/api/addons/event/AddonLoadEvent.java deleted file mode 100644 index 452a1f1e2..000000000 --- a/src/main/java/us/tastybento/bskyblock/api/addons/event/AddonLoadEvent.java +++ /dev/null @@ -1,25 +0,0 @@ -package us.tastybento.bskyblock.api.addons.event; - -import us.tastybento.bskyblock.api.addons.Addon; - -/** - * This event is run when an addon is getting loaded. - * - * @author ComminQ - */ -public class AddonLoadEvent extends PremadeEvent { - private Addon addon; - - public AddonLoadEvent(Addon addon){ - this.addon = addon; - } - - public Addon getAddon() { - return addon; - } - - public void setAddon(Addon addon) { - this.addon = addon; - } - -} diff --git a/src/main/java/us/tastybento/bskyblock/api/events/BSBReadyEvent.java b/src/main/java/us/tastybento/bskyblock/api/events/BSBReadyEvent.java index 1d3642104..41da85eed 100644 --- a/src/main/java/us/tastybento/bskyblock/api/events/BSBReadyEvent.java +++ b/src/main/java/us/tastybento/bskyblock/api/events/BSBReadyEvent.java @@ -1,23 +1,9 @@ package us.tastybento.bskyblock.api.events; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - /** * Fired when BSkyBlock is ready to play and all files are loaded * * @author tastybento * @since 1.0 */ -public class BSBReadyEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} +public class BSBReadyEvent extends PremadeEvent {} diff --git a/src/main/java/us/tastybento/bskyblock/api/events/IslandBaseEvent.java b/src/main/java/us/tastybento/bskyblock/api/events/IslandBaseEvent.java index cd585b06f..71524cb97 100644 --- a/src/main/java/us/tastybento/bskyblock/api/events/IslandBaseEvent.java +++ b/src/main/java/us/tastybento/bskyblock/api/events/IslandBaseEvent.java @@ -4,8 +4,6 @@ import java.util.UUID; import org.bukkit.Location; import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; import us.tastybento.bskyblock.database.objects.Island; @@ -14,8 +12,7 @@ import us.tastybento.bskyblock.database.objects.Island; * @author Poslovitch * @version 1.0 */ -public class IslandBaseEvent extends Event implements Cancellable{ - private static final HandlerList handlers = new HandlerList(); +public class IslandBaseEvent extends PremadeEvent implements Cancellable { private boolean cancelled; private final Island island; @@ -80,15 +77,6 @@ public class IslandBaseEvent extends Event implements Cancellable{ return location; } - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - @Override public boolean isCancelled() { return cancelled; diff --git a/src/main/java/us/tastybento/bskyblock/api/addons/event/PremadeEvent.java b/src/main/java/us/tastybento/bskyblock/api/events/PremadeEvent.java similarity index 73% rename from src/main/java/us/tastybento/bskyblock/api/addons/event/PremadeEvent.java rename to src/main/java/us/tastybento/bskyblock/api/events/PremadeEvent.java index d7cb5a687..2131aeb8e 100644 --- a/src/main/java/us/tastybento/bskyblock/api/addons/event/PremadeEvent.java +++ b/src/main/java/us/tastybento/bskyblock/api/events/PremadeEvent.java @@ -1,9 +1,9 @@ -package us.tastybento.bskyblock.api.addons.event; +package us.tastybento.bskyblock.api.events; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -public abstract class PremadeEvent extends Event{ +public abstract class PremadeEvent extends Event { public static final HandlerList handlers = new HandlerList(); diff --git a/src/main/java/us/tastybento/bskyblock/api/events/addon/AddonBaseEvent.java b/src/main/java/us/tastybento/bskyblock/api/events/addon/AddonBaseEvent.java new file mode 100644 index 000000000..cc8c4d3ec --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/api/events/addon/AddonBaseEvent.java @@ -0,0 +1,22 @@ +package us.tastybento.bskyblock.api.events.addon; + +import us.tastybento.bskyblock.api.addons.Addon; +import us.tastybento.bskyblock.api.events.PremadeEvent; + +/** + * @author Poslovitch + * @since 1.0 + */ +public class AddonBaseEvent extends PremadeEvent { + + private final Addon addon; + + public AddonBaseEvent(Addon addon) { + super(); + this.addon = addon; + } + + public Addon getAddon() { + return addon; + } +} diff --git a/src/main/java/us/tastybento/bskyblock/api/events/addon/AddonEvent.java b/src/main/java/us/tastybento/bskyblock/api/events/addon/AddonEvent.java new file mode 100644 index 000000000..0b6318a41 --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/api/events/addon/AddonEvent.java @@ -0,0 +1,71 @@ +package us.tastybento.bskyblock.api.events.addon; + +import us.tastybento.bskyblock.api.addons.Addon; + +public class AddonEvent { + + public enum Reason { + ENABLE, + DISABLE, + LOAD, + UNKNOWN + } + + public static AddonEventBuilder builder() { + return new AddonEventBuilder(); + } + + public static class AddonEnableEvent extends AddonBaseEvent { + private AddonEnableEvent(Addon addon) { + // Final variables have to be declared in the constuctor + super(addon); + } + } + public static class AddonDisableEvent extends AddonBaseEvent { + private AddonDisableEvent(Addon addon) { + // Final variables have to be declared in the constuctor + super(addon); + } + } + public static class AddonLoadEvent extends AddonBaseEvent { + private AddonLoadEvent(Addon addon) { + // Final variables have to be declared in the constuctor + super(addon); + } + } + public static class AddonGeneralEvent extends AddonBaseEvent { + private AddonGeneralEvent(Addon addon) { + // Final variables have to be declared in the constuctor + super(addon); + } + } + + public static class AddonEventBuilder { + // Here field are NOT final. They are just used for the building. + private Addon addon; + private Reason reason = Reason.UNKNOWN; + + public AddonEventBuilder addon(Addon addon) { + this.addon = addon; + return this; + } + + public AddonEventBuilder reason(Reason reason) { + this.reason = reason; + return this; + } + + public AddonBaseEvent build() { + switch (reason) { + case ENABLE: + return new AddonEnableEvent(addon); + case DISABLE: + return new AddonDisableEvent(addon); + case LOAD: + return new AddonLoadEvent(addon); + default: + return new AddonGeneralEvent(addon); + } + } + } +} diff --git a/src/main/java/us/tastybento/bskyblock/api/events/command/CommandEvent.java b/src/main/java/us/tastybento/bskyblock/api/events/command/CommandEvent.java index b11ce90e1..02441343a 100644 --- a/src/main/java/us/tastybento/bskyblock/api/events/command/CommandEvent.java +++ b/src/main/java/us/tastybento/bskyblock/api/events/command/CommandEvent.java @@ -3,8 +3,7 @@ package us.tastybento.bskyblock.api.events.command; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; +import us.tastybento.bskyblock.api.events.PremadeEvent; /** * Fired when a team event happens. @@ -12,9 +11,8 @@ import org.bukkit.event.HandlerList; * @author tastybento * @since 1.0 */ -public class CommandEvent extends Event implements Cancellable { - - private static final HandlerList handlers = new HandlerList(); +public class CommandEvent extends PremadeEvent implements Cancellable { + private boolean cancelled; private final CommandSender sender; @@ -92,13 +90,4 @@ public class CommandEvent extends Event implements Cancellable { public void setCancelled(boolean arg0) { cancelled = arg0; } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } } diff --git a/src/main/java/us/tastybento/bskyblock/api/events/purge/PurgeStartEvent.java b/src/main/java/us/tastybento/bskyblock/api/events/purge/PurgeStartEvent.java index 9f4a131bb..c742766cd 100644 --- a/src/main/java/us/tastybento/bskyblock/api/events/purge/PurgeStartEvent.java +++ b/src/main/java/us/tastybento/bskyblock/api/events/purge/PurgeStartEvent.java @@ -4,8 +4,7 @@ import java.util.List; import java.util.UUID; import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; +import us.tastybento.bskyblock.api.events.PremadeEvent; /** * This event is fired when islands to remove have been chosen and before starting to remove them. @@ -15,8 +14,7 @@ import org.bukkit.event.HandlerList; * @author Poslovitch * @since 1.0 */ -public class PurgeStartEvent extends Event implements Cancellable { - private static final HandlerList handlers = new HandlerList(); +public class PurgeStartEvent extends PremadeEvent implements Cancellable { private boolean cancelled; private final UUID user; @@ -79,13 +77,4 @@ public class PurgeStartEvent extends Event implements Cancellable { public void setCancelled(boolean cancel) { this.cancelled = cancel; } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } } diff --git a/src/main/java/us/tastybento/bskyblock/api/events/team/TeamEvent.java b/src/main/java/us/tastybento/bskyblock/api/events/team/TeamEvent.java index 8f2168163..2998a9f13 100644 --- a/src/main/java/us/tastybento/bskyblock/api/events/team/TeamEvent.java +++ b/src/main/java/us/tastybento/bskyblock/api/events/team/TeamEvent.java @@ -15,7 +15,7 @@ import us.tastybento.bskyblock.database.objects.Island; */ public class TeamEvent { - public enum TeamReason { + public enum Reason { INVITE, JOIN, REJECT, @@ -94,10 +94,9 @@ public class TeamEvent { } public static class TeamEventBuilder { - // Here field are NOT final. They are just used for the building. private Island island; private UUID player; - private TeamReason reason = TeamReason.UNKNOWN; + private Reason reason = Reason.UNKNOWN; private boolean admin; private Location location; @@ -120,7 +119,7 @@ public class TeamEvent { * @param reason for the event * @return */ - public TeamEventBuilder reason(TeamReason reason) { + public TeamEventBuilder reason(Reason reason) { this.reason = reason; return this; } @@ -161,7 +160,6 @@ public class TeamEvent { return new TeamUninviteEvent(island, player, admin, location); default: return new TeamGeneralEvent(island, player, admin, location); - } } } diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamCommand.java index 64cff9485..e94818fcd 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamCommand.java @@ -11,7 +11,6 @@ import us.tastybento.bskyblock.api.commands.CompositeCommand; import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.team.TeamEvent; -import us.tastybento.bskyblock.api.events.team.TeamEvent.TeamReason; import us.tastybento.bskyblock.config.Settings; public class IslandTeamCommand extends AbstractIslandTeamCommand { @@ -43,7 +42,7 @@ public class IslandTeamCommand extends AbstractIslandTeamCommand { IslandBaseEvent event = TeamEvent.builder() .island(getIslands() .getIsland(playerUUID)) - .reason(TeamReason.INFO) + .reason(TeamEvent.Reason.INFO) .involvedPlayer(playerUUID) .build(); getPlugin().getServer().getPluginManager().callEvent(event); diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteAcceptCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteAcceptCommand.java index 9420e5ff5..114f74c16 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteAcceptCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteAcceptCommand.java @@ -10,7 +10,6 @@ import org.bukkit.Location; import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.team.TeamEvent; -import us.tastybento.bskyblock.api.events.team.TeamEvent.TeamReason; import us.tastybento.bskyblock.config.Settings; import us.tastybento.bskyblock.database.objects.Island; @@ -58,7 +57,7 @@ public class IslandTeamInviteAcceptCommand extends AbstractIslandTeamCommand { IslandBaseEvent event = TeamEvent.builder() .island(getIslands() .getIsland(prospectiveTeamLeaderUUID)) - .reason(TeamReason.JOIN) + .reason(TeamEvent.Reason.JOIN) .involvedPlayer(playerUUID) .build(); getPlugin().getServer().getPluginManager().callEvent(event); diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteCommand.java index 0b9b514d6..21e92ce85 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteCommand.java @@ -14,7 +14,6 @@ import org.bukkit.permissions.PermissionAttachmentInfo; import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.team.TeamEvent; -import us.tastybento.bskyblock.api.events.team.TeamEvent.TeamReason; import us.tastybento.bskyblock.config.Settings; import us.tastybento.bskyblock.util.Util; @@ -117,7 +116,7 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand { // Fire event so add-ons can run commands, etc. IslandBaseEvent event = TeamEvent.builder() .island(getIslands().getIsland(playerUUID)) - .reason(TeamReason.INVITE) + .reason(TeamEvent.Reason.INVITE) .involvedPlayer(invitedPlayerUUID) .build(); getPlugin().getServer().getPluginManager().callEvent(event); diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteRejectCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteRejectCommand.java index db2961770..b255cc5b2 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteRejectCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteRejectCommand.java @@ -6,7 +6,6 @@ import java.util.UUID; import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.team.TeamEvent; -import us.tastybento.bskyblock.api.events.team.TeamEvent.TeamReason; import us.tastybento.bskyblock.config.Settings; public class IslandTeamInviteRejectCommand extends AbstractIslandTeamCommand { @@ -31,7 +30,7 @@ public class IslandTeamInviteRejectCommand extends AbstractIslandTeamCommand { IslandBaseEvent event = TeamEvent.builder() .island(getIslands() .getIsland(inviteList.get(playerUUID))) - .reason(TeamReason.REJECT) + .reason(TeamEvent.Reason.REJECT) .involvedPlayer(playerUUID) .build(); getPlugin().getServer().getPluginManager().callEvent(event); diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamSetownerCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamSetownerCommand.java index 894810777..f94fe72ce 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamSetownerCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamSetownerCommand.java @@ -12,7 +12,6 @@ import org.bukkit.permissions.PermissionAttachmentInfo; import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.team.TeamEvent; -import us.tastybento.bskyblock.api.events.team.TeamEvent.TeamReason; import us.tastybento.bskyblock.config.Settings; import us.tastybento.bskyblock.database.objects.Island; import us.tastybento.bskyblock.util.Util; @@ -66,7 +65,7 @@ public class IslandTeamSetownerCommand extends AbstractIslandTeamCommand { IslandBaseEvent event = TeamEvent.builder() .island(getIslands() .getIsland(playerUUID)) - .reason(TeamReason.MAKELEADER) + .reason(TeamEvent.Reason.MAKELEADER) .involvedPlayer(targetUUID) .build(); getPlugin().getServer().getPluginManager().callEvent(event); diff --git a/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java b/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java index d14ca99ef..3491243ce 100644 --- a/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java +++ b/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java @@ -16,10 +16,9 @@ import org.bukkit.Bukkit; import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.addons.Addon; import us.tastybento.bskyblock.api.addons.AddonClassLoader; -import us.tastybento.bskyblock.api.addons.event.AddonEnableEvent; -import us.tastybento.bskyblock.api.addons.event.AddonLoadEvent; import us.tastybento.bskyblock.api.addons.exception.InvalidAddonFormatException; import us.tastybento.bskyblock.api.addons.exception.InvalidAddonInheritException; +import us.tastybento.bskyblock.api.events.addon.AddonEvent; /** * @author Tastybento, ComminQ @@ -38,7 +37,7 @@ public final class AddonsManager { /** * Loads all the addons from the addons folder */ - public void loadAddons() { + public void enableAddons() { File f = new File(BSkyBlock.getInstance().getDataFolder(), "addons"); if (f.exists()) { if (f.isDirectory()) { @@ -67,7 +66,7 @@ public final class AddonsManager { this.addons.stream().forEach(addon -> { addon.onEnable(); - BSkyBlock.getInstance().getServer().getPluginManager().callEvent(new AddonEnableEvent(addon)); + Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build()); addon.setEnabled(true); BSkyBlock.getInstance().getLogger().info("Enabling " + addon.getDescription().getName() + "..."); }); @@ -111,7 +110,7 @@ public final class AddonsManager { addon = loader.addon; addon.setDataFolder(new File(f.getParent(), f.getName().replace(".jar", ""))); addon.setFile(f); - Bukkit.getPluginManager().callEvent(new AddonLoadEvent(addon)); + Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build()); this.addons.add(addon); addon.onLoad(); BSkyBlock.getInstance().getLogger().info("Loading BSkyBlock addon " + addon.getDescription().getName() + "..."); @@ -137,6 +136,26 @@ public final class AddonsManager { return map; } + /** + * Disable all the enabled addons + */ + public void disableAddons() { + // Unload addons + addons.forEach(addon -> { + addon.onDisable(); + Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build()); + System.out.println("Disabling " + addon.getDescription().getName() + "..."); + }); + + loader.forEach(loader -> { + try { + loader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + public List getAddons() { return addons; } diff --git a/src/test/java/bskyblock/TestBSkyBlock.java b/src/test/java/bskyblock/TestBSkyBlock.java index 57a40c868..d54ec40b5 100644 --- a/src/test/java/bskyblock/TestBSkyBlock.java +++ b/src/test/java/bskyblock/TestBSkyBlock.java @@ -29,7 +29,6 @@ import us.tastybento.bskyblock.api.commands.CompositeCommand; import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.team.TeamEvent; -import us.tastybento.bskyblock.api.events.team.TeamEvent.TeamReason; import us.tastybento.bskyblock.config.Settings; import us.tastybento.bskyblock.util.Util; @@ -69,7 +68,7 @@ public class TestBSkyBlock { // Test island events IslandBaseEvent event = TeamEvent.builder() //.island(getIslands().getIsland(playerUUID)) - .reason(TeamReason.INFO) + .reason(TeamEvent.Reason.INFO) .involvedPlayer(playerUUID) .build(); assertEquals(playerUUID, event.getPlayerUUID());