Change dimension command to teleport to any different instance

This commit is contained in:
TheMode 2021-07-28 20:30:45 +02:00
parent 16fbc5ea2c
commit d6dd81b35f

View File

@ -1,53 +1,30 @@
package demo.commands;
import net.kyori.adventure.text.Component;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.CommandContext;
import net.minestom.server.command.builder.arguments.ArgumentType;
import net.minestom.server.command.builder.arguments.ArgumentWord;
import net.minestom.server.command.builder.condition.Conditions;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.world.DimensionType;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
public class DimensionCommand extends Command {
private final ArgumentWord dimension_type;
public DimensionCommand() {
super("dimensiontest");
setCondition(Conditions::playerOnly);
dimension_type = ArgumentType.Word("dimension type");
dimension_type.from(MinecraftServer.getDimensionTypeManager().unmodifiableList().stream().map(DimensionType::getName).map(Object::toString).toArray(String[]::new));
addSyntax(this::execute, dimension_type);
}
private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) {
final Player player = commandSender.asPlayer();
final Instance instance = player.getInstance();
final String typeName = commandContext.get(dimension_type);
final Optional<Instance> targetInstance = MinecraftServer.getInstanceManager().getInstances().stream().filter(in -> in.getDimensionType().toString().equals(typeName)).findFirst();
if (targetInstance.isPresent()) {
if (instance != null) {
if (targetInstance.get() != instance) {
player.sendMessage(Component.text("You were in " + instance.getDimensionType()));
player.setInstance(targetInstance.get());
player.sendMessage(Component.text("You are now in " + typeName));
} else {
player.sendMessage(Component.text("You are already in the instance"));
}
} else {
player.setInstance(targetInstance.get());
player.sendMessage(Component.text("You did the impossible and you are now in " + typeName));
addSyntax((sender, context) -> {
final Player player = sender.asPlayer();
final Instance instance = player.getInstance();
final var instances = MinecraftServer.getInstanceManager().getInstances().stream().filter(instance1 -> !instance1.equals(instance)).collect(Collectors.toList());
if (instances.isEmpty()) {
player.sendMessage("No instance available");
return;
}
} else {
player.sendMessage(Component.text("Could not find instance with dimension " + typeName));
}
final var newInstance = instances.get(ThreadLocalRandom.current().nextInt(instances.size()));
player.setInstance(newInstance).thenRun(() -> player.sendMessage("Teleported"));
});
}
}