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.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -46,6 +47,7 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
this.aliasesMap = new HashMap<>(1); this.aliasesMap = new HashMap<>(1);
this.label = label; this.label = label;
this.help = help; this.help = help;
this.teamMembers = new HashSet<UUID>(1);
// Register the help argument if needed // Register the help argument if needed
if (help) { if (help) {
@ -209,12 +211,20 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
isPlayer = true; isPlayer = true;
player = (Player)sender; player = (Player)sender;
playerUUID = player.getUniqueId(); 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 // Check if the player is in a team or not and if so, grab the team leader's UUID
if (plugin.getPlayers().inTeam(playerUUID)) { if (plugin.getPlayers().inTeam(playerUUID)) {
inTeam = true; inTeam = true;
teamLeaderUUID = plugin.getIslands().getTeamLeader(playerUUID); teamLeaderUUID = plugin.getIslands().getTeamLeader(playerUUID);
teamMembers = plugin.getIslands().getMembers(teamLeaderUUID); 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.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.math.NumberUtils;
@ -13,6 +15,7 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.scheduler.BukkitRunnable;
import com.google.common.collect.BiMap; import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
@ -33,7 +36,7 @@ import us.tastybento.bskyblock.util.VaultHelper;
* @author Poslovitch * @author Poslovitch
*/ */
public class IslandCommand extends AbstractCommand { public class IslandCommand extends AbstractCommand {
protected static final boolean DEBUG = true; protected static final boolean DEBUG = false;
private BSkyBlock plugin; private BSkyBlock plugin;
/** /**
* Invite list - invited player name string (key), inviter name string * 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(); private final BiMap<UUID, UUID> inviteList = HashBiMap.create();
// The time a player has to wait until they can reset their island again // The time a player has to wait until they can reset their island again
private HashMap<UUID, Long> resetWaitTime = new HashMap<UUID, Long>(); private HashMap<UUID, Long> resetWaitTime = new HashMap<UUID, Long>();
protected Set<UUID> leavingPlayers = new HashSet<UUID>();
public IslandCommand(BSkyBlock plugin) { public IslandCommand(BSkyBlock plugin) {
super(plugin, Settings.ISLANDCOMMAND, true); super(plugin, Settings.ISLANDCOMMAND, true);
@ -69,7 +73,7 @@ public class IslandCommand extends AbstractCommand {
public void execute(CommandSender sender, String[] args) { public void execute(CommandSender sender, String[] args) {
if (sender instanceof Player) { if (sender instanceof Player) {
Player player = (Player)sender; Player player = (Player)sender;
if (plugin.getIslands().hasIsland(player.getUniqueId())) { if (plugin.getPlayers().inTeam(playerUUID) || plugin.getIslands().hasIsland(playerUUID)) {
// Has island // Has island
plugin.getIslands().homeTeleport(player); plugin.getIslands().homeTeleport(player);
} else { } else {
@ -575,8 +579,10 @@ public class IslandCommand extends AbstractCommand {
if (isPlayer) { if (isPlayer) {
if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) { if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) {
return new CanUseResp(true); 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); return new CanUseResp(false);
} }
@ -775,7 +781,7 @@ public class IslandCommand extends AbstractCommand {
@Override @Override
public CanUseResp canUse(CommandSender sender) { public CanUseResp canUse(CommandSender sender) {
if (isPlayer) { 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(true);
} }
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"));
@ -785,7 +791,71 @@ public class IslandCommand extends AbstractCommand {
@Override @Override
public void execute(CommandSender sender, String[] args) { 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 @Override
public CanUseResp canUse(CommandSender sender) { public CanUseResp canUse(CommandSender sender) {
if (isPlayer) { 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(true);
} }
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"));
@ -850,7 +920,8 @@ public class IslandCommand extends AbstractCommand {
@Override @Override
public void execute(CommandSender sender, String[] args) { public void execute(CommandSender sender, String[] args) {
if (!isPlayer) { if (!isPlayer) {
Util.sendMessage(player, ChatColor.RED + plugin.getLocale(player).get("general.errors.use-in-game"));
return;
} }
// Check if player has been invited // Check if player has been invited
if (!inviteList.containsKey(playerUUID)) { if (!inviteList.containsKey(playerUUID)) {

View File

@ -124,7 +124,10 @@ public class PluginConfig {
Settings.defaultWorldSettings.put(SettingsFlag.ANIMAL_SPAWN, true); Settings.defaultWorldSettings.put(SettingsFlag.ANIMAL_SPAWN, true);
Settings.defaultWorldSettings.put(SettingsFlag.MONSTER_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 //TODO end loading

View File

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

View File

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

View File

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