mirror of
https://github.com/GeyserMC/Rainbow.git
synced 2025-12-19 14:59:16 +00:00
Improved /rainbow command feedback
This commit is contained in:
@@ -27,10 +27,6 @@ public final class PackManager {
|
||||
currentPack.ifPresentOrElse(consumer, runnable);
|
||||
}
|
||||
|
||||
public <T> Optional<T> run(Function<BedrockPack, T> function) {
|
||||
return currentPack.map(function);
|
||||
}
|
||||
|
||||
public Optional<Boolean> finish() {
|
||||
Optional<Boolean> success = currentPack.map(BedrockPack::save);
|
||||
currentPack = Optional.empty();
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
package org.geysermc.rainbow.command;
|
||||
|
||||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
|
||||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import org.geysermc.rainbow.PackManager;
|
||||
import org.geysermc.rainbow.mapper.InventoryMapper;
|
||||
import org.geysermc.rainbow.mapper.ItemSuggestionProvider;
|
||||
import org.geysermc.rainbow.mapper.PackMapper;
|
||||
import org.geysermc.rainbow.pack.BedrockPack;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class PackGeneratorCommand {
|
||||
|
||||
@@ -30,8 +26,8 @@ public class PackGeneratorCommand {
|
||||
try {
|
||||
packManager.startPack(name);
|
||||
} catch (Exception exception) {
|
||||
context.getSource().sendError(Component.literal("Failed to create new pack: " + exception.getMessage()));
|
||||
return -1;
|
||||
context.getSource().sendError(Component.literal("Failed to create new pack!"));
|
||||
throw new RuntimeException(exception);
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Created pack with name " + name));
|
||||
return 0;
|
||||
@@ -39,36 +35,43 @@ public class PackGeneratorCommand {
|
||||
)
|
||||
)
|
||||
.then(ClientCommandManager.literal("map")
|
||||
.executes(context -> {
|
||||
packManager.run(pack -> {
|
||||
ItemStack heldItem = context.getSource().getPlayer().getMainHandItem();
|
||||
Optional<Boolean> problems = pack.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!"));
|
||||
.executes(runWithPack(packManager, (source, pack) -> {
|
||||
ItemStack heldItem = source.getPlayer().getMainHandItem();
|
||||
switch (pack.map(heldItem)) {
|
||||
case NONE_MAPPED -> source.sendError(Component.literal("No item was mapped. Either no custom item was found, or it was already included in the pack"));
|
||||
case PROBLEMS_OCCURRED -> source.sendFeedback(Component.literal("The held item was mapped, however problems occurred whilst doing so. Read the pack report after finishing the pack for more information"));
|
||||
case MAPPED_SUCCESSFULLY -> source.sendFeedback(Component.literal("The held item was mapped"));
|
||||
}
|
||||
context.getSource().sendFeedback(Component.literal("Added held item to Geyser mappings"));
|
||||
});
|
||||
return 0;
|
||||
})
|
||||
}))
|
||||
)
|
||||
.then(ClientCommandManager.literal("mapinventory")
|
||||
.executes(context -> mapInventory(packManager, context.getSource().getPlayer().getInventory(), context.getSource()::sendFeedback, true))
|
||||
)
|
||||
.then(ClientCommandManager.literal("finish")
|
||||
.executes(context -> {
|
||||
packManager.finish().ifPresent(success -> {
|
||||
if (!success) {
|
||||
context.getSource().sendError(Component.literal("Errors occurred whilst trying to write the pack to disk!"));
|
||||
} else {
|
||||
context.getSource().sendFeedback(Component.literal("Wrote pack to disk"));
|
||||
.executes(runWithPack(packManager, (source, pack) -> {
|
||||
int mapped = 0;
|
||||
boolean errors = false;
|
||||
Inventory inventory = source.getPlayer().getInventory();
|
||||
|
||||
for (ItemStack stack : inventory) {
|
||||
BedrockPack.MappingResult result = pack.map(stack);
|
||||
if (result != BedrockPack.MappingResult.NONE_MAPPED) {
|
||||
mapped++;
|
||||
if (result == BedrockPack.MappingResult.PROBLEMS_OCCURRED) {
|
||||
errors = true;
|
||||
}
|
||||
});
|
||||
return 0;
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (mapped > 0) {
|
||||
source.sendFeedback(Component.literal("Mapped " + mapped + " items from your inventory"));
|
||||
if (errors) {
|
||||
source.sendFeedback(Component.literal("Problems occurred whilst mapping items. Read the pack report after finishing the pack for more information"));
|
||||
}
|
||||
} else {
|
||||
source.sendError(Component.literal("No items were mapped. Either no custom items were found, or they were already included in the pack"));
|
||||
}
|
||||
}))
|
||||
)
|
||||
.then(ClientCommandManager.literal("auto")
|
||||
/* This is disabled for now.
|
||||
.then(ClientCommandManager.literal("command")
|
||||
.then(ClientCommandManager.argument("suggestions", CommandSuggestionsArgumentType.TYPE)
|
||||
.executes(context -> {
|
||||
@@ -85,45 +88,40 @@ public class PackGeneratorCommand {
|
||||
})
|
||||
)
|
||||
)
|
||||
*/
|
||||
.then(ClientCommandManager.literal("inventory")
|
||||
.executes(context -> {
|
||||
.executes(runWithPack(packManager, (source, pack) -> {
|
||||
packMapper.setItemProvider(InventoryMapper.INSTANCE);
|
||||
context.getSource().sendFeedback(Component.literal("Now watching inventories for custom items to map"));
|
||||
return 0;
|
||||
})
|
||||
source.sendFeedback(Component.literal("Now watching inventories for custom items to map"));
|
||||
}))
|
||||
)
|
||||
.then(ClientCommandManager.literal("stop")
|
||||
.executes(context -> {
|
||||
.executes(runWithPack(packManager, (source, pack) -> {
|
||||
packMapper.setItemProvider(null);
|
||||
context.getSource().sendFeedback(Component.literal("Stopped automatic mapping of custom items"));
|
||||
source.sendFeedback(Component.literal("Stopped automatic mapping of custom items"));
|
||||
}))
|
||||
)
|
||||
)
|
||||
.then(ClientCommandManager.literal("finish")
|
||||
.executes(context -> {
|
||||
packManager.finish().ifPresentOrElse(success -> {
|
||||
if (!success) {
|
||||
context.getSource().sendError(Component.literal("Errors occurred whilst writing the pack to disk!"));
|
||||
} else {
|
||||
context.getSource().sendFeedback(Component.literal("Wrote pack to disk"));
|
||||
}
|
||||
}, () -> context.getSource().sendError(Component.literal("Create a pack first!")));
|
||||
return 0;
|
||||
})
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static int mapInventory(PackManager manager, Inventory inventory, Consumer<Component> feedback, boolean feedbackOnEmpty) {
|
||||
return manager.run(pack -> {
|
||||
int mapped = 0;
|
||||
boolean errors = false;
|
||||
for (ItemStack stack : inventory) {
|
||||
Optional<Boolean> problems = pack.map(stack);
|
||||
if (problems.isPresent()) {
|
||||
mapped++;
|
||||
errors |= problems.get();
|
||||
}
|
||||
}
|
||||
if (mapped > 0) {
|
||||
if (errors) {
|
||||
feedback.accept(Component.literal("Problems occurred whilst mapping items!").withStyle(ChatFormatting.RED));
|
||||
}
|
||||
feedback.accept(Component.literal("Mapped " + mapped + " items from your inventory"));
|
||||
} else if (feedbackOnEmpty) {
|
||||
feedback.accept(Component.literal("No items were mapped"));
|
||||
}
|
||||
|
||||
return mapped;
|
||||
}).orElse(0);
|
||||
private static Command<FabricClientCommandSource> runWithPack(PackManager manager, BiConsumer<FabricClientCommandSource, BedrockPack> executor) {
|
||||
return context -> {
|
||||
manager.runOrElse(pack -> executor.accept(context.getSource(), pack),
|
||||
() -> context.getSource().sendError(Component.literal("Create a pack first!")));
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import net.minecraft.client.multiplayer.ClientPacketListener;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import org.geysermc.rainbow.PackManager;
|
||||
import org.geysermc.rainbow.pack.BedrockPack;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
@@ -30,7 +31,7 @@ public class PackMapper {
|
||||
// TODO maybe report problems here... probably better to do so in pack class though
|
||||
long mapped = itemProvider.nextItems(player, connection)
|
||||
.map(pack::map)
|
||||
.filter(Optional::isPresent)
|
||||
.filter(result -> result != BedrockPack.MappingResult.NONE_MAPPED)
|
||||
.count();
|
||||
if (mapped != 0) {
|
||||
player.displayClientMessage(Component.literal("Mapped " + mapped + " items"), false);
|
||||
|
||||
@@ -71,19 +71,19 @@ public class BedrockPack {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Optional<Boolean> map(ItemStack stack) {
|
||||
public MappingResult map(ItemStack stack) {
|
||||
if (stack.isEmpty()) {
|
||||
return Optional.empty();
|
||||
return MappingResult.NONE_MAPPED;
|
||||
}
|
||||
|
||||
Optional<? extends ResourceLocation> patchedModel = stack.getComponentsPatch().get(DataComponents.ITEM_MODEL);
|
||||
//noinspection OptionalAssignedToNull - annoying Mojang
|
||||
if (patchedModel == null || patchedModel.isEmpty()) {
|
||||
return Optional.empty();
|
||||
return MappingResult.NONE_MAPPED;
|
||||
}
|
||||
ResourceLocation model = patchedModel.get();
|
||||
if (!modelsMapped.add(model)) {
|
||||
return Optional.empty();
|
||||
return MappingResult.NONE_MAPPED;
|
||||
}
|
||||
|
||||
AtomicBoolean problems = new AtomicBoolean();
|
||||
@@ -108,7 +108,7 @@ public class BedrockPack {
|
||||
}
|
||||
bedrockItems.add(bedrockItem);
|
||||
}, texturesToExport::add);
|
||||
return Optional.of(problems.get());
|
||||
return problems.get() ? MappingResult.PROBLEMS_OCCURRED : MappingResult.MAPPED_SUCCESSFULLY;
|
||||
}
|
||||
|
||||
public boolean save() {
|
||||
@@ -189,4 +189,10 @@ Textures tried to export: %d
|
||||
return new PackManifest(new PackManifest.Header(name, PackConstants.DEFAULT_PACK_DESCRIPTION, UUID.randomUUID(), BedrockVersion.of(0), PackConstants.ENGINE_VERSION),
|
||||
List.of(new PackManifest.Module(name, PackConstants.DEFAULT_PACK_DESCRIPTION, UUID.randomUUID(), BedrockVersion.of(0))));
|
||||
}
|
||||
|
||||
public enum MappingResult {
|
||||
NONE_MAPPED,
|
||||
MAPPED_SUCCESSFULLY,
|
||||
PROBLEMS_OCCURRED
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user