Add inventory check and confirmation for island teleportation.

- Introduced inventory validation to prevent accidental item loss.
- Added `/skyblock confirm` command for explicit teleport confirmation.
This commit is contained in:
Nick Guy 2025-08-11 17:53:07 +01:00
parent e67f70af56
commit 6332a26ce0

View file

@ -13,7 +13,6 @@ import com.ncguy.usefulskyblock.IslandNetworkGenerator;
import com.ncguy.usefulskyblock.Reference;
import com.ncguy.usefulskyblock.StructureRef;
import com.ncguy.usefulskyblock.data.BiomedStructure;
import com.ncguy.usefulskyblock.utils.MathsUtils;
import com.ncguy.usefulskyblock.utils.TextUtils;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
@ -30,6 +29,7 @@ import org.bukkit.block.structure.StructureRotation;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
import org.bukkit.scoreboard.Team;
@ -37,6 +37,7 @@ import org.bukkit.util.BlockVector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@ -217,22 +218,26 @@ public class SkyblockGenCommand extends AbstractSkyblockCommand {
return gen.generateIslandNetwork(origin, def);
}
private int executeRoot(CommandContext<CommandSourceStack> ctx) {
private int executeRoot(CommandContext<CommandSourceStack> ctx, boolean confirmationGiven) {
Entity executor = ctx.getSource().getExecutor();
if (!(executor instanceof Player)) return 0;
Player player = (Player) executor;
var islandHomeLoc = Reference.ISLAND_HOME_LOC.assign(player);
World overworld = getServer().getWorld(new NamespacedKey("minecraft", "overworld"));
if (islandHomeLoc.has()) {
if (!isInventoryEmpty(confirmationGiven, executor, player))
return -1;
BlockVector blockVector = islandHomeLoc.get();
Location loc = new Location(overworld, blockVector.getX(), blockVector.getY(), blockVector.getZ());
if (!player.teleport(loc, PlayerTeleportEvent.TeleportCause.COMMAND)) {
player.sendMessage("Failed to teleport you to your island home, please notify an administrator");
return -1;
}
player.getInventory().clear();
return 0;
}
@ -250,6 +255,10 @@ public class SkyblockGenCommand extends AbstractSkyblockCommand {
String sanitizedTeamName = playerTeamName.replaceAll("[^a-z0-9_.]", "_");
var teamSpawn = Reference.SKYBLOCK_TEAM_ROOT.withSuffix("." + sanitizedTeamName).assign(overworld);
if (teamSpawn.has()) {
if (!isInventoryEmpty(confirmationGiven, executor, player))
return -1;
islandHomeLoc.set(teamSpawn.get());
executor.sendMessage("Teleported to island home..");
BlockVector blockVector = islandHomeLoc.get();
@ -258,6 +267,7 @@ public class SkyblockGenCommand extends AbstractSkyblockCommand {
player.sendMessage("Failed to teleport you to your island home, please notify an administrator");
return -1;
}
player.getInventory().clear();
return 0;
}
@ -268,6 +278,22 @@ public class SkyblockGenCommand extends AbstractSkyblockCommand {
return 0;
}
private boolean isInventoryEmpty(boolean confirmationGiven, Entity executor, Player player) {
PlayerInventory inventory = player.getInventory();
boolean hasArmour = Arrays.stream(inventory.getArmorContents()).anyMatch(Objects::nonNull);
boolean hasItemInOffhand = inventory.getItemInOffHand().getType() != Material.AIR;
boolean hasItemInInventory = Arrays.stream(inventory.getContents()).anyMatch(Objects::nonNull);
if(hasArmour || hasItemInOffhand || hasItemInInventory) {
if(!confirmationGiven) {
executor.sendMessage(Component.text("You have items in your inventory, teleporting you to your island will remove them."));
executor.sendMessage(Component.text("If you wish to keep your items, please confirm via the command ").append(Component.text("/skyblock confirm", Style.style(TextColor.color(0x00, 0xff, 0xff)))));
return false;
}
}
return true;
}
public static LiteralCommandNode<CommandSourceStack> create() {
var root = Commands.literal("skyblock");
@ -295,11 +321,20 @@ public class SkyblockGenCommand extends AbstractSkyblockCommand {
.executes(cmd::executeTeamRemove)));
root.then(team);
root.executes(cmd::executeRoot);
root.executes(cmd::executeRootNoConfirm);
root.then(Commands.literal("confirm").executes(cmd::executeRootConfirm));
return root.build();
}
private int executeRootConfirm(CommandContext<CommandSourceStack> ctx) {
return executeRoot(ctx, true);
}
private int executeRootNoConfirm(CommandContext<CommandSourceStack> ctx) {
return executeRoot(ctx, false);
}
private int executeGetProgress(CommandContext<CommandSourceStack> ctx) {
Entity executor = ctx.getSource().getExecutor();
if (!(executor instanceof Player player)) return -1;