mirror of
https://github.com/GeyserMC/Rainbow.git
synced 2025-12-19 14:59:16 +00:00
Command cleanup
This commit is contained in:
@@ -1,13 +1,8 @@
|
||||
package org.geysermc.packgenerator;
|
||||
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
||||
import com.mojang.brigadier.suggestion.Suggestion;
|
||||
import com.mojang.logging.LogUtils;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
|
||||
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
|
||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||
import net.minecraft.network.chat.Component;
|
||||
@@ -15,7 +10,6 @@ import net.minecraft.network.protocol.game.ServerboundChatCommandPacket;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -31,81 +25,7 @@ public class GeyserMappingsGenerator implements ClientModInitializer {
|
||||
|
||||
@Override
|
||||
public void onInitializeClient() {
|
||||
// TODO do the exceptions properly
|
||||
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> {
|
||||
dispatcher.register(ClientCommandManager.literal("packgenerator")
|
||||
.then(ClientCommandManager.literal("create")
|
||||
.then(ClientCommandManager.argument("name", StringArgumentType.word())
|
||||
.executes(context -> {
|
||||
String name = StringArgumentType.getString(context, "name");
|
||||
try {
|
||||
PackManager.getInstance().startPack(name);
|
||||
} catch (IOException exception) {
|
||||
throw new SimpleCommandExceptionType(Component.literal(exception.getMessage())).create();
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Created pack with name " + name));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
)
|
||||
.then(ClientCommandManager.literal("map")
|
||||
.executes(context -> {
|
||||
ItemStack heldItem = context.getSource().getPlayer().getMainHandItem();
|
||||
PackManager.getInstance().map(heldItem);
|
||||
context.getSource().sendFeedback(Component.literal("Added held item to Geyser mappings"));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
.then(ClientCommandManager.literal("mapinventory")
|
||||
.executes(context -> {
|
||||
int mapped = 0;
|
||||
for (ItemStack stack : context.getSource().getPlayer().getInventory()) {
|
||||
if (PackManager.getInstance().map(stack)) {
|
||||
mapped++;
|
||||
}
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Mapped " + mapped + " items from your inventory"));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
.then(ClientCommandManager.literal("finish")
|
||||
.executes(context -> {
|
||||
if (!PackManager.getInstance().finish()) {
|
||||
throw new SimpleCommandExceptionType(Component.literal("Errors occurred whilst trying to write the pack to disk!")).create();
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Wrote pack to disk"));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
.then(ClientCommandManager.literal("auto")
|
||||
.then(ClientCommandManager.argument("namespace", StringArgumentType.word())
|
||||
.then(ClientCommandManager.argument("path", StringArgumentType.string())
|
||||
.executes(context -> {
|
||||
String namespace = StringArgumentType.getString(context, "namespace");
|
||||
String path = StringArgumentType.getString(context, "path");
|
||||
|
||||
// Duplicated code, this is just to try this out
|
||||
try {
|
||||
PackManager.getInstance().startPack(namespace);
|
||||
} catch (IOException exception) {
|
||||
throw new SimpleCommandExceptionType(Component.literal(exception.getMessage())).create();
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Created pack with name " + namespace));
|
||||
|
||||
// hack
|
||||
CommandContext<?> suggestionsContext = new CommandContext<>(null,
|
||||
"loot give @s loot " + namespace + ":" + path,
|
||||
null, null, null, null, null, null, null, false);
|
||||
context.getSource().getClient().getConnection().getSuggestionsProvider()
|
||||
.customSuggestion(suggestionsContext)
|
||||
.whenComplete((suggestions, throwable) -> pendingPackCommands.addAll(suggestions.getList().stream().map(Suggestion::getText).toList()));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
});
|
||||
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> PackGeneratorCommand.register(dispatcher));
|
||||
|
||||
ClientTickEvents.START_CLIENT_TICK.register(client -> {
|
||||
if (!pendingPackCommands.isEmpty() || waitingOnItem || waitingOnClear) {
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package org.geysermc.packgenerator;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
||||
import com.mojang.brigadier.suggestion.Suggestion;
|
||||
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
|
||||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
public class PackGeneratorCommand {
|
||||
|
||||
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
|
||||
dispatcher.register(ClientCommandManager.literal("packgenerator")
|
||||
.then(ClientCommandManager.literal("create")
|
||||
.then(ClientCommandManager.argument("name", StringArgumentType.word())
|
||||
.executes(context -> {
|
||||
String name = StringArgumentType.getString(context, "name");
|
||||
try {
|
||||
PackManager.getInstance().startPack(name);
|
||||
} catch (Exception exception) {
|
||||
context.getSource().sendError(Component.literal("Failed to create new pack: " + exception.getMessage()));
|
||||
return -1;
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Created pack with name " + name));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
)
|
||||
.then(ClientCommandManager.literal("map")
|
||||
.executes(context -> {
|
||||
ItemStack heldItem = context.getSource().getPlayer().getMainHandItem();
|
||||
Optional<Boolean> problems = PackManager.getInstance().map(heldItem);
|
||||
if (problems.isEmpty()) {
|
||||
context.getSource().sendError(Component.literal("No item found to map!"));
|
||||
} else if (problems.get()) {
|
||||
context.getSource().sendError(Component.literal("Problems occurred whilst mapping the item!"));
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Added held item to Geyser mappings"));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
.then(ClientCommandManager.literal("mapinventory")
|
||||
.executes(context -> {
|
||||
int mapped = 0;
|
||||
boolean errors = false;
|
||||
for (ItemStack stack : context.getSource().getPlayer().getInventory()) {
|
||||
Optional<Boolean> problems = PackManager.getInstance().map(stack);
|
||||
if (problems.isPresent()) {
|
||||
mapped++;
|
||||
errors |= problems.get();
|
||||
}
|
||||
}
|
||||
if (errors) {
|
||||
context.getSource().sendError(Component.literal("Problems occurred whilst mapping items!"));
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Mapped " + mapped + " items from your inventory"));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
.then(ClientCommandManager.literal("finish")
|
||||
.executes(context -> {
|
||||
if (!PackManager.getInstance().finish()) {
|
||||
context.getSource().sendError(Component.literal("Errors occurred whilst trying to write the pack to disk!"));
|
||||
return -1;
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Wrote pack to disk"));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
.then(ClientCommandManager.literal("auto")
|
||||
.then(ClientCommandManager.argument("namespace", StringArgumentType.word())
|
||||
.then(ClientCommandManager.argument("path", StringArgumentType.string())
|
||||
.executes(context -> {
|
||||
String namespace = StringArgumentType.getString(context, "namespace");
|
||||
String path = StringArgumentType.getString(context, "path");
|
||||
|
||||
// Duplicated code, this is just to try this out
|
||||
try {
|
||||
PackManager.getInstance().startPack(namespace);
|
||||
} catch (IOException exception) {
|
||||
throw new SimpleCommandExceptionType(Component.literal(exception.getMessage())).create();
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Created pack with name " + namespace));
|
||||
|
||||
// hack
|
||||
CommandContext<?> suggestionsContext = new CommandContext<>(null,
|
||||
"loot give @s loot " + namespace + ":" + path,
|
||||
null, null, null, null, null, null, null, false);
|
||||
context.getSource().getClient().getConnection().getSuggestionsProvider()
|
||||
.customSuggestion(suggestionsContext)
|
||||
.whenComplete((suggestions, throwable) -> pendingPackCommands.addAll(suggestions.getList().stream().map(Suggestion::getText).toList()));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import net.minecraft.world.item.ItemStack;
|
||||
import org.geysermc.packgenerator.pack.BedrockPack;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class PackManager {
|
||||
|
||||
@@ -16,15 +17,15 @@ public final class PackManager {
|
||||
|
||||
private PackManager() {}
|
||||
|
||||
public void startPack(String name) throws CommandSyntaxException, IOException {
|
||||
public void startPack(String name) throws IOException {
|
||||
if (currentPack != null) {
|
||||
throw new SimpleCommandExceptionType(Component.literal("Already started a pack (" + currentPack.name() + ")")).create();
|
||||
throw new IllegalStateException("Already started a pack (" + currentPack.name() + ")");
|
||||
}
|
||||
|
||||
currentPack = new BedrockPack(name);
|
||||
}
|
||||
|
||||
public boolean map(ItemStack stack) throws CommandSyntaxException {
|
||||
public Optional<Boolean> map(ItemStack stack) throws CommandSyntaxException {
|
||||
ensurePackIsCreated();
|
||||
|
||||
return currentPack.map(stack);
|
||||
|
||||
@@ -13,7 +13,6 @@ import org.geysermc.packgenerator.mapping.geyser.GeyserMappings;
|
||||
import org.geysermc.packgenerator.pack.attachable.BedrockAttachable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -22,6 +21,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@@ -64,9 +64,9 @@ public class BedrockPack {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean map(ItemStack stack) {
|
||||
public Optional<Boolean> map(ItemStack stack) {
|
||||
if (stack.isEmpty()) {
|
||||
return false;
|
||||
return Optional.empty();
|
||||
}
|
||||
AtomicBoolean problems = new AtomicBoolean();
|
||||
ProblemReporter mapReporter = new ProblemReporter() {
|
||||
@@ -93,7 +93,7 @@ public class BedrockPack {
|
||||
texturesToExport.add(texture);
|
||||
AttachableMapper.mapItem(stack, mapping.bedrockIdentifier(), texturesToExport::add).ifPresent(attachables::add);
|
||||
});
|
||||
return problems.get();
|
||||
return Optional.of(problems.get());
|
||||
}
|
||||
|
||||
public boolean save() {
|
||||
|
||||
Reference in New Issue
Block a user