Completed island leave command.

Fixed bugs with teams.
This commit is contained in:
tastybento 2017-08-05 20:13:06 -07:00
parent d5f78e300b
commit 5d43a500a4
7 changed files with 143 additions and 31 deletions

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -46,6 +47,7 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
this.aliasesMap = new HashMap<>(1);
this.label = label;
this.help = help;
this.teamMembers = new HashSet<UUID>(1);
// Register the help argument if needed
if (help) {
@ -209,12 +211,20 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
isPlayer = true;
player = (Player)sender;
playerUUID = player.getUniqueId();
} else {
isPlayer = false;
player = null;
playerUUID = null;
}
// Check if the player is in a team or not and if so, grab the team leader's UUID
if (plugin.getPlayers().inTeam(playerUUID)) {
inTeam = true;
teamLeaderUUID = plugin.getIslands().getTeamLeader(playerUUID);
teamMembers = plugin.getIslands().getMembers(teamLeaderUUID);
} else {
inTeam = false;
teamLeaderUUID = null;
teamMembers.clear();
}
}

View File

@ -2,7 +2,9 @@ package us.tastybento.bskyblock.commands;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils;
@ -13,6 +15,7 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.scheduler.BukkitRunnable;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
@ -33,7 +36,7 @@ import us.tastybento.bskyblock.util.VaultHelper;
* @author Poslovitch
*/
public class IslandCommand extends AbstractCommand {
protected static final boolean DEBUG = true;
protected static final boolean DEBUG = false;
private BSkyBlock plugin;
/**
* Invite list - invited player name string (key), inviter name string
@ -42,6 +45,7 @@ public class IslandCommand extends AbstractCommand {
private final BiMap<UUID, UUID> inviteList = HashBiMap.create();
// The time a player has to wait until they can reset their island again
private HashMap<UUID, Long> resetWaitTime = new HashMap<UUID, Long>();
protected Set<UUID> leavingPlayers = new HashSet<UUID>();
public IslandCommand(BSkyBlock plugin) {
super(plugin, Settings.ISLANDCOMMAND, true);
@ -69,7 +73,7 @@ public class IslandCommand extends AbstractCommand {
public void execute(CommandSender sender, String[] args) {
if (sender instanceof Player) {
Player player = (Player)sender;
if (plugin.getIslands().hasIsland(player.getUniqueId())) {
if (plugin.getPlayers().inTeam(playerUUID) || plugin.getIslands().hasIsland(playerUUID)) {
// Has island
plugin.getIslands().homeTeleport(player);
} else {
@ -575,8 +579,10 @@ public class IslandCommand extends AbstractCommand {
if (isPlayer) {
if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) {
return new CanUseResp(true);
} else {
return new CanUseResp(ChatColor.RED + plugin.getLocale(player).get("general.errors.no-permission"));
}
return new CanUseResp(ChatColor.RED + plugin.getLocale(player).get("general.errors.no-permission"));
}
return new CanUseResp(false);
}
@ -775,7 +781,7 @@ public class IslandCommand extends AbstractCommand {
@Override
public CanUseResp canUse(CommandSender sender) {
if (isPlayer) {
if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) {
if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.join")) {
return new CanUseResp(true);
}
return new CanUseResp(ChatColor.RED + plugin.getLocale(player).get("general.errors.no-permission"));
@ -785,7 +791,71 @@ public class IslandCommand extends AbstractCommand {
@Override
public void execute(CommandSender sender, String[] args) {
if (!isPlayer) {
Util.sendMessage(player, ChatColor.RED + plugin.getLocale(player).get("general.errors.use-in-game"));
return;
}
if (Util.inWorld(player)) {
if (plugin.getPlayers().inTeam(playerUUID)) {
// Team leaders cannot leave
if (teamLeaderUUID != null && teamLeaderUUID.equals(playerUUID)) {
Util.sendMessage(player, ChatColor.YELLOW + plugin.getLocale(playerUUID).get("leave.errorYouAreTheLeader"));
return;
}
// Check for confirmation
if (Settings.leaveConfirmation && !leavingPlayers.contains(playerUUID)) {
leavingPlayers.add(playerUUID);
Util.sendMessage(player, ChatColor.RED + plugin.getLocale(playerUUID).get("leave.Warning"));
new BukkitRunnable() {
@Override
public void run() {
// If the player is still on the list, remove them and cancel the leave
if (leavingPlayers.contains(playerUUID)) {
leavingPlayers.remove(playerUUID);
Util.sendMessage(player, ChatColor.RED + plugin.getLocale(playerUUID).get("leave.Canceled"));
}
}
}.runTaskLater(plugin, Settings.leaveConfirmWait * 20L);
return;
}
// Remove from confirmation list
leavingPlayers.remove(playerUUID);
// Remove from team
if (!plugin.getIslands().setLeaveTeam(playerUUID)) {
//Util.sendMessage(player, ChatColor.RED + plugin.myLocale(player.getUniqueId()).leaveerrorYouCannotLeaveIsland);
// If this is canceled, fail silently
return;
}
// Log the location that this player left so they
// cannot join again before the cool down ends
plugin.getPlayers().startInviteCoolDownTimer(playerUUID, plugin.getIslands().getIslandLocation(teamLeaderUUID));
Util.sendMessage(player, ChatColor.YELLOW + plugin.getLocale(playerUUID).get("leave.youHaveLeftTheIsland"));
// Tell the leader if they are online
if (plugin.getServer().getPlayer(teamLeaderUUID) != null) {
Player leader = plugin.getServer().getPlayer(teamLeaderUUID);
Util.sendMessage(leader, ChatColor.RED + plugin.getLocale(teamLeaderUUID).get("leave.nameHasLeftYourIsland").replace("[name]", player.getName()));
} else {
// TODO: Leave them a message
//plugin.getMessages().setMessage(teamLeader, ChatColor.RED + plugin.myLocale(teamLeader).leavenameHasLeftYourIsland.replace("[name]", player.getName()));
}
// Clear all player variables and save
plugin.getPlayers().resetPlayer(player);
if (!player.performCommand(Settings.SPAWNCOMMAND)) {
player.teleport(player.getWorld().getSpawnLocation());
}
return;
} else {
Util.sendMessage(player, ChatColor.RED + plugin.getLocale(player.getUniqueId()).get("leave.errorYouCannotLeaveIsland"));
return;
}
} else {
Util.sendMessage(player, ChatColor.RED + plugin.getLocale(player.getUniqueId()).get("leave.errorYouMustBeInWorld"));
}
return;
}
@ -839,7 +909,7 @@ public class IslandCommand extends AbstractCommand {
@Override
public CanUseResp canUse(CommandSender sender) {
if (isPlayer) {
if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) {
if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.join")) {
return new CanUseResp(true);
}
return new CanUseResp(ChatColor.RED + plugin.getLocale(player).get("general.errors.no-permission"));
@ -850,7 +920,8 @@ public class IslandCommand extends AbstractCommand {
@Override
public void execute(CommandSender sender, String[] args) {
if (!isPlayer) {
Util.sendMessage(player, ChatColor.RED + plugin.getLocale(player).get("general.errors.use-in-game"));
return;
}
// Check if player has been invited
if (!inviteList.containsKey(playerUUID)) {

View File

@ -124,7 +124,10 @@ public class PluginConfig {
Settings.defaultWorldSettings.put(SettingsFlag.ANIMAL_SPAWN, true);
Settings.defaultWorldSettings.put(SettingsFlag.MONSTER_SPAWN, true);
// Entities
// Team
Settings.maxTeamSize = plugin.getConfig().getInt("island.max-team-size", 4);
Settings.leaveConfirmation = plugin.getConfig().getBoolean("require-confirmation.leave", true);
Settings.leaveConfirmWait = plugin.getConfig().getLong("require-confirmation.leave-wait", 10) * 20;
//TODO end loading

View File

@ -224,5 +224,7 @@ public class Settings {
public static boolean allowTNTPushing;
public static boolean showInActionBar;
public static boolean teamJoinDeathReset;
public static long leaveConfirmWait;
public static boolean leaveConfirmation;
}

View File

@ -1,11 +1,11 @@
package us.tastybento.bskyblock.database.managers;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import java.util.WeakHashMap;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.ChatColor;
@ -25,6 +25,7 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.util.Vector;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.events.island.IslandLeaveEvent;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.objects.Island;
@ -44,12 +45,12 @@ import us.tastybento.bskyblock.util.Util;
*/
public class IslandsManager {
private static final boolean DEBUG = true;
private static final boolean DEBUG = false;
private BSkyBlock plugin;
private BSBDatabase database;
private WeakHashMap<Location, Island> islandsByLocation;
private WeakHashMap<UUID, Island> islandsByUUID;
private HashMap<Location, Island> islandsByLocation;
private HashMap<UUID, Island> islandsByUUID;
// 2D islandGrid of islands, x,z
private TreeMap<Integer, TreeMap<Integer, Island>> islandGrid = new TreeMap<Integer, TreeMap<Integer, Island>>();
@ -69,8 +70,8 @@ public class IslandsManager {
database = BSBDatabase.getDatabase();
// Set up the database handler to store and retrieve Island classes
handler = (AbstractDatabaseHandler<Island>) database.getHandler(plugin, Island.class);
islandsByLocation = new WeakHashMap<Location, Island>();
islandsByUUID = new WeakHashMap<UUID, Island>();
islandsByLocation = new HashMap<Location, Island>();
islandsByUUID = new HashMap<UUID, Island>();
spawn = null;
}
@ -85,6 +86,9 @@ public class IslandsManager {
for (Island island : handler.loadObjects()) {
islandsByLocation.put(island.getCenter(), island);
islandsByUUID.put(island.getOwner(), island);
for (UUID member: island.getMembers()) {
islandsByUUID.put(member, island);
}
addToGrid(island);
}
} catch (Exception e) {
@ -323,15 +327,24 @@ public class IslandsManager {
* @param playerUUID
*/
public void removePlayer(UUID playerUUID) {
if (DEBUG)
plugin.getLogger().info("DEBUG: removing player");
Island island = islandsByUUID.get(playerUUID);
if (island != null) {
if (DEBUG)
plugin.getLogger().info("DEBUG: island found");
if (island.getOwner() != null && island.getOwner().equals(playerUUID)) {
if (DEBUG)
plugin.getLogger().info("DEBUG: player is the owner of this island");
// Clear ownership and members
island.getMembers().clear();
island.setOwner(null);
}
island.getMembers().remove(playerUUID);
}
if (DEBUG)
plugin.getLogger().info("DEBUG: removing reference to island by UUID");
islandsByUUID.remove(playerUUID);
}
/**
@ -354,14 +367,10 @@ public class IslandsManager {
}
// TODO: Fire a join team event. If canceled, return false
if (!setLeaveTeam(playerUUID)) {
// Player not allowed to leave team
return false;
}
// Add player to new island
teamIsland.addMember(playerUUID);
islandsByUUID.put(playerUUID, teamIsland);
return true;
}
@ -371,10 +380,29 @@ public class IslandsManager {
* @return true if successful, false if not
*/
public boolean setLeaveTeam(UUID playerUUID) {
if (DEBUG)
plugin.getLogger().info("DEBUG: leaving team");
// Try to remove player from old island
// TODO: Fire an event, if not cancelled, zero the player data
plugin.getPlayers().zeroPlayerData(playerUUID);
return true;
Island island = islandsByUUID.get(playerUUID);
if (island != null) {
if (DEBUG)
plugin.getLogger().info("DEBUG: island found");
IslandLeaveEvent leaveEvent = new IslandLeaveEvent(island, playerUUID);
plugin.getServer().getPluginManager().callEvent(leaveEvent);
if (leaveEvent.isCancelled()) {
if (DEBUG)
plugin.getLogger().info("DEBUG: leave event was canceled");
return false;
}
if (DEBUG)
plugin.getLogger().info("DEBUG: clearing player data");
plugin.getPlayers().clearPlayerHomes(playerUUID);
removePlayer(playerUUID);
return true;
}
return false;
}
/**
@ -387,7 +415,7 @@ public class IslandsManager {
Island island = islandsByUUID.get(playerUUID);
if (island != null)
return island.getMembers();
return null;
return new HashSet<UUID>(0);
}
/**

View File

@ -233,23 +233,17 @@ public class PlayersManager{
*/
public boolean inTeam(final UUID playerUUID) {
addPlayer(playerUUID);
Island island = plugin.getIslands().getIsland(playerUUID);
if (island == null) {
return false;
}
return island.getMembers().size() > 1 ? true: false;
return plugin.getIslands().getMembers(playerUUID).size() > 1 ? true: false;
}
/**
* Removes any island associated with this player and generally cleans up
* the player
* Clears player home locations
*
* @param playerUUID
*/
public void zeroPlayerData(UUID playerUUID) {
public void clearPlayerHomes(UUID playerUUID) {
Players player = addPlayer(playerUUID);
player.clearHomeLocations();
plugin.getIslands().removePlayer(playerUUID);
/*
* TODO
playerCache.get(playerUUID).save(); // Needed?

View File

@ -877,4 +877,8 @@ public class Island extends DataObject {
public Location getSpawnPoint() {
return spawnPoint;
}
public void removeMember(UUID playerUUID) {
this.members.remove(playerUUID);
}
}