From 2c2cd774d3c7bbf7ae7e1c7030da1a5895730e07 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 5 Aug 2017 16:09:58 -0700 Subject: [PATCH] Added team invite command. --- locales/bsb_en_US.yml | 2 + .../bskyblock/commands/IslandCommand.java | 133 +++++++++++++++--- .../tastybento/bskyblock/config/Settings.java | 1 + .../database/managers/IslandsManager.java | 47 +++++-- .../database/managers/PlayersManager.java | 15 +- .../bskyblock/database/objects/Players.java | 18 ++- .../bskyblock/util/SafeSpotTeleport.java | 8 ++ 7 files changed, 184 insertions(+), 40 deletions(-) diff --git a/locales/bsb_en_US.yml b/locales/bsb_en_US.yml index bcb4e6d88..cdfe01d61 100755 --- a/locales/bsb_en_US.yml +++ b/locales/bsb_en_US.yml @@ -289,7 +289,9 @@ expel: invite: errorCantJoinIsland: "You couldn't join the island, maybe it's full." errorCoolDown: "You can invite that player again in [time] minutes" + errorNoOneInvitedYou: "No one invited you" errorThatPlayerIsAlreadyInATeam: "That player is already in a team." + errorYouAreAlreadyOnATeam: "You are already on a team!" errorYouCannotInviteYourself: "You can not invite yourself!" errorYouMustHaveIslandToInvite: "You must have an island in order to invite people to it!" errorYourIslandIsFull: "Your island is full, you can't invite anyone else." diff --git a/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java b/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java index 45508b3ab..6b00b5304 100755 --- a/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java @@ -1,11 +1,14 @@ package us.tastybento.bskyblock.commands; +import java.util.Calendar; +import java.util.HashMap; import java.util.List; import java.util.UUID; import org.apache.commons.lang.math.NumberUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.GameMode; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -16,6 +19,7 @@ import com.google.common.collect.HashBiMap; import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.commands.AbstractCommand; +import us.tastybento.bskyblock.api.events.team.PlayerAcceptInviteEvent; import us.tastybento.bskyblock.config.Settings; import us.tastybento.bskyblock.database.objects.Island; import us.tastybento.bskyblock.schematics.Schematic; @@ -29,13 +33,15 @@ import us.tastybento.bskyblock.util.VaultHelper; * @author Poslovitch */ public class IslandCommand extends AbstractCommand { + protected static final boolean DEBUG = true; private BSkyBlock plugin; /** * Invite list - invited player name string (key), inviter name string * (value) */ private final BiMap inviteList = HashBiMap.create(); - + // The time a player has to wait until they can reset their island again + private HashMap resetWaitTime = new HashMap(); public IslandCommand(BSkyBlock plugin) { super(plugin, Settings.ISLANDCOMMAND, true); @@ -294,11 +300,14 @@ public class IslandCommand extends AbstractCommand { if (plugin.getIslands().hasIsland(player.getUniqueId())) { // Get the player's old island Island oldIsland = plugin.getIslands().getIsland(player.getUniqueId()); + if (DEBUG) plugin.getLogger().info("DEBUG: old island is at " + oldIsland.getCenter().getBlockX() + "," + oldIsland.getCenter().getBlockZ()); // Remove them from this island (it still exists and will be deleted later) plugin.getIslands().removePlayer(player.getUniqueId()); + if (DEBUG) plugin.getLogger().info("DEBUG: old island's owner is " + oldIsland.getOwner()); // Create new island and then delete the old one + if (DEBUG) plugin.getLogger().info("DEBUG: making new island "); Schematic schematic = plugin.getSchematics().getSchematic("default"); plugin.getIslands().newIsland(player, schematic, oldIsland); @@ -499,6 +508,7 @@ public class IslandCommand extends AbstractCommand { @Override public void execute(CommandSender sender, String[] args) { + if (DEBUG) plugin.getLogger().info("DEBUG: executing team command"); if (inTeam) { if (teamLeaderUUID.equals(playerUUID)) { @@ -550,6 +560,7 @@ public class IslandCommand extends AbstractCommand { @Override public String[] usage(CommandSender sender){ + if (DEBUG) plugin.getLogger().info("DEBUG: executing team help"); return new String[] {null, plugin.getLocale(sender).get("help.island.team")}; @@ -732,18 +743,18 @@ public class IslandCommand extends AbstractCommand { @Override public void execute(CommandSender sender, String[] args) { - // Invite label with no name, i.e., /island invite - tells the player who has invited them so far - if (inviteList.inverse().containsKey(playerUUID)) { - Player invitee = plugin.getServer().getPlayer(inviteList.inverse().get(playerUUID)); - if (invitee != null) { - inviteList.inverse().remove(playerUUID); - Util.sendMessage(invitee, ChatColor.RED + plugin.getLocale(invitee.getUniqueId()).get("invite.nameHasUninvitedYou").replace("[name]", player.getName())); - Util.sendMessage(player, ChatColor.GREEN + plugin.getLocale(playerUUID).get("general.success")); - } - } else { - Util.sendMessage(player, ChatColor.RED + plugin.getLocale(sender).get("island.help.Invite")); + // Invite label with no name, i.e., /island invite - tells the player who has invited them so far + if (inviteList.inverse().containsKey(playerUUID)) { + Player invitee = plugin.getServer().getPlayer(inviteList.inverse().get(playerUUID)); + if (invitee != null) { + inviteList.inverse().remove(playerUUID); + Util.sendMessage(invitee, ChatColor.RED + plugin.getLocale(invitee.getUniqueId()).get("invite.nameHasUninvitedYou").replace("[name]", player.getName())); + Util.sendMessage(player, ChatColor.GREEN + plugin.getLocale(playerUUID).get("general.success")); } - return; + } else { + Util.sendMessage(player, ChatColor.RED + plugin.getLocale(sender).get("island.help.Invite")); + } + return; } @Override @@ -763,7 +774,12 @@ public class IslandCommand extends AbstractCommand { @Override public CanUseResp canUse(CommandSender sender) { - + if (isPlayer) { + if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) { + return new CanUseResp(true); + } + return new CanUseResp(ChatColor.RED + plugin.getLocale(player).get("general.errors.no-permission")); + } return new CanUseResp(false); } @@ -790,7 +806,12 @@ public class IslandCommand extends AbstractCommand { @Override public CanUseResp canUse(CommandSender sender) { - + if (isPlayer) { + if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) { + return new CanUseResp(true); + } + return new CanUseResp(ChatColor.RED + plugin.getLocale(player).get("general.errors.no-permission")); + } return new CanUseResp(false); } @@ -817,14 +838,70 @@ public class IslandCommand extends AbstractCommand { @Override public CanUseResp canUse(CommandSender sender) { - + if (isPlayer) { + if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) { + return new CanUseResp(true); + } + return new CanUseResp(ChatColor.RED + plugin.getLocale(player).get("general.errors.no-permission")); + } return new CanUseResp(false); } @Override public void execute(CommandSender sender, String[] args) { + if (!isPlayer) { - + } + // Check if player has been invited + if (!inviteList.containsKey(playerUUID)) { + Util.sendMessage(player, ChatColor.RED + plugin.getLocale(player).get("invite.errorNoOneInvitedYou")); + return; + } + // Check if player is already in a team + if (plugin.getPlayers().inTeam(playerUUID)) { + Util.sendMessage(player, ChatColor.RED + plugin.getLocale(player).get("invite.errorYouAreAlreadyOnATeam")); + return; + } + player.setGameMode(GameMode.SPECTATOR); + // If the invitee has an island of their own + if (plugin.getPlayers().hasIsland(playerUUID)) { + plugin.getLogger().info(player.getName() + "'s island will be deleted because they joined a party."); + plugin.getIslands().deletePlayerIsland(playerUUID, true); + plugin.getLogger().info("Island deleted."); + } + // Set the team leader + teamLeaderUUID = inviteList.get(playerUUID); + // TODO implement this + plugin.getPlayers().resetPlayer(player); + // Add the player to the team + if (DEBUG) + plugin.getLogger().info("DEBUG: adding " + playerUUID + " to team " + teamLeaderUUID); + plugin.getIslands().setJoinTeam(playerUUID, inviteList.get(playerUUID)); + setResetWaitTime(player); + if (Settings.teamJoinDeathReset) { + plugin.getPlayers().setDeaths(player.getUniqueId(), 0); + } + if (DEBUG) + plugin.getLogger().info("DEBUG: team leader's home is " + plugin.getPlayers().getHomeLocation(teamLeaderUUID)); + // Set the player's home + plugin.getPlayers().setHomeLocation(playerUUID, plugin.getPlayers().getHomeLocation(teamLeaderUUID)); + if (DEBUG) + plugin.getLogger().info("DEBUG: teleporting player to new island"); + plugin.getIslands().homeTeleport(player); + + // Fire event so add-ons can run commands, etc. + plugin.getServer().getPluginManager().callEvent(new PlayerAcceptInviteEvent(player)); + Util.sendMessage(player, ChatColor.GREEN + plugin.getLocale(player.getUniqueId()).get("invite.youHaveJoinedAnIsland")); + + if (DEBUG) + plugin.getLogger().info("DEBUG: Removing player from invite list"); + if (plugin.getServer().getPlayer(inviteList.get(playerUUID)) != null) { + Util.sendMessage(plugin.getServer().getPlayer(inviteList.get(playerUUID)), + ChatColor.GREEN + plugin.getLocale(player.getUniqueId()).get("invite.hasJoinedYourIsland").replace("[name]", player.getName())); + } + // Remove the invite + inviteList.remove(player.getUniqueId()); + plugin.getIslands().save(true); } @Override @@ -844,7 +921,12 @@ public class IslandCommand extends AbstractCommand { @Override public CanUseResp canUse(CommandSender sender) { - + if (isPlayer) { + if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) { + return new CanUseResp(true); + } + return new CanUseResp(ChatColor.RED + plugin.getLocale(player).get("general.errors.no-permission")); + } return new CanUseResp(false); } @@ -871,7 +953,12 @@ public class IslandCommand extends AbstractCommand { @Override public CanUseResp canUse(CommandSender sender) { - + if (isPlayer) { + if (VaultHelper.hasPerm(player, Settings.PERMPREFIX + "team.create")) { + return new CanUseResp(true); + } + return new CanUseResp(ChatColor.RED + plugin.getLocale(player).get("general.errors.no-permission")); + } return new CanUseResp(false); } @@ -1344,6 +1431,16 @@ public class IslandCommand extends AbstractCommand { }); } + /** + * Sets a timeout for player into the Hashmap resetWaitTime + * + * @param player + */ + private void setResetWaitTime(final Player player) { + resetWaitTime.put(player.getUniqueId(), Long.valueOf(Calendar.getInstance().getTimeInMillis() + Settings.resetWait * 1000)); + } + + /** * Creates an island for player * @param player diff --git a/src/main/java/us/tastybento/bskyblock/config/Settings.java b/src/main/java/us/tastybento/bskyblock/config/Settings.java index 4108a90ee..1c1958dbd 100755 --- a/src/main/java/us/tastybento/bskyblock/config/Settings.java +++ b/src/main/java/us/tastybento/bskyblock/config/Settings.java @@ -223,5 +223,6 @@ public class Settings { public static HashMap limitedBlocks; public static boolean allowTNTPushing; public static boolean showInActionBar; + public static boolean teamJoinDeathReset; } diff --git a/src/main/java/us/tastybento/bskyblock/database/managers/IslandsManager.java b/src/main/java/us/tastybento/bskyblock/database/managers/IslandsManager.java index 93a76b1d4..73c420e21 100644 --- a/src/main/java/us/tastybento/bskyblock/database/managers/IslandsManager.java +++ b/src/main/java/us/tastybento/bskyblock/database/managers/IslandsManager.java @@ -9,6 +9,7 @@ import java.util.WeakHashMap; import org.apache.commons.lang.math.NumberUtils; import org.bukkit.ChatColor; +import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -43,7 +44,7 @@ import us.tastybento.bskyblock.util.Util; */ public class IslandsManager { - private static final boolean DEBUG = false; + private static final boolean DEBUG = true; private BSkyBlock plugin; private BSBDatabase database; @@ -324,7 +325,7 @@ public class IslandsManager { public void removePlayer(UUID playerUUID) { Island island = islandsByUUID.get(playerUUID); if (island != null) { - if (island.getOwner().equals(playerUUID)) { + if (island.getOwner() != null && island.getOwner().equals(playerUUID)) { // Clear ownership and members island.getMembers().clear(); island.setOwner(null); @@ -360,6 +361,7 @@ public class IslandsManager { } // Add player to new island teamIsland.addMember(playerUUID); + return true; } @@ -519,7 +521,8 @@ public class IslandsManager { if (DEBUG) plugin.getLogger().info("home teleport called for #" + number); home = getSafeHomeLocation(player.getUniqueId(), number); - //plugin.getLogger().info("home get safe loc = " + home); + if (DEBUG) + plugin.getLogger().info("home get safe loc = " + home); // Check if the player is a passenger in a boat if (player.isInsideVehicle()) { Entity boat = player.getVehicle(); @@ -548,6 +551,10 @@ public class IslandsManager { } else { Util.sendMessage(player, ChatColor.GREEN + "teleported to #" + number); } + // Exit spectator mode if in it + if (player.getGameMode().equals(GameMode.SPECTATOR)) { + player.setGameMode(GameMode.SURVIVAL); + } return true; } @@ -569,7 +576,8 @@ public class IslandsManager { l = plugin.getPlayers().getHomeLocation(playerUUID, number); } // Check if it is safe - //plugin.getLogger().info("DEBUG: Home location " + l); + if (DEBUG) + plugin.getLogger().info("DEBUG: Home location " + l); if (l != null) { // Homes are stored as integers and need correcting to be more central if (isSafeLocation(l)) { @@ -586,26 +594,38 @@ public class IslandsManager { } } } - - //plugin.getLogger().info("DEBUG: Home location either isn't safe, or does not exist so try the island"); + if (DEBUG) + plugin.getLogger().info("DEBUG: Home location either isn't safe, or does not exist so try the island"); // Home location either isn't safe, or does not exist so try the island // location if (plugin.getPlayers().inTeam(playerUUID)) { + if (DEBUG) + plugin.getLogger().info("DEBUG:player is in team"); l = plugin.getIslands().getIslandLocation(playerUUID); if (isSafeLocation(l)) { + if (DEBUG) + plugin.getLogger().info("DEBUG:island loc is safe"); plugin.getPlayers().setHomeLocation(playerUUID, l, number); return l.clone().add(new Vector(0.5D,0,0.5D)); } else { // try team leader's home + if (DEBUG) + plugin.getLogger().info("DEBUG: trying leader's home"); Location tlh = plugin.getPlayers().getHomeLocation(plugin.getIslands().getTeamLeader(playerUUID)); if (tlh != null) { + if (DEBUG) + plugin.getLogger().info("DEBUG: leader has a home"); if (isSafeLocation(tlh)) { + if (DEBUG) + plugin.getLogger().info("DEBUG: team leader's home is safe"); plugin.getPlayers().setHomeLocation(playerUUID, tlh, number); return tlh.clone().add(new Vector(0.5D,0,0.5D)); } } } } else { + if (DEBUG) + plugin.getLogger().info("DEBUG: player is not in team - trying island location"); l = plugin.getIslands().getIslandLocation(playerUUID); if (isSafeLocation(l)) { plugin.getPlayers().setHomeLocation(playerUUID, l, number); @@ -616,24 +636,28 @@ public class IslandsManager { plugin.getLogger().warning(plugin.getPlayers().getName(playerUUID) + " player has no island!"); return null; } - //plugin.getLogger().info("DEBUG: If these island locations are not safe, then we need to get creative"); + if (DEBUG) + plugin.getLogger().info("DEBUG: If these island locations are not safe, then we need to get creative"); // If these island locations are not safe, then we need to get creative // Try the default location - //plugin.getLogger().info("DEBUG: default"); + if (DEBUG) + plugin.getLogger().info("DEBUG: try default location"); Location dl = new Location(l.getWorld(), l.getX() + 0.5D, l.getY() + 5D, l.getZ() + 2.5D, 0F, 30F); if (isSafeLocation(dl)) { plugin.getPlayers().setHomeLocation(playerUUID, dl, number); return dl; } // Try just above the bedrock - //plugin.getLogger().info("DEBUG: above bedrock"); + if (DEBUG) + plugin.getLogger().info("DEBUG: above bedrock"); dl = new Location(l.getWorld(), l.getX() + 0.5D, l.getY() + 5D, l.getZ() + 0.5D, 0F, 30F); if (isSafeLocation(dl)) { plugin.getPlayers().setHomeLocation(playerUUID, dl, number); return dl; } // Try all the way up to the sky - //plugin.getLogger().info("DEBUG: try all the way to the sky"); + if (DEBUG) + plugin.getLogger().info("DEBUG: try all the way to the sky"); for (int y = l.getBlockY(); y < 255; y++) { final Location n = new Location(l.getWorld(), l.getX() + 0.5D, y, l.getZ() + 0.5D); if (isSafeLocation(n)) { @@ -641,7 +665,8 @@ public class IslandsManager { return n; } } - //plugin.getLogger().info("DEBUG: unsuccessful"); + if (DEBUG) + plugin.getLogger().info("DEBUG: unsuccessful"); // Unsuccessful return null; } diff --git a/src/main/java/us/tastybento/bskyblock/database/managers/PlayersManager.java b/src/main/java/us/tastybento/bskyblock/database/managers/PlayersManager.java index 1bedad6ff..6ada0cbc1 100644 --- a/src/main/java/us/tastybento/bskyblock/database/managers/PlayersManager.java +++ b/src/main/java/us/tastybento/bskyblock/database/managers/PlayersManager.java @@ -117,26 +117,31 @@ public class PlayersManager{ public Players addPlayer(final UUID playerUUID) { if (playerUUID == null) return null; - //plugin.getLogger().info("DEBUG: adding player " + playerUUID); + if (DEBUG) + plugin.getLogger().info("DEBUG: adding player " + playerUUID); if (!playerCache.containsKey(playerUUID)) { - //plugin.getLogger().info("DEBUG: player not in cache"); + if (DEBUG) + plugin.getLogger().info("DEBUG: player not in cache"); Players player = null; // If the player is in the database, load it, otherwise create a new player if (handler.objectExits(playerUUID.toString())) { - //plugin.getLogger().info("DEBUG: player in database"); + if (DEBUG) + plugin.getLogger().info("DEBUG: player in database"); try { player = handler.loadObject(playerUUID.toString()); } catch (Exception e) { e.printStackTrace(); } } else { - //plugin.getLogger().info("DEBUG: new player"); + if (DEBUG) + plugin.getLogger().info("DEBUG: new player"); player = new Players(playerUUID); } playerCache.put(playerUUID, player); return player; } else { - //plugin.getLogger().info("DEBUG: known player"); + if (DEBUG) + plugin.getLogger().info("DEBUG: known player"); return playerCache.get(playerUUID); } } diff --git a/src/main/java/us/tastybento/bskyblock/database/objects/Players.java b/src/main/java/us/tastybento/bskyblock/database/objects/Players.java index 6d601a2e2..c75caa056 100755 --- a/src/main/java/us/tastybento/bskyblock/database/objects/Players.java +++ b/src/main/java/us/tastybento/bskyblock/database/objects/Players.java @@ -30,7 +30,7 @@ public class Players extends DataObject { * This is required for database storage */ public Players() {} - + /** * @param uniqueId * Constructor - initializes the state variables @@ -61,11 +61,16 @@ public class Players extends DataObject { * @return Location of this home or null if not available */ public Location getHomeLocation(int number) { - if (homeLocations.containsKey(number)) { - return homeLocations.get(number); - } else { - return null; - } + /* + * Bukkit.getLogger().info("DEBUG: getting home location " + number); + + Bukkit.getLogger().info("DEBUG: " + homeLocations.toString()); + for (Entry en : homeLocations.entrySet()) { + Bukkit.getLogger().info("DEBUG: " + en.getKey() + " ==> " + en.getValue()); + if (number == en.getKey()) + Bukkit.getLogger().info("DEBUG: key = number"); + }*/ + return homeLocations.get(Integer.valueOf(number)); } /** @@ -107,6 +112,7 @@ public class Players extends DataObject { * @param homeLocations the homeLocations to set */ public void setHomeLocations(HashMap homeLocations) { + Bukkit.getLogger().info("DEBUG: " + homeLocations.toString()); this.homeLocations = homeLocations; } diff --git a/src/main/java/us/tastybento/bskyblock/util/SafeSpotTeleport.java b/src/main/java/us/tastybento/bskyblock/util/SafeSpotTeleport.java index 456feed70..f064d4c43 100644 --- a/src/main/java/us/tastybento/bskyblock/util/SafeSpotTeleport.java +++ b/src/main/java/us/tastybento/bskyblock/util/SafeSpotTeleport.java @@ -5,6 +5,7 @@ import java.util.List; import org.bukkit.ChatColor; import org.bukkit.ChunkSnapshot; +import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -216,6 +217,13 @@ public class SafeSpotTeleport { Vector velocity = entity.getVelocity(); entity.teleport(destination); entity.setVelocity(velocity); + // Exit spectator mode if in it + if (entity instanceof Player) { + Player player = (Player)entity; + if (player.getGameMode().equals(GameMode.SPECTATOR)) { + player.setGameMode(GameMode.SURVIVAL); + } + } }}); } else { // We did not find a spot