Rewrote TeamEvent to be a proper builder with final variables.

This follows the proper builder approach, where all variables are
defined in a builder with a final build at the end to make the object.
The variables in the object are final so cannot be changed afterwards.
This commit is contained in:
tastybento 2017-08-28 22:31:47 -07:00
parent 4eaa5a1f5f
commit 3b604058da
6 changed files with 144 additions and 215 deletions

View File

@ -1,93 +0,0 @@
package us.tastybento.bskyblock.api.events.island;
import java.util.UUID;
import us.tastybento.bskyblock.api.events.IslandEvent;
import us.tastybento.bskyblock.database.objects.Island;
/**
* Fired when a team event happens.
*
* @author tastybento
* @since 1.0
*/
public class TeamEvent extends IslandEvent {
public enum TeamReason {
INVITE,
JOIN,
REJECT,
LEAVE,
KICK,
MAKELEADER,
INFO,
DELETE,
UNKNOWN,
UNINVITE
};
/**
* Player involved with this event
*/
private UUID player;
/**
* True if this is an admin action
*/
private boolean admin;
/**
* Reason for this event
*/
private TeamReason reason = TeamReason.UNKNOWN;
public TeamEvent(Island island) {
super(island);
}
/**
* @return the player involved with this event
*/
public UUID getPlayer() {
return player;
}
/**
* @return true if this is an admin action
*/
public boolean isAdmin() {
return admin;
}
/**
* @return the reason for this team event
*/
public TeamReason getReason() {
return reason;
}
/**
* True if this is an admin driven event
* @param admin
* @return TeamEvent
*/
public TeamEvent admin(boolean admin) {
this.admin = admin;
return this;
}
/**
* @param reason for the event
* @return
*/
public TeamEvent reason(TeamReason reason) {
this.reason = reason;
return this;
}
/**
* @param player involved in the event
* @return
*/
public TeamEvent involvedPlayer(UUID player) {
this.player = player;
return this;
}
}

View File

@ -1,39 +0,0 @@
package us.tastybento.bskyblock.api.events.team;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* Fired when a player accepts an invite to join a team.
*
* @author Poslovitch
* @since 1.0
*/
public class PlayerAcceptInviteEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final Player player;
/**
* @param player
*/
public PlayerAcceptInviteEvent(Player player) {
this.player = player;
}
/**
* @return the player
*/
public Player getPlayer() {
return this.player;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -1,56 +0,0 @@
/*******************************************************************************
* This file is part of ASkyBlock.
* <p>
* ASkyBlock is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* ASkyBlock is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with ASkyBlock. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
package us.tastybento.bskyblock.api.events.team;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* Fired when a player rejects an invite to join a team.
*
* @author Poslovitch
* @since 1.0
*/
public class PlayerRejectInviteEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final Player player;
/**
* @param player
*/
public PlayerRejectInviteEvent(Player player) {
this.player = player;
}
/**
* @return the player
*/
public Player getPlayer() {
return this.player;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,120 @@
package us.tastybento.bskyblock.api.events.team;
import java.util.UUID;
import us.tastybento.bskyblock.api.events.IslandEvent;
import us.tastybento.bskyblock.database.objects.Island;
/**
* Fired when a team event happens.
*
* @author tastybento
* @since 1.0
*/
public class TeamEvent extends IslandEvent {
public enum TeamReason {
INVITE,
JOIN,
REJECT,
LEAVE,
KICK,
MAKELEADER,
INFO,
DELETE,
UNKNOWN,
UNINVITE
};
/**
* Player involved with this event
*/
private final UUID player;
/**
* True if this is an admin action
*/
private final boolean admin;
/**
* Reason for this event
*/
private final TeamReason reason;
private TeamEvent(Island island, UUID player, boolean admin, TeamReason reason) {
// Final variables have to be declared in the constuctor
super(island);
this.player = player;
this.admin = admin;
this.reason = reason;
}
public static TeamEventBuilder builder() {
return new TeamEventBuilder();
}
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 boolean admin;
public TeamEventBuilder island(Island island) {
this.island = island;
return this;
}
/**
* True if this is an admin driven event
* @param admin
* @return TeamEvent
*/
public TeamEventBuilder admin(boolean admin) {
this.admin = admin;
return this;
}
/**
* @param reason for the event
* @return
*/
public TeamEventBuilder reason(TeamReason reason) {
this.reason = reason;
return this;
}
/**
* @param player involved in the event
* @return
*/
public TeamEventBuilder involvedPlayer(UUID player) {
this.player = player;
return this;
}
public TeamEvent build() {
return new TeamEvent(island, player, admin, reason);
}
}
/**
* @return the player involved with this event
*/
public UUID getPlayer() {
return player;
}
/**
* @return true if this is an admin action
*/
public boolean isAdmin() {
return admin;
}
/**
* @return the reason for this team event
*/
public TeamReason getReason() {
return reason;
}
}

View File

@ -13,8 +13,8 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.AbstractCommand;
import us.tastybento.bskyblock.api.events.island.TeamEvent;
import us.tastybento.bskyblock.api.events.island.TeamEvent.TeamReason;
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;
@ -100,10 +100,10 @@ public class AdminCommand extends AbstractCommand {
switch (args[0].toLowerCase()) {
case "info":
// Fire event so add-ons can run commands, etc.
TeamEvent event = new TeamEvent(getIslands().getIsland(targetPlayer))
TeamEvent event = TeamEvent.builder().island(getIslands().getIsland(targetPlayer))
.admin(true)
.reason(TeamReason.INFO)
.involvedPlayer(targetPlayer);
.involvedPlayer(targetPlayer).build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
// Display info
@ -126,10 +126,10 @@ public class AdminCommand extends AbstractCommand {
return;
}
// Fire event so add-ons can run commands, etc.
TeamEvent event2 = new TeamEvent(getIslands().getIsland(targetPlayer))
TeamEvent event2 = TeamEvent.builder().island(getIslands().getIsland(targetPlayer))
.admin(true)
.reason(TeamReason.MAKELEADER)
.involvedPlayer(targetPlayer);
.involvedPlayer(targetPlayer).build();
plugin.getServer().getPluginManager().callEvent(event2);
if (event2.isCancelled()) return;
// Display info
@ -201,10 +201,10 @@ public class AdminCommand extends AbstractCommand {
return;
}
// Fire event so add-ons can run commands, etc.
TeamEvent event3 = new TeamEvent(getIslands().getIsland(targetPlayer))
TeamEvent event3 = TeamEvent.builder().island(getIslands().getIsland(targetPlayer))
.admin(true)
.reason(TeamReason.KICK)
.involvedPlayer(targetPlayer);
.involvedPlayer(targetPlayer).build();
plugin.getServer().getPluginManager().callEvent(event3);
if (event3.isCancelled()) return;
// Display info
@ -223,10 +223,10 @@ public class AdminCommand extends AbstractCommand {
return;
case "delete":
// Fire event so add-ons can run commands, etc.
TeamEvent event4 = new TeamEvent(getIslands().getIsland(targetPlayer))
TeamEvent event4 = TeamEvent.builder().island(getIslands().getIsland(targetPlayer))
.admin(true)
.reason(TeamReason.DELETE)
.involvedPlayer(targetPlayer);
.involvedPlayer(targetPlayer).build();
plugin.getServer().getPluginManager().callEvent(event4);
if (event4.isCancelled()) return;
UUID teamLeader = getIslands().getTeamLeader(targetPlayer);
@ -283,10 +283,10 @@ public class AdminCommand extends AbstractCommand {
// Get the team's island
Island teamIsland = getIslands().getIsland(targetPlayer2);
// Fire event so add-ons can run commands, etc.
TeamEvent event = new TeamEvent(teamIsland)
TeamEvent event = TeamEvent.builder().island(teamIsland)
.admin(true)
.reason(TeamReason.JOIN)
.involvedPlayer(targetPlayer);
.involvedPlayer(targetPlayer).build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
// Move player to team's island

View File

@ -21,9 +21,8 @@ import com.google.common.collect.HashBiMap;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.AbstractCommand;
import us.tastybento.bskyblock.api.events.island.TeamEvent;
import us.tastybento.bskyblock.api.events.island.TeamEvent.TeamReason;
import us.tastybento.bskyblock.api.events.team.PlayerAcceptInviteEvent;
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.schematics.Schematic;
@ -473,7 +472,7 @@ public class IslandCommand extends AbstractCommand {
if (DEBUG)
plugin.getLogger().info("DEBUG: executing team command for " + playerUUID);
// Fire event so add-ons can run commands, etc.
TeamEvent event = new TeamEvent(getIslands().getIsland(playerUUID)).reason(TeamReason.INFO).involvedPlayer(playerUUID);
TeamEvent event = TeamEvent.builder().island(getIslands().getIsland(playerUUID)).reason(TeamReason.INFO).involvedPlayer(playerUUID).build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
if (teamLeaderUUID.equals(playerUUID)) {
@ -623,7 +622,7 @@ public class IslandCommand extends AbstractCommand {
Util.sendMessage(player, ChatColor.RED + getLocale(sender).get("invite.removingInvite"));
}
// Fire event so add-ons can run commands, etc.
TeamEvent event = new TeamEvent(getIslands().getIsland(playerUUID)).reason(TeamReason.INVITE).involvedPlayer(invitedPlayerUUID);
TeamEvent event = TeamEvent.builder().island(getIslands().getIsland(playerUUID)).reason(TeamReason.INVITE).involvedPlayer(invitedPlayerUUID).build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
// Put the invited player (key) onto the list with inviter (value)
@ -673,7 +672,7 @@ public class IslandCommand extends AbstractCommand {
@Override
public void execute(CommandSender sender, String[] args) {
// Fire event so add-ons can run commands, etc.
TeamEvent event = new TeamEvent(getIslands().getIsland(playerUUID)).reason(TeamReason.UNINVITE).involvedPlayer(playerUUID);
TeamEvent event = TeamEvent.builder().island(getIslands().getIsland(playerUUID)).reason(TeamReason.UNINVITE).involvedPlayer(playerUUID).build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
if (inviteList.inverse().containsKey(playerUUID)) {
@ -720,7 +719,7 @@ public class IslandCommand extends AbstractCommand {
return;
}
// Fire event so add-ons can run commands, etc.
TeamEvent event = new TeamEvent(getIslands().getIsland(playerUUID)).reason(TeamReason.LEAVE).involvedPlayer(playerUUID);
TeamEvent event = TeamEvent.builder().island(getIslands().getIsland(playerUUID)).reason(TeamReason.LEAVE).involvedPlayer(playerUUID).build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
// Check for confirmation
@ -807,7 +806,7 @@ public class IslandCommand extends AbstractCommand {
return;
}
// Fire event so add-ons can run commands, etc.
TeamEvent event = new TeamEvent(getIslands().getIsland(playerUUID)).reason(TeamReason.KICK).involvedPlayer(targetPlayerUUID);
TeamEvent event = TeamEvent.builder().island(getIslands().getIsland(playerUUID)).reason(TeamReason.KICK).involvedPlayer(targetPlayerUUID).build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
// Check for confirmation
@ -894,7 +893,7 @@ public class IslandCommand extends AbstractCommand {
if (DEBUG)
plugin.getLogger().info("DEBUG: Invite is valid");
// Fire event so add-ons can run commands, etc.
TeamEvent event = new TeamEvent(getIslands().getIsland(prospectiveTeamLeaderUUID)).reason(TeamReason.JOIN).involvedPlayer(playerUUID);
TeamEvent event = TeamEvent.builder().island(getIslands().getIsland(prospectiveTeamLeaderUUID)).reason(TeamReason.JOIN).involvedPlayer(playerUUID).build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
// Remove the invite
@ -929,8 +928,6 @@ public class IslandCommand extends AbstractCommand {
// Put player back into normal mode
player.setGameMode(GameMode.SURVIVAL);
// Fire event so add-ons can run commands, etc.
plugin.getServer().getPluginManager().callEvent(new PlayerAcceptInviteEvent(player));
Util.sendMessage(player, ChatColor.GREEN + getLocale(sender).get("invite.youHaveJoinedAnIsland").replace("[label]", Settings.ISLANDCOMMAND));
if (plugin.getServer().getPlayer(inviteList.get(playerUUID)) != null) {
@ -970,7 +967,7 @@ public class IslandCommand extends AbstractCommand {
// Reject /island reject
if (inviteList.containsKey(playerUUID)) {
// Fire event so add-ons can run commands, etc.
TeamEvent event = new TeamEvent(getIslands().getIsland(inviteList.get(playerUUID))).reason(TeamReason.REJECT).involvedPlayer(playerUUID);
TeamEvent event = TeamEvent.builder().island(getIslands().getIsland(inviteList.get(playerUUID))).reason(TeamReason.REJECT).involvedPlayer(playerUUID).build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
@ -1038,7 +1035,7 @@ public class IslandCommand extends AbstractCommand {
return;
}
// Fire event so add-ons can run commands, etc.
TeamEvent event = new TeamEvent(getIslands().getIsland(playerUUID)).reason(TeamReason.MAKELEADER).involvedPlayer(targetUUID);
TeamEvent event = TeamEvent.builder().island(getIslands().getIsland(playerUUID)).reason(TeamReason.MAKELEADER).involvedPlayer(targetUUID).build();
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;