mirror of
https://github.com/GeyserMC/Rainbow.git
synced 2025-12-19 14:59:16 +00:00
Some refactors
This commit is contained in:
@@ -8,8 +8,8 @@ import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry;
|
|||||||
import net.minecraft.commands.synchronization.SingletonArgumentInfo;
|
import net.minecraft.commands.synchronization.SingletonArgumentInfo;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
import org.geysermc.packgenerator.command.CommandSuggestionsArgumentType;
|
import org.geysermc.packgenerator.command.CommandSuggestionsArgumentType;
|
||||||
import org.geysermc.packgenerator.command.ItemSuggestionMapper;
|
|
||||||
import org.geysermc.packgenerator.command.PackGeneratorCommand;
|
import org.geysermc.packgenerator.command.PackGeneratorCommand;
|
||||||
|
import org.geysermc.packgenerator.mapper.PackMappers;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
public class GeyserMappingsGenerator implements ClientModInitializer {
|
public class GeyserMappingsGenerator implements ClientModInitializer {
|
||||||
@@ -18,16 +18,19 @@ public class GeyserMappingsGenerator implements ClientModInitializer {
|
|||||||
public static final String MOD_NAME = "Geyser Mappings Generator";
|
public static final String MOD_NAME = "Geyser Mappings Generator";
|
||||||
public static final Logger LOGGER = LogUtils.getLogger();
|
public static final Logger LOGGER = LogUtils.getLogger();
|
||||||
|
|
||||||
|
private final PackManager packManager = new PackManager();
|
||||||
|
private final PackMappers packMappers = new PackMappers(packManager);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitializeClient() {
|
public void onInitializeClient() {
|
||||||
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> PackGeneratorCommand.register(dispatcher));
|
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> PackGeneratorCommand.register(dispatcher, packManager, packMappers));
|
||||||
ClientTickEvents.START_CLIENT_TICK.register(minecraft -> ItemSuggestionMapper.getInstance().tick(minecraft));
|
ClientTickEvents.START_CLIENT_TICK.register(packMappers::tick);
|
||||||
|
|
||||||
ArgumentTypeRegistry.registerArgumentType(getModdedLocation("command_suggestions"),
|
ArgumentTypeRegistry.registerArgumentType(getModdedLocation("command_suggestions"),
|
||||||
CommandSuggestionsArgumentType.class, SingletonArgumentInfo.contextFree(CommandSuggestionsArgumentType::new));
|
CommandSuggestionsArgumentType.class, SingletonArgumentInfo.contextFree(CommandSuggestionsArgumentType::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResourceLocation getModdedLocation(String path) {
|
public static ResourceLocation getModdedLocation(String path) {
|
||||||
return ResourceLocation.fromNamespaceAndPath(MOD_ID, path);
|
return ResourceLocation.fromNamespaceAndPath(MOD_ID, path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,12 +8,8 @@ import java.util.Optional;
|
|||||||
|
|
||||||
public final class PackManager {
|
public final class PackManager {
|
||||||
|
|
||||||
private static final PackManager INSTANCE = new PackManager();
|
|
||||||
|
|
||||||
private BedrockPack currentPack;
|
private BedrockPack currentPack;
|
||||||
|
|
||||||
private PackManager() {}
|
|
||||||
|
|
||||||
public void startPack(String name) throws IOException {
|
public void startPack(String name) throws IOException {
|
||||||
if (currentPack != null) {
|
if (currentPack != null) {
|
||||||
throw new IllegalStateException("Already started a pack (" + currentPack.name() + ")");
|
throw new IllegalStateException("Already started a pack (" + currentPack.name() + ")");
|
||||||
@@ -40,8 +36,4 @@ public final class PackManager {
|
|||||||
throw new IllegalStateException("Create a new pack first!");
|
throw new IllegalStateException("Create a new pack first!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PackManager getInstance() {
|
|
||||||
return INSTANCE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import net.minecraft.ChatFormatting;
|
|||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.world.entity.player.Inventory;
|
import net.minecraft.world.entity.player.Inventory;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import org.geysermc.packgenerator.GeyserMappingsGenerator;
|
||||||
import org.geysermc.packgenerator.PackManager;
|
import org.geysermc.packgenerator.PackManager;
|
||||||
|
import org.geysermc.packgenerator.mapper.PackMappers;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@@ -18,14 +20,14 @@ import java.util.function.Consumer;
|
|||||||
|
|
||||||
public class PackGeneratorCommand {
|
public class PackGeneratorCommand {
|
||||||
|
|
||||||
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
|
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher, PackManager packManager, PackMappers mappers) {
|
||||||
dispatcher.register(ClientCommandManager.literal("packgenerator")
|
dispatcher.register(ClientCommandManager.literal("packgenerator")
|
||||||
.then(ClientCommandManager.literal("create")
|
.then(ClientCommandManager.literal("create")
|
||||||
.then(ClientCommandManager.argument("name", StringArgumentType.word())
|
.then(ClientCommandManager.argument("name", StringArgumentType.word())
|
||||||
.executes(context -> {
|
.executes(context -> {
|
||||||
String name = StringArgumentType.getString(context, "name");
|
String name = StringArgumentType.getString(context, "name");
|
||||||
try {
|
try {
|
||||||
PackManager.getInstance().startPack(name);
|
packManager.startPack(name);
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
context.getSource().sendError(Component.literal("Failed to create new pack: " + exception.getMessage()));
|
context.getSource().sendError(Component.literal("Failed to create new pack: " + exception.getMessage()));
|
||||||
return -1;
|
return -1;
|
||||||
@@ -38,7 +40,7 @@ public class PackGeneratorCommand {
|
|||||||
.then(ClientCommandManager.literal("map")
|
.then(ClientCommandManager.literal("map")
|
||||||
.executes(context -> {
|
.executes(context -> {
|
||||||
ItemStack heldItem = context.getSource().getPlayer().getMainHandItem();
|
ItemStack heldItem = context.getSource().getPlayer().getMainHandItem();
|
||||||
Optional<Boolean> problems = PackManager.getInstance().map(heldItem);
|
Optional<Boolean> problems = packManager.map(heldItem);
|
||||||
if (problems.isEmpty()) {
|
if (problems.isEmpty()) {
|
||||||
context.getSource().sendError(Component.literal("No item found to map!"));
|
context.getSource().sendError(Component.literal("No item found to map!"));
|
||||||
} else if (problems.get()) {
|
} else if (problems.get()) {
|
||||||
@@ -49,11 +51,11 @@ public class PackGeneratorCommand {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
.then(ClientCommandManager.literal("mapinventory")
|
.then(ClientCommandManager.literal("mapinventory")
|
||||||
.executes(context -> mapInventory(context.getSource().getPlayer().getInventory(), context.getSource()::sendFeedback, true))
|
.executes(context -> mapInventory(packManager, context.getSource().getPlayer().getInventory(), context.getSource()::sendFeedback, true))
|
||||||
)
|
)
|
||||||
.then(ClientCommandManager.literal("finish")
|
.then(ClientCommandManager.literal("finish")
|
||||||
.executes(context -> {
|
.executes(context -> {
|
||||||
if (!PackManager.getInstance().finish()) {
|
if (!packManager.finish()) {
|
||||||
context.getSource().sendError(Component.literal("Errors occurred whilst trying to write the pack to disk!"));
|
context.getSource().sendError(Component.literal("Errors occurred whilst trying to write the pack to disk!"));
|
||||||
}
|
}
|
||||||
context.getSource().sendFeedback(Component.literal("Wrote pack to disk"));
|
context.getSource().sendFeedback(Component.literal("Wrote pack to disk"));
|
||||||
@@ -63,14 +65,14 @@ public class PackGeneratorCommand {
|
|||||||
.then(ClientCommandManager.literal("auto")
|
.then(ClientCommandManager.literal("auto")
|
||||||
.then(ClientCommandManager.argument("suggestions", CommandSuggestionsArgumentType.TYPE)
|
.then(ClientCommandManager.argument("suggestions", CommandSuggestionsArgumentType.TYPE)
|
||||||
.executes(context -> {
|
.executes(context -> {
|
||||||
PackManager.getInstance().ensurePackIsCreated();
|
packManager.ensurePackIsCreated();
|
||||||
Pair<String, CompletableFuture<Suggestions>> suggestions = CommandSuggestionsArgumentType.getSuggestions(context, "suggestions");
|
Pair<String, CompletableFuture<Suggestions>> suggestions = CommandSuggestionsArgumentType.getSuggestions(context, "suggestions");
|
||||||
String baseCommand = suggestions.getFirst();
|
String baseCommand = suggestions.getFirst();
|
||||||
suggestions.getSecond().thenAccept(completed -> {
|
suggestions.getSecond().thenAccept(completed -> {
|
||||||
ItemSuggestionMapper.getInstance().start(completed.getList().stream()
|
mappers.getSuggestionMapper().start(completed.getList().stream()
|
||||||
.map(suggestion -> baseCommand.substring(0, suggestion.getRange().getStart()) + suggestion.getText())
|
.map(suggestion -> baseCommand.substring(0, suggestion.getRange().getStart()) + suggestion.getText())
|
||||||
.toList());
|
.toList());
|
||||||
context.getSource().sendFeedback(Component.literal("Running " + ItemSuggestionMapper.getInstance().queueSize() + " commands to obtain custom items to map"));
|
context.getSource().sendFeedback(Component.literal("Running " + mappers.getSuggestionMapper().queueSize() + " commands to obtain custom items to map"));
|
||||||
});
|
});
|
||||||
return 0;
|
return 0;
|
||||||
})
|
})
|
||||||
@@ -79,11 +81,11 @@ public class PackGeneratorCommand {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int mapInventory(Inventory inventory, Consumer<Component> feedback, boolean feedbackOnEmpty) {
|
public static int mapInventory(PackManager manager, Inventory inventory, Consumer<Component> feedback, boolean feedbackOnEmpty) {
|
||||||
int mapped = 0;
|
int mapped = 0;
|
||||||
boolean errors = false;
|
boolean errors = false;
|
||||||
for (ItemStack stack : inventory) {
|
for (ItemStack stack : inventory) {
|
||||||
Optional<Boolean> problems = PackManager.getInstance().map(stack);
|
Optional<Boolean> problems = manager.map(stack);
|
||||||
if (problems.isPresent()) {
|
if (problems.isPresent()) {
|
||||||
mapped++;
|
mapped++;
|
||||||
errors |= problems.get();
|
errors |= problems.get();
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package org.geysermc.packgenerator.mapper;
|
||||||
|
|
||||||
|
import org.geysermc.packgenerator.PackManager;
|
||||||
|
|
||||||
|
public class InventoryMapper {
|
||||||
|
private final PackManager packManager;
|
||||||
|
|
||||||
|
public InventoryMapper(PackManager packManager) {
|
||||||
|
this.packManager = packManager;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +1,25 @@
|
|||||||
package org.geysermc.packgenerator.command;
|
package org.geysermc.packgenerator.mapper;
|
||||||
|
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.network.protocol.game.ServerboundChatCommandPacket;
|
import net.minecraft.network.protocol.game.ServerboundChatCommandPacket;
|
||||||
|
import org.geysermc.packgenerator.PackManager;
|
||||||
|
import org.geysermc.packgenerator.command.PackGeneratorCommand;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
// TODO safety
|
// TODO safety
|
||||||
public final class ItemSuggestionMapper {
|
public final class ItemSuggestionMapper {
|
||||||
private static final ItemSuggestionMapper INSTANCE = new ItemSuggestionMapper();
|
private final PackManager packManager;
|
||||||
|
|
||||||
private final List<String> remainingCommands = new ArrayList<>();
|
private final List<String> remainingCommands = new ArrayList<>();
|
||||||
private boolean waitingOnItem = false;
|
private boolean waitingOnItem = false;
|
||||||
private boolean waitingOnClear = false;
|
private boolean waitingOnClear = false;
|
||||||
private int mapped = 0;
|
private int mapped = 0;
|
||||||
|
|
||||||
private ItemSuggestionMapper() {}
|
public ItemSuggestionMapper(PackManager packManager) {
|
||||||
|
this.packManager = packManager;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
public boolean start(List<String> commands) {
|
public boolean start(List<String> commands) {
|
||||||
@@ -41,7 +44,7 @@ public final class ItemSuggestionMapper {
|
|||||||
waitingOnItem = true;
|
waitingOnItem = true;
|
||||||
} else {
|
} else {
|
||||||
if (!minecraft.player.getInventory().isEmpty()) {
|
if (!minecraft.player.getInventory().isEmpty()) {
|
||||||
mapped += PackGeneratorCommand.mapInventory(minecraft.player.getInventory(),
|
mapped += PackGeneratorCommand.mapInventory(packManager, minecraft.player.getInventory(),
|
||||||
component -> minecraft.player.displayClientMessage(component, false), false);
|
component -> minecraft.player.displayClientMessage(component, false), false);
|
||||||
|
|
||||||
minecraft.getConnection().send(new ServerboundChatCommandPacket("clear"));
|
minecraft.getConnection().send(new ServerboundChatCommandPacket("clear"));
|
||||||
@@ -67,8 +70,4 @@ public final class ItemSuggestionMapper {
|
|||||||
waitingOnClear = false;
|
waitingOnClear = false;
|
||||||
mapped = 0;
|
mapped = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ItemSuggestionMapper getInstance() {
|
|
||||||
return INSTANCE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package org.geysermc.packgenerator.mapper;
|
||||||
|
|
||||||
|
import net.minecraft.client.Minecraft;
|
||||||
|
import org.geysermc.packgenerator.PackManager;
|
||||||
|
|
||||||
|
public class PackMappers {
|
||||||
|
private final ItemSuggestionMapper suggestionMapper;
|
||||||
|
private final InventoryMapper inventoryMapper;
|
||||||
|
|
||||||
|
public PackMappers(PackManager packManager) {
|
||||||
|
this.suggestionMapper = new ItemSuggestionMapper(packManager);
|
||||||
|
this.inventoryMapper = new InventoryMapper(packManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemSuggestionMapper getSuggestionMapper() {
|
||||||
|
return suggestionMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InventoryMapper getInventoryMapper() {
|
||||||
|
return inventoryMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tick(Minecraft minecraft) {
|
||||||
|
suggestionMapper.tick(minecraft);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user