Skip to content

Commit

Permalink
you are mental
Browse files Browse the repository at this point in the history
  • Loading branch information
not-coded committed Aug 24, 2024
1 parent d99d8f4 commit 35e76fc
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/main/java/com/nexia/core/commands/staff/MapCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.nexia.base.player.NexiaPlayer;
import com.nexia.core.utilities.chat.ChatFormat;
import com.nexia.core.utilities.commands.CommandUtil;
import com.nexia.base.player.NexiaPlayer;
import com.nexia.core.utilities.time.ServerTime;
import com.nexia.core.utilities.world.WorldUtil;
import com.nexia.nexus.api.command.CommandSourceInfo;
Expand All @@ -14,57 +15,67 @@
import com.nexia.nexus.api.world.util.Location;
import net.kyori.adventure.text.Component;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.arguments.ResourceLocationArgument;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biomes;
import org.apache.commons.io.FileUtils;

import org.jetbrains.annotations.NotNull;
import java.io.File;
import static net.minecraft.server.commands.LocateBiomeCommand.ERROR_INVALID_BIOME;

public class MapCommand {
public static void register(CommandDispatcher<CommandSourceInfo> dispatcher) {
dispatcher.register((CommandUtils.literal("map")
.requires(commandSourceInfo -> CommandUtil.hasPermission(commandSourceInfo, "nexia.staff.map", 2))
.then(CommandUtils.argument("type", StringArgumentType.string())
.suggests(((context, builder) -> SharedSuggestionProvider.suggest((new String[]{"delete", "create", "tp"}), builder)))
.then(CommandUtils.argument("map", StringArgumentType.greedyString())
.then(CommandUtils.argument("map", ResourceLocationArgument.id())
.suggests(((context, builder) -> SharedSuggestionProvider.suggest((WorldUtil.getAllWorldsId()), builder)))
.executes(MapCommand::run)
.executes((context -> run(context, StringArgumentType.getString(context, "type"), context.getArgument("map", ResourceLocation.class), Biomes.THE_VOID)))
.then(CommandUtils.argument("biome", ResourceLocationArgument.id())
.suggests(((context, builder) -> SharedSuggestionProvider.suggestResource((ServerTime.minecraftServer.registryAccess().registryOrThrow(Registry.BIOME_REGISTRY).keySet()), builder)))
.executes((context -> run(context, StringArgumentType.getString(context, "type"), context.getArgument("map", ResourceLocation.class), context.getArgument("biome", ResourceLocation.class))))
)
)
)
));
}

private static int run(CommandContext<CommandSourceInfo> context) {
private static int run(CommandContext<CommandSourceInfo> context, String type, ResourceLocation map, ResourceLocation biomeLocation) throws CommandSyntaxException {
// check if biome is valid
ServerTime.minecraftServer.registryAccess().registryOrThrow(Registry.BIOME_REGISTRY).getOptional(biomeLocation).orElseThrow(() -> ERROR_INVALID_BIOME.create(biomeLocation));

return run(context, type, map, ResourceKey.create(Registry.BIOME_REGISTRY, biomeLocation));
}

private static int run(CommandContext<CommandSourceInfo> context, String type, ResourceLocation map, @NotNull ResourceKey<Biome> biome) {
NexiaPlayer player = null;
if(context.getSource().getExecutingEntity() instanceof Player nexusPlayer) {
player = new NexiaPlayer(nexusPlayer);
}

String type = StringArgumentType.getString(context, "type");
String map = StringArgumentType.getString(context, "map");

if(map.trim().isEmpty() || type.trim().isEmpty()) {
if(map == null || map.toString().trim().isEmpty() || type.trim().isEmpty()) {
context.getSource().sendMessage(
ChatFormat.nexiaMessage
.append(Component.text("Invalid name!", ChatFormat.failColor))
);
return 1;
}

String[] mapname = map.split(":");

if(type.equalsIgnoreCase("create")){

ServerLevel level = ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(mapname[0], mapname[1])).location(), (WorldUtil.defaultWorldConfig)).asWorld();
ServerLevel level = ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, map).location(), (WorldUtil.defaultWorldConfig.setGenerator(WorldUtil.getChunkGenerator(biome)))).asWorld();

if (player != null) {
player.teleport(new Location(0, 80, 0, 0, 0, WorldUtil.getWorld(level)));

player.sendNexiaMessage(
Component.text("Created map called: ", ChatFormat.normalColor)
.append(Component.text(map, ChatFormat.brandColor2))
.append(Component.text(map.toString(), ChatFormat.brandColor2))
);
}

Expand All @@ -73,29 +84,29 @@ private static int run(CommandContext<CommandSourceInfo> context) {
}

if (type.equalsIgnoreCase("delete")) {
ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(mapname[0], mapname[1])).location(), null).delete();
ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, map).location(), null).delete();
if(player != null) {
player.sendNexiaMessage(
Component.text("Deleted map called: ", ChatFormat.normalColor)
.append(Component.text(map, ChatFormat.brandColor2))
.append(Component.text(map.toString(), ChatFormat.brandColor2))
);
}

try {
FileUtils.forceDeleteOnExit(new File("/world/dimensions/" + mapname[0], mapname[1]));
FileUtils.forceDeleteOnExit(new File("/world/dimensions/" + map.getNamespace(), map.getPath()));
} catch (Exception ignored) { }
return 1;
}

if(type.equalsIgnoreCase("tp")) {
ServerLevel level = ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(mapname[0], mapname[1])).location(), null).asWorld();
ServerLevel level = ServerTime.fantasy.getOrOpenPersistentWorld(ResourceKey.create(Registry.DIMENSION_REGISTRY, map).location(), null).asWorld();

if(player != null) {
player.teleport(new Location(0, 80, 0, 0, 0, WorldUtil.getWorld(level)));

player.sendNexiaMessage(
Component.text("Teleported to map called: ", ChatFormat.normalColor)
.append(Component.text(map, ChatFormat.brandColor2))
.append(Component.text(map.toString(), ChatFormat.brandColor2))
);
}

Expand Down

0 comments on commit 35e76fc

Please sign in to comment.