Add set spawn command.

This commit is contained in:
benwoo1110 2020-12-19 19:35:40 +08:00
parent da6d6d64c2
commit 7b2a14c31f
4 changed files with 190 additions and 1 deletions

View File

@ -0,0 +1,88 @@
package com.onarandombox.MultiverseCore.commands_acf;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.BlockSafety;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class SetSpawnCommand extends MultiverseCommand {
public SetSpawnCommand(MultiverseCore plugin) {
super(plugin);
}
//TODO: For some reason this doesnt work with coord args.
@CommandAlias("mvsetspawn")
@CommandPermission("multiverse.core.spawn.set")
@Syntax("[world x y z [yaw pitch]]")
@CommandCompletion("@MVWorlds")
@Description("Sets the spawn for the current world.")
public void onAliasSetSpawnCommand(@NotNull CommandSender sender,
@NotNull @Flags("other,defaultself,fallbackself") Location location) {
doSpawnSet(sender, location);
}
@CommandAlias("mv")
@Subcommand("setspawn")
@CommandPermission("multiverse.core.spawn.set")
@Syntax("[world x y z [yaw pitch]]")
//TODO: Location tab complete doesnt work.
@CommandCompletion("@MVWorlds")
@Description("Sets the spawn for the current world.")
public void onSetSpawnCommand(@NotNull CommandSender sender,
@NotNull @Flags("other,defaultself,fallbackself") Location location) {
doSpawnSet(sender, location);
}
private void doSpawnSet(@NotNull CommandSender sender, @NotNull Location location) {
World bukkitWorld = location.getWorld();
if (bukkitWorld == null) {
sender.sendMessage("No world found for the spawn location your tried to set.");
return;
}
MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(bukkitWorld);
if (world == null) {
bukkitWorld.setSpawnLocation(location.getBlockX(), location.getBlockY(), location.getBlockZ());
sender.sendMessage("Multiverse does not know about this world, only X,Y and Z set.");
sender.sendMessage("Please import it (see /mv import) to set the spawn fully with Pitch and Yaw.");
return;
}
world.setSpawnLocation(location);
BlockSafety blockSafety = this.plugin.getBlockSafety();
if (!blockSafety.playerCanSpawnHereSafely(location) && world.getAdjustSpawn()) {
sender.sendMessage("It looks like that location would normally be unsafe. But I trust you.");
sender.sendMessage("I'm turning off the Safe-T-Teleporter for spawns to this world.");
sender.sendMessage("If you want turn this back on, just do " + ChatColor.AQUA + "/mvm set adjustspawn true " + world.getName());
world.setAdjustSpawn(false);
}
sender.sendMessage((sender instanceof Player && ((Player) sender).getWorld().equals(world.getCBWorld()))
? "Spawn of this world is set to:"
: "Spawn for " + world.getColoredWorldString() + " is set to:");
sender.sendMessage(plugin.getLocationManipulation().strCoords(location));
if (!plugin.saveWorldConfig()) {
sender.sendMessage(ChatColor.RED + "There was an issue saving worlds.yml! Your changes will only be temporary!");
}
}
}

View File

@ -2,19 +2,25 @@ package com.onarandombox.MultiverseCore.commands_helper;
import co.aikar.commands.BukkitCommandCompletionContext;
import co.aikar.commands.PaperCommandCompletions;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.enums.AddProperties;
import org.bukkit.GameRule;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public class MVCommandCompletions extends PaperCommandCompletions {
@ -30,6 +36,7 @@ public class MVCommandCompletions extends PaperCommandCompletions {
registerAsyncCompletion("MVWorlds", this::suggestMVWorlds);
registerAsyncCompletion("unloadedWorlds", this::suggestUnloadedWorlds);
registerAsyncCompletion("potentialWorlds", this::suggestPotentialWorlds);
registerCompletion("location", this::suggestLocation);
registerAsyncCompletion("MVConfigs", this::suggestMVConfig); //TODO: Change to static
registerStaticCompletion("gameRules", suggestGameRules());
registerStaticCompletion("environments", suggestEnvironments());
@ -78,10 +85,46 @@ public class MVCommandCompletions extends PaperCommandCompletions {
}
private boolean folderHasDat(@NotNull File worldFolder) {
File[] files = worldFolder.listFiles((file, name) -> name.equalsIgnoreCase("level.dat"));
File[] files = worldFolder.listFiles((file, name) -> name.equalsIgnoreCase(".dat"));
return files != null && files.length > 0;
}
@NotNull
private List<String> suggestLocation(@NotNull BukkitCommandCompletionContext context) {
Player player = context.getPlayer();
if (player == null) {
return Collections.singletonList("0");
}
DecimalFormat df = new DecimalFormat();
df.setMinimumFractionDigits(0);
df.setMaximumFractionDigits(2);
Location playerLocation = player.getLocation();
double coordValue;
switch (context.getConfig()) {
case "x":
coordValue = playerLocation.getX();
break;
case "y":
coordValue = playerLocation.getY();
break;
case "z":
coordValue = playerLocation.getZ();
break;
case "yaw":
coordValue = playerLocation.getYaw();
break;
case "pitch":
coordValue = playerLocation.getPitch();
break;
default:
return Collections.emptyList();
}
return Arrays.asList("~", String.valueOf(coordValue));
}
@NotNull
private Set<String> suggestMVConfig(@NotNull BukkitCommandCompletionContext context) {
return this.plugin.getMVConfig().serialize().keySet();

View File

@ -4,6 +4,7 @@ import co.aikar.commands.BukkitCommandExecutionContext;
import co.aikar.commands.InvalidCommandArgument;
import co.aikar.commands.PaperCommandContexts;
import co.aikar.commands.annotation.Values;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVDestination;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
@ -12,6 +13,7 @@ import com.onarandombox.MultiverseCore.destination.InvalidDestination;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameRule;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.WorldType;
import org.bukkit.command.CommandSender;
@ -25,6 +27,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public class MVCommandContexts extends PaperCommandContexts {
@ -47,6 +50,7 @@ public class MVCommandContexts extends PaperCommandContexts {
registerIssuerAwareContext(GameRule.class, this::deriveGameRule);
registerIssuerAwareContext(MVDestination.class, this::deriveMVDestination);
registerIssuerAwareContext(String.class, this::deriveString);
registerIssuerAwareContext(Location.class, this::deriveLocation);
}
@NotNull
@ -412,4 +416,56 @@ public class MVCommandContexts extends PaperCommandContexts {
// Removes relative paths.
return worldName.replaceAll("^[./\\\\]+", "");
}
private Location deriveLocation(BukkitCommandExecutionContext context) {
Logging.info("testing location");
if (context.getArgs().isEmpty()) {
Player player = context.getPlayer();
if (player != null) {
return player.getLocation();
}
throw new InvalidCommandArgument("You need to specify world and coordinates from the console!");
}
MultiverseWorld world;
try {
world = deriveMultiverseWorld(context);
}
catch (ClassCastException e) {
e.printStackTrace();
throw new InvalidCommandArgument("There was an error getting Target location world!");
}
Logging.info(world.getName());
List<String> locationArgs = context.getArgs();
if (locationArgs.size() != 3 && locationArgs.size() != 5) {
context.getSender().sendMessage(ChatColor.RED + "Invalid location arguments.");
context.getSender().sendMessage("Use no arguments for your current location, or world/x/y/z, or world/x/y/z/yaw/pitch!");
throw new InvalidCommandArgument(true);
}
double x = parsePos(locationArgs.get(0), "x");
double y = parsePos(locationArgs.get(1), "y");
double z = parsePos(locationArgs.get(2), "z");
double yaw = 0.0;
double pitch = 0.0;
if (locationArgs.size() == 5) {
yaw = parsePos(locationArgs.get(3), "yaw");
pitch = parsePos(locationArgs.get(4), "pitch");
}
return new Location(world.getCBWorld(), x, y, z, (float) yaw, (float) pitch);
}
private double parsePos(String value, String posType) {
try {
return Double.parseDouble(value);
}
catch (NumberFormatException e) {
throw new InvalidCommandArgument("'" + value + "' for "+ posType + " coordinate is not a number.", false);
}
}
}

View File

@ -30,6 +30,7 @@ import com.onarandombox.MultiverseCore.commands_acf.RegenCommand;
import com.onarandombox.MultiverseCore.commands_acf.ReloadCommand;
import com.onarandombox.MultiverseCore.commands_acf.RemoveCommand;
import com.onarandombox.MultiverseCore.commands_acf.ScriptCommand;
import com.onarandombox.MultiverseCore.commands_acf.SetSpawnCommand;
import com.onarandombox.MultiverseCore.commands_acf.SilentCommand;
import com.onarandombox.MultiverseCore.commands_acf.SpawnCommand;
import com.onarandombox.MultiverseCore.commands_acf.TeleportCommand;
@ -92,6 +93,7 @@ public class MVCommandManager extends PaperCommandManager {
registerCommand(new TeleportCommand(plugin));
registerCommand(new SilentCommand(plugin));
registerCommand(new PurgeCommand(plugin));
registerCommand(new SetSpawnCommand(plugin));
}
@Override