Fix for double calling of events (#1111)
* Fixes tests for AdminSetrankCommand https://github.com/BentoBoxWorld/BentoBox/issues/1107 * Fixes double calling of events. https://github.com/BentoBoxWorld/BentoBox/issues/1110 * Makes AddonEvent static https://github.com/BentoBoxWorld/BentoBox/pull/1111
This commit is contained in:
parent
c385ee78f5
commit
aa5c253e03
|
@ -5,13 +5,11 @@ import java.util.List;
|
|||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.island.IslandEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -85,14 +83,14 @@ public class AdminRegisterCommand extends ConfirmableCommand {
|
|||
}
|
||||
user.sendMessage("commands.admin.register.registered-island", "[xyz]", Util.xyz(i.getCenter().toVector()));
|
||||
user.sendMessage("general.success");
|
||||
IslandBaseEvent event = IslandEvent.builder()
|
||||
.island(i)
|
||||
.location(i.getCenter())
|
||||
.reason(IslandEvent.Reason.REGISTERED)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
// Build and call event
|
||||
IslandEvent.builder()
|
||||
.island(i)
|
||||
.location(i.getCenter())
|
||||
.reason(IslandEvent.Reason.REGISTERED)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
return true;
|
||||
}).orElse(false)) {
|
||||
// Island does not exist - this is a reservation
|
||||
|
@ -108,19 +106,19 @@ public class AdminRegisterCommand extends ConfirmableCommand {
|
|||
i.setReserved(true);
|
||||
i.getCenter().getBlock().setType(Material.BEDROCK);
|
||||
user.sendMessage("commands.admin.register.reserved-island", "[xyz]", Util.xyz(i.getCenter().toVector()));
|
||||
IslandBaseEvent event = IslandEvent.builder()
|
||||
.island(i)
|
||||
.location(i.getCenter())
|
||||
.reason(IslandEvent.Reason.RESERVED)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
// Build and fire event
|
||||
IslandEvent.builder()
|
||||
.island(i)
|
||||
.location(i.getCenter())
|
||||
.reason(IslandEvent.Reason.RESERVED)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
});
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -69,7 +69,7 @@ public class AdminSetrankCommand extends CompositeCommand {
|
|||
// We want to change the player's rank on the island he is part of.
|
||||
|
||||
// Check if the target is part of an island
|
||||
if (!getPlugin().getIslands().hasIsland(getWorld(), targetUUID) && !getPlugin().getIslands().inTeam(getWorld(), targetUUID)) {
|
||||
if (!getIslands().hasIsland(getWorld(), targetUUID) && !getPlugin().getIslands().inTeam(getWorld(), targetUUID)) {
|
||||
user.sendMessage("general.errors.player-has-no-island");
|
||||
return false;
|
||||
}
|
||||
|
@ -96,9 +96,9 @@ public class AdminSetrankCommand extends CompositeCommand {
|
|||
User target = User.getInstance(targetUUID);
|
||||
Island island;
|
||||
if (ownerUUID != null) {
|
||||
island = getPlugin().getIslands().getIsland(getWorld(), ownerUUID);
|
||||
island = getIslands().getIsland(getWorld(), ownerUUID);
|
||||
} else {
|
||||
island = getPlugin().getIslands().getIsland(getWorld(), targetUUID);
|
||||
island = getIslands().getIsland(getWorld(), targetUUID);
|
||||
}
|
||||
int currentRank = island.getRank(target);
|
||||
island.setRank(target, rankValue);
|
||||
|
|
|
@ -4,14 +4,12 @@ import java.util.List;
|
|||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.island.IslandEvent;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
|
@ -57,15 +55,14 @@ public class AdminSetspawnCommand extends ConfirmableCommand {
|
|||
private void setSpawn(User user, Island i) {
|
||||
if (!i.getMembers().isEmpty()) {
|
||||
if (i.isOwned()) {
|
||||
// Fire event
|
||||
IslandBaseEvent event = IslandEvent.builder()
|
||||
.island(i)
|
||||
.location(i.getCenter())
|
||||
.reason(IslandEvent.Reason.UNREGISTERED)
|
||||
.involvedPlayer(i.getOwner())
|
||||
.admin(true)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
// Build and fire event
|
||||
IslandEvent.builder()
|
||||
.island(i)
|
||||
.location(i.getCenter())
|
||||
.reason(IslandEvent.Reason.UNREGISTERED)
|
||||
.involvedPlayer(i.getOwner())
|
||||
.admin(true)
|
||||
.build();
|
||||
}
|
||||
// If island is owned, then unregister the owner and any members
|
||||
new ImmutableSet.Builder<UUID>().addAll(i.getMembers().keySet()).build().forEach(m -> {
|
||||
|
|
|
@ -5,13 +5,10 @@ import java.util.List;
|
|||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.island.IslandEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -63,14 +60,13 @@ public class AdminUnregisterCommand extends ConfirmableCommand {
|
|||
private void unregisterPlayer(User user, UUID targetUUID) {
|
||||
// Unregister island
|
||||
Island oldIsland = getIslands().getIsland(getWorld(), targetUUID);
|
||||
IslandBaseEvent event = IslandEvent.builder()
|
||||
.island(oldIsland)
|
||||
.location(oldIsland.getCenter())
|
||||
.reason(IslandEvent.Reason.UNREGISTERED)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
IslandEvent.builder()
|
||||
.island(oldIsland)
|
||||
.location(oldIsland.getCenter())
|
||||
.reason(IslandEvent.Reason.UNREGISTERED)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
// Remove all island members
|
||||
new ImmutableSet.Builder<UUID>().addAll(oldIsland.getMembers().keySet()).build().forEach(m -> {
|
||||
getIslands().removePlayer(getWorld(), m);
|
||||
|
|
|
@ -3,10 +3,7 @@ package world.bentobox.bentobox.api.commands.admin.team;
|
|||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -69,13 +66,12 @@ public class AdminTeamAddCommand extends CompositeCommand {
|
|||
if (teamIsland != null) {
|
||||
getIslands().setJoinTeam(teamIsland, targetUUID);
|
||||
user.sendMessage("commands.admin.team.add.success", TextVariables.NAME, target.getName(), "[owner]", owner.getName());
|
||||
IslandBaseEvent event = TeamEvent.builder()
|
||||
.island(teamIsland)
|
||||
.reason(TeamEvent.Reason.JOINED)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
TeamEvent.builder()
|
||||
.island(teamIsland)
|
||||
.reason(TeamEvent.Reason.JOINED)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
return true;
|
||||
} else {
|
||||
user.sendMessage("general.errors.player-has-no-island");
|
||||
|
|
|
@ -3,10 +3,7 @@ package world.bentobox.bentobox.api.commands.admin.team;
|
|||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -57,13 +54,12 @@ public class AdminTeamDisbandCommand extends CompositeCommand {
|
|||
// The owner gets to keep the island
|
||||
if (!m.equals(targetUUID)) {
|
||||
getIslands().setLeaveTeam(getWorld(), m);
|
||||
IslandBaseEvent event = TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.KICK)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.KICK)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
}
|
||||
});
|
||||
user.sendMessage("commands.admin.team.disband.success", TextVariables.NAME, args.get(0));
|
||||
|
|
|
@ -3,11 +3,9 @@ package world.bentobox.bentobox.api.commands.admin.team;
|
|||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -71,13 +69,12 @@ public class AdminTeamKickCommand extends CompositeCommand {
|
|||
user.sendMessage("commands.admin.team.kick.success", TextVariables.NAME, target.getName(), "[owner]", getPlayers().getName(island.getOwner()));
|
||||
|
||||
// Fire event so add-ons know
|
||||
IslandBaseEvent event = TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.KICK)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.KICK)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,10 +3,7 @@ package world.bentobox.bentobox.api.commands.admin.team;
|
|||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -55,13 +52,12 @@ public class AdminTeamSetownerCommand extends CompositeCommand {
|
|||
user.sendMessage("commands.admin.team.setowner.success", TextVariables.NAME, args.get(0));
|
||||
// Fire event so add-ons know
|
||||
Island island = getIslands().getIsland(getWorld(), targetUUID);
|
||||
IslandBaseEvent event = TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.SETOWNER)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.SETOWNER)
|
||||
.involvedPlayer(targetUUID)
|
||||
.admin(true)
|
||||
.build();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ import org.eclipse.jdt.annotation.NonNull;
|
|||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
|
@ -223,12 +222,11 @@ public class IslandResetCommand extends ConfirmableCommand {
|
|||
}
|
||||
|
||||
// Fire event
|
||||
IslandBaseEvent e = TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.DELETE)
|
||||
.involvedPlayer(memberUUID)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(e);
|
||||
TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.DELETE)
|
||||
.involvedPlayer(memberUUID)
|
||||
.build();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@ import org.eclipse.jdt.annotation.NonNull;
|
|||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -158,14 +157,13 @@ public class IslandTeamCommand extends CompositeCommand {
|
|||
}
|
||||
|
||||
private boolean fireEvent(User user) {
|
||||
IslandBaseEvent event = TeamEvent.builder()
|
||||
return TeamEvent.builder()
|
||||
.island(getIslands()
|
||||
.getIsland(getWorld(), user.getUniqueId()))
|
||||
.reason(TeamEvent.Reason.INFO)
|
||||
.involvedPlayer(user.getUniqueId())
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
return event.isCancelled();
|
||||
.build()
|
||||
.isCancelled();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,11 +3,8 @@ package world.bentobox.bentobox.api.commands.island.team;
|
|||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
import world.bentobox.bentobox.api.commands.island.team.Invite.Type;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -58,13 +55,12 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
|
|||
return false;
|
||||
}
|
||||
// Fire event so add-ons can run commands, etc.
|
||||
IslandBaseEvent event = TeamEvent.builder()
|
||||
return !TeamEvent.builder()
|
||||
.island(getIslands().getIsland(getWorld(), prospectiveOwnerUUID))
|
||||
.reason(TeamEvent.Reason.JOIN)
|
||||
.involvedPlayer(playerUUID)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
return !event.isCancelled();
|
||||
.build()
|
||||
.isCancelled();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -148,12 +144,11 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
|
|||
}
|
||||
getIslands().save(teamIsland);
|
||||
// Fire event
|
||||
IslandBaseEvent e = TeamEvent.builder()
|
||||
.island(getIslands().getIsland(getWorld(), prospectiveOwnerUUID))
|
||||
.reason(TeamEvent.Reason.JOINED)
|
||||
.involvedPlayer(playerUUID)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(e);
|
||||
TeamEvent.builder()
|
||||
.island(getIslands().getIsland(getWorld(), prospectiveOwnerUUID))
|
||||
.reason(TeamEvent.Reason.JOINED)
|
||||
.involvedPlayer(playerUUID)
|
||||
.build();
|
||||
}
|
||||
|
||||
private void cleanPlayer(User user) {
|
||||
|
|
|
@ -6,12 +6,10 @@ import java.util.Optional;
|
|||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.island.team.Invite.Type;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -120,13 +118,12 @@ public class IslandTeamInviteCommand extends CompositeCommand {
|
|||
user.sendMessage("commands.island.team.invite.removing-invite");
|
||||
}
|
||||
// Fire event so add-ons can run commands, etc.
|
||||
IslandBaseEvent event = TeamEvent.builder()
|
||||
if (TeamEvent.builder()
|
||||
.island(getIslands().getIsland(getWorld(), user.getUniqueId()))
|
||||
.reason(TeamEvent.Reason.INVITE)
|
||||
.involvedPlayer(invitedPlayer.getUniqueId())
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (event.isCancelled()) {
|
||||
.build()
|
||||
.isCancelled()) {
|
||||
return false;
|
||||
}
|
||||
// Put the invited player (key) onto the list with inviter (value)
|
||||
|
|
|
@ -3,10 +3,7 @@ package world.bentobox.bentobox.api.commands.island.team;
|
|||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -33,14 +30,13 @@ public class IslandTeamInviteRejectCommand extends CompositeCommand {
|
|||
// Reject /island reject
|
||||
if (itc.isInvited(playerUUID)) {
|
||||
// Fire event so add-ons can run commands, etc.
|
||||
IslandBaseEvent event = TeamEvent.builder()
|
||||
if (TeamEvent.builder()
|
||||
.island(getIslands()
|
||||
.getIsland(getWorld(), itc.getInviter(playerUUID)))
|
||||
.reason(TeamEvent.Reason.REJECT)
|
||||
.involvedPlayer(playerUUID)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (event.isCancelled()) {
|
||||
.build()
|
||||
.isCancelled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@ import org.bukkit.Bukkit;
|
|||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -126,12 +125,11 @@ public class IslandTeamKickCommand extends ConfirmableCommand {
|
|||
}
|
||||
user.sendMessage("commands.island.team.kick.success", TextVariables.NAME, target.getName());
|
||||
// Fire event
|
||||
IslandBaseEvent e = TeamEvent.builder()
|
||||
.island(oldIsland)
|
||||
.reason(TeamEvent.Reason.KICK)
|
||||
.involvedPlayer(targetUUID)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(e);
|
||||
TeamEvent.builder()
|
||||
.island(oldIsland)
|
||||
.reason(TeamEvent.Reason.KICK)
|
||||
.involvedPlayer(targetUUID)
|
||||
.build();
|
||||
|
||||
// Add cooldown for this player and target
|
||||
if (getSettings().getInviteCooldown() > 0 && getParent() != null) {
|
||||
|
|
|
@ -7,7 +7,6 @@ import org.bukkit.Bukkit;
|
|||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -121,11 +120,10 @@ public class IslandTeamLeaveCommand extends ConfirmableCommand {
|
|||
}
|
||||
user.sendMessage("commands.island.team.leave.success");
|
||||
// Fire event
|
||||
IslandBaseEvent e = TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.LEAVE)
|
||||
.involvedPlayer(user.getUniqueId())
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(e);
|
||||
TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.LEAVE)
|
||||
.involvedPlayer(user.getUniqueId())
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -8,7 +8,6 @@ import java.util.UUID;
|
|||
import org.bukkit.Bukkit;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.team.TeamEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
@ -63,13 +62,12 @@ public class IslandTeamSetownerCommand extends CompositeCommand {
|
|||
}
|
||||
// Fire event so add-ons can run commands, etc.
|
||||
Island island = getIslands().getIsland(getWorld(), playerUUID);
|
||||
IslandBaseEvent event = TeamEvent.builder()
|
||||
if (TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.SETOWNER)
|
||||
.involvedPlayer(targetUUID)
|
||||
.build();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (event.isCancelled()) {
|
||||
.build()
|
||||
.isCancelled()) {
|
||||
return false;
|
||||
}
|
||||
getIslands().setOwner(getWorld(), user, targetUUID);
|
||||
|
|
|
@ -3,6 +3,8 @@ package world.bentobox.bentobox.api.events.addon;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import world.bentobox.bentobox.api.addons.Addon;
|
||||
|
||||
public class AddonEvent {
|
||||
|
@ -16,36 +18,36 @@ public class AddonEvent {
|
|||
}
|
||||
|
||||
|
||||
public AddonEventBuilder builder() {
|
||||
public static AddonEventBuilder builder() {
|
||||
return new AddonEventBuilder();
|
||||
}
|
||||
|
||||
public class AddonEnableEvent extends AddonBaseEvent {
|
||||
public static class AddonEnableEvent extends AddonBaseEvent {
|
||||
private AddonEnableEvent(Addon addon, Map<String, Object> keyValues) {
|
||||
// Final variables have to be declared in the constructor
|
||||
super(addon, keyValues);
|
||||
}
|
||||
}
|
||||
public class AddonDisableEvent extends AddonBaseEvent {
|
||||
public static class AddonDisableEvent extends AddonBaseEvent {
|
||||
private AddonDisableEvent(Addon addon, Map<String, Object> keyValues) {
|
||||
// Final variables have to be declared in the constructor
|
||||
super(addon, keyValues);
|
||||
}
|
||||
}
|
||||
public class AddonLoadEvent extends AddonBaseEvent {
|
||||
public static class AddonLoadEvent extends AddonBaseEvent {
|
||||
private AddonLoadEvent(Addon addon, Map<String, Object> keyValues) {
|
||||
// Final variables have to be declared in the constructor
|
||||
super(addon, keyValues);
|
||||
}
|
||||
}
|
||||
public class AddonGeneralEvent extends AddonBaseEvent {
|
||||
public static class AddonGeneralEvent extends AddonBaseEvent {
|
||||
private AddonGeneralEvent(Addon addon, Map<String, Object> keyValues) {
|
||||
// Final variables have to be declared in the constructor
|
||||
super(addon, keyValues);
|
||||
}
|
||||
}
|
||||
|
||||
public class AddonEventBuilder {
|
||||
public static class AddonEventBuilder {
|
||||
// Here field are NOT final. They are just used for the building.
|
||||
private Addon addon;
|
||||
private Reason reason = Reason.UNKNOWN;
|
||||
|
@ -71,7 +73,7 @@ public class AddonEvent {
|
|||
return this;
|
||||
}
|
||||
|
||||
public AddonBaseEvent build() {
|
||||
private AddonBaseEvent getEvent() {
|
||||
switch (reason) {
|
||||
case ENABLE:
|
||||
return new AddonEnableEvent(addon, keyValues);
|
||||
|
@ -83,5 +85,15 @@ public class AddonEvent {
|
|||
return new AddonGeneralEvent(addon, keyValues);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build and fire event
|
||||
* @return event
|
||||
*/
|
||||
public AddonBaseEvent build() {
|
||||
AddonBaseEvent e = getEvent();
|
||||
Bukkit.getPluginManager().callEvent(e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.events.team;
|
|||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
|
@ -179,7 +180,7 @@ public class TeamEvent {
|
|||
return this;
|
||||
}
|
||||
|
||||
public IslandBaseEvent build() {
|
||||
private IslandBaseEvent getEvent() {
|
||||
switch (reason) {
|
||||
case JOIN:
|
||||
return new TeamJoinEvent(island, player, admin, location);
|
||||
|
@ -205,5 +206,15 @@ public class TeamEvent {
|
|||
return new TeamGeneralEvent(island, player, admin, location);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the event and call it
|
||||
* @return event
|
||||
*/
|
||||
public IslandBaseEvent build() {
|
||||
IslandBaseEvent e = getEvent();
|
||||
Bukkit.getPluginManager().callEvent(e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -108,7 +108,7 @@ public class AddonsManager {
|
|||
plugin.getLocalesManager().loadLocalesFromFile(addon.getDescription().getName());
|
||||
|
||||
// Fire the load event
|
||||
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.LOAD).build());
|
||||
AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build();
|
||||
|
||||
// Add it to the list of addons
|
||||
addons.remove(addon);
|
||||
|
@ -176,7 +176,7 @@ public class AddonsManager {
|
|||
gameMode.getPlayerCommand().ifPresent(c -> c.setWorld(gameMode.getOverWorld()));
|
||||
gameMode.getAdminCommand().ifPresent(c -> c.setWorld(gameMode.getOverWorld()));
|
||||
}
|
||||
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build());
|
||||
AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build();
|
||||
addon.setState(Addon.State.ENABLED);
|
||||
plugin.log("Enabling " + addon.getDescription().getName() + "...");
|
||||
} catch (NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError e) {
|
||||
|
@ -429,7 +429,7 @@ public class AddonsManager {
|
|||
addon.getDescription().getAuthors().forEach(plugin::logError);
|
||||
plugin.logStacktrace(e);
|
||||
}
|
||||
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build());
|
||||
AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build();
|
||||
}
|
||||
// Clear loaders
|
||||
if (loaders.containsKey(addon)) {
|
||||
|
|
|
@ -20,11 +20,12 @@ import org.bukkit.Bukkit;
|
|||
import org.bukkit.entity.Player;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
@ -36,14 +37,14 @@ import world.bentobox.bentobox.database.objects.Island;
|
|||
import world.bentobox.bentobox.managers.IslandsManager;
|
||||
import world.bentobox.bentobox.managers.PlayersManager;
|
||||
import world.bentobox.bentobox.managers.RanksManager;
|
||||
import world.bentobox.bentobox.util.Util;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@Ignore("Sorry, I don't have the time to fix the tests right now.")
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
|
||||
@PrepareForTest({Bukkit.class, BentoBox.class, Util.class})
|
||||
public class AdminSetrankCommandTest {
|
||||
|
||||
@Mock
|
||||
|
@ -55,7 +56,6 @@ public class AdminSetrankCommandTest {
|
|||
@Mock
|
||||
private PlayersManager pm;
|
||||
|
||||
@Mock
|
||||
private RanksManager rm;
|
||||
private AdminSetrankCommand c;
|
||||
|
||||
|
@ -71,6 +71,7 @@ public class AdminSetrankCommandTest {
|
|||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
|
||||
// Ranks Manager
|
||||
rm = new RanksManager();
|
||||
when(plugin.getRanksManager()).thenReturn(rm);
|
||||
|
||||
// Players Manager
|
||||
|
@ -84,7 +85,14 @@ public class AdminSetrankCommandTest {
|
|||
Player p = mock(Player.class);
|
||||
when(p.getUniqueId()).thenReturn(targetUUID);
|
||||
User.getInstance(p);
|
||||
|
||||
|
||||
// Online players
|
||||
PowerMockito.mockStatic(Util.class);
|
||||
when(Util.getOnlinePlayerList(any())).thenReturn(Collections.singletonList("tastybento"));
|
||||
|
||||
// Translations
|
||||
when(user.getTranslation(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
|
||||
|
||||
// Command
|
||||
c = new AdminSetrankCommand(ac);
|
||||
}
|
||||
|
@ -153,7 +161,7 @@ public class AdminSetrankCommandTest {
|
|||
@Test
|
||||
public void testCanExecuteKnownPlayerNoIsland() {
|
||||
when(pm.getUUID(any())).thenReturn(targetUUID);
|
||||
assertFalse(c.canExecute(user, "", Arrays.asList("tastybento", "member")));
|
||||
assertFalse(c.canExecute(user, "", Arrays.asList("tastybento", "ranks.member")));
|
||||
verify(user).sendMessage("general.errors.player-has-no-island");
|
||||
}
|
||||
|
||||
|
@ -175,9 +183,7 @@ public class AdminSetrankCommandTest {
|
|||
public void testCanExecuteKnownPlayerHasIslandTooLowRank() {
|
||||
when(pm.getUUID(any())).thenReturn(targetUUID);
|
||||
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
|
||||
when(rm.getRanks()).thenReturn(Collections.singletonMap("visitor", 0));
|
||||
when(user.getTranslation(anyString())).thenReturn("visitor");
|
||||
assertFalse(c.canExecute(user, "", Arrays.asList("tastybento", "visitor")));
|
||||
assertFalse(c.canExecute(user, "", Arrays.asList("tastybento", "ranks.visitor")));
|
||||
verify(user).sendMessage("commands.admin.setrank.not-possible");
|
||||
}
|
||||
|
||||
|
@ -188,9 +194,7 @@ public class AdminSetrankCommandTest {
|
|||
public void testCanExecuteKnownPlayerHasIslandSuccess() {
|
||||
when(pm.getUUID(any())).thenReturn(targetUUID);
|
||||
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
|
||||
when(rm.getRanks()).thenReturn(Collections.singletonMap("member", 500));
|
||||
when(user.getTranslation(anyString())).thenReturn("member");
|
||||
assertTrue(c.canExecute(user, "", Arrays.asList("tastybento", "member")));
|
||||
assertTrue(c.canExecute(user, "", Arrays.asList("tastybento", "ranks.member")));
|
||||
}
|
||||
|
||||
|
||||
|
@ -204,28 +208,26 @@ public class AdminSetrankCommandTest {
|
|||
Island island = mock(Island.class);
|
||||
when(island.getRank(any())).thenReturn(RanksManager.SUB_OWNER_RANK);
|
||||
when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
|
||||
when(user.getTranslation(any())).thenReturn("sub-owner", "member");
|
||||
assertTrue(c.execute(user, "", Arrays.asList("tastybento", "member")));
|
||||
verify(user).sendMessage(eq("commands.admin.setrank.rank-set"),
|
||||
eq("[from]"),
|
||||
eq("sub-owner"),
|
||||
eq("ranks.sub-owner"),
|
||||
eq("[to]"),
|
||||
eq("member"));
|
||||
eq("ranks.member"),
|
||||
eq("[name]"),
|
||||
eq(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSetrankCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Ignore("NPE on Bukkit method")
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfString() {
|
||||
when(rm.getRanks()).thenReturn(Collections.singletonMap("owner", 0));
|
||||
when(user.getTranslation(any())).thenReturn("owner");
|
||||
Optional<List<String>> result = c.tabComplete(user, "", Arrays.asList("setrank", ""));
|
||||
assertTrue(result.isPresent());
|
||||
result.ifPresent(list -> {
|
||||
assertTrue(list.size() == 1);
|
||||
assertEquals("owner", list.get(0));
|
||||
assertEquals("tastybento", list.get(0));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -286,7 +286,6 @@ public class IslandTeamInviteAcceptCommandTest {
|
|||
when(teb.build()).thenReturn(ibe);
|
||||
when(TeamEvent.builder()).thenReturn(teb);
|
||||
assertFalse(c.canExecute(user, "accept", Collections.emptyList()));
|
||||
verify(pim).callEvent(any());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue