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
This commit is contained in:
Florian CUNY 2017-12-28 15:29:32 +01:00
parent 40c59f2f7e
commit 390d58cc8b
19 changed files with 138 additions and 172 deletions

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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 {}

View File

@ -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;

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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);
}
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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<Addon> getAddons() {
return addons;
}

View File

@ -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());