Skip to content

Commit

Permalink
added shard migrate command (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
HamaIndustries authored Feb 9, 2024
1 parent 443a3e4 commit 2d1374a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.modfest.scatteredshards.api.shard.ShardType;
import net.modfest.scatteredshards.client.screen.ShardCreatorGuiDescription;
import net.modfest.scatteredshards.client.screen.ShardTabletGuiDescription;
import net.modfest.scatteredshards.command.ShardCommand;

import java.util.concurrent.CompletableFuture;

Expand All @@ -33,7 +34,6 @@ private static DynamicCommandExceptionType createInvalidException(String item) {
}

private static final DynamicCommandExceptionType INVALID_SET_ID = createInvalidException("set_id");
private static final DynamicCommandExceptionType INVALID_SHARD_TYPE = createInvalidException("shard_type");
private static final DynamicCommandExceptionType INVALID_SHARD_ID = createInvalidException("shard_id");

public static int view(CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException {
Expand All @@ -49,7 +49,7 @@ public static int creatorNew(CommandContext<FabricClientCommandSource> context)
String modId = StringArgumentType.getString(context, "mod_id");
Identifier shardTypeId = context.getArgument("shard_type", Identifier.class);
ShardType shardType = ScatteredShardsAPI.getClientLibrary().shardTypes().get(shardTypeId)
.orElseThrow(() -> INVALID_SHARD_TYPE.create(shardTypeId));
.orElseThrow(() -> ShardCommand.INVALID_SHARD_TYPE.create(shardTypeId));

var client = context.getSource().getClient();
client.send(() -> client.setScreen(ShardCreatorGuiDescription.Screen.newShard(modId, shardType)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package net.modfest.scatteredshards.command;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.tree.CommandNode;

import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.modfest.scatteredshards.ScatteredShards;
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
import net.modfest.scatteredshards.api.impl.ShardLibraryPersistentState;
import net.modfest.scatteredshards.api.shard.Shard;
import net.modfest.scatteredshards.api.shard.ShardType;
import net.modfest.scatteredshards.networking.S2CDeleteShard;
import net.modfest.scatteredshards.networking.S2CSyncLibrary;
import net.modfest.scatteredshards.networking.S2CSyncShard;

public class LibraryCommand {

public static int delete(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
Identifier shardId = ctx.getArgument("shard_id", Identifier.class);

Expand Down Expand Up @@ -53,6 +61,33 @@ public static int deleteAll(CommandContext<ServerCommandSource> ctx) throws Comm

return toDelete;
}

public static int migrate(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
Identifier shardId = ctx.getArgument("shard_id", Identifier.class);
String modId = StringArgumentType.getString(ctx, "mod_id");
Identifier shardTypeId = ctx.getArgument("shard_type", Identifier.class);
Identifier newShardId = ShardType.createModId(shardTypeId, modId);

var library = ScatteredShardsAPI.getServerLibrary();
library.shardTypes().get(shardTypeId).orElseThrow(() -> ShardCommand.INVALID_SHARD_TYPE.create(shardTypeId));
Shard shard = library.shards().get(shardId).orElseThrow(() -> ShardCommand.INVALID_SHARD.create(shardId));

library.shards().remove(shardId);
shard.setShardType(shardTypeId);
library.shards().put(newShardId, shard);

var server = ctx.getSource().getServer();
ShardLibraryPersistentState.get(server).markDirty();

S2CDeleteShard.sendToAll(server, shardId);
for (var player : server.getPlayerManager().getPlayerList()) {
S2CSyncShard.send(player, newShardId, shard);
}

ctx.getSource().sendFeedback(() -> Text.stringifiedTranslatable("commands.scattered_shards.shard.library.migrate", shardId, newShardId), true);

return Command.SINGLE_SUCCESS;
}

public static void register(CommandNode<ServerCommandSource> parent) {
var library = Node.literal("library")
Expand Down Expand Up @@ -81,6 +116,19 @@ public static void register(CommandNode<ServerCommandSource> parent) {
)
.build();
deleteCommand.addChild(deleteAllCommand);

var migrateCommand = Node.literal("migrate")
.requires(
Permissions.require(ScatteredShards.permission("command.library.migrate"), 3)
).build();
var migrateShardArg = Node.shardId("shard_id").build();
var migrateModArg = Node.stringArgument("mod_id").suggests(Node::suggestModIds).build();
var migrateShardTypeArg = Node.identifier("shard_type").suggests(Node::suggestShardTypes)
.executes(LibraryCommand::migrate).build();
migrateModArg.addChild(migrateShardTypeArg);
migrateShardArg.addChild(migrateModArg);
migrateCommand.addChild(migrateShardArg);
library.addChild(migrateCommand);

parent.addChild(library);
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/net/modfest/scatteredshards/command/Node.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package net.modfest.scatteredshards.command;

import java.util.concurrent.CompletableFuture;

import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.tree.CommandNode;

import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.command.EntitySelector;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.IdentifierArgumentType;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
import net.modfest.scatteredshards.api.shard.ShardType;

public class Node {
public static LiteralArgumentBuilder<ServerCommandSource> literal(String name) {
Expand Down Expand Up @@ -68,6 +76,10 @@ public static RequiredArgumentBuilder<ServerCommandSource, Float> floatValue(Str
public static RequiredArgumentBuilder<ServerCommandSource, Boolean> booleanValue(String name) {
return RequiredArgumentBuilder.argument(name, BoolArgumentType.bool());
}

public static RequiredArgumentBuilder<ServerCommandSource, String> stringArgument(String name) {
return RequiredArgumentBuilder.<ServerCommandSource, String>argument(name, StringArgumentType.string());
}

/**
* Creates literal nodes as necessary to extend a command path to include the desired command node, and returns the
Expand All @@ -90,4 +102,20 @@ public CommandNode<ServerCommandSource> getOrCreate(CommandNode<ServerCommandSou

return cur;
}

public static CompletableFuture<Suggestions> suggestModIds(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
for (var mod : FabricLoader.getInstance().getAllMods()) {
builder.suggest(mod.getMetadata().getId());
}
return builder.buildFuture();
}

public static CompletableFuture<Suggestions> suggestShardTypes(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
ScatteredShardsAPI.getServerLibrary().shardTypes().forEach((id, shardSet) -> {
if (!id.equals(ShardType.MISSING_ID)) {
builder.suggest(id.toString());
}
});
return builder.buildFuture();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class ShardCommand {
public static final DynamicCommandExceptionType NO_ROOM_FOR_ITEM = new DynamicCommandExceptionType(
it -> Text.translatable("error.scattered_shards.no_inventory_room", it)
);

public static final DynamicCommandExceptionType INVALID_SHARD_TYPE = new DynamicCommandExceptionType(
it -> Text.translatable("error.scattered_shards.invalid_shard_type", it)
);

public static void register() {
CommandRegistrationCallback.EVENT.register((dispatcher, context, environment) -> {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/scattered_shards/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"commands.scattered_shards.shard.uncollect.all": "Un-Collected %s shards",
"commands.scattered_shards.shard.library.delete": "Deleted shard '%s' from the library",
"commands.scattered_shards.shard.library.delete.all": "Deleted %s shards from the library",
"commands.scattered_shards.shard.library.migrate": "Migrated shard %s to %s",
"gui.scattered_shards.creator.title": "Shard Creator",
"gui.scattered_shards.creator.field.name": "Name...",
"gui.scattered_shards.creator.field.lore": "Lore...",
Expand Down

0 comments on commit 2d1374a

Please sign in to comment.