Skip to content

Commit

Permalink
Refactor profile viewer command and update open screen commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed Jun 29, 2024
1 parent 0338192 commit d824171
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import de.hysky.skyblocker.SkyblockerMod;
Expand Down Expand Up @@ -190,17 +189,9 @@ public static void initClass() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {
LiteralArgumentBuilder<FabricClientCommandSource> literalArgumentBuilder = ClientCommandManager.literal("pv")
.then(ClientCommandManager.argument("username", StringArgumentType.string())
.executes(context -> {
String username = StringArgumentType.getString(context, "username");
Command<FabricClientCommandSource> cmd = Scheduler.queueOpenScreenCommand(() -> new ProfileViewerScreen(username));
return cmd.run(context);
})
.executes(Scheduler.queueOpenScreenFactoryCommand(context -> new ProfileViewerScreen(StringArgumentType.getString(context, "username"))))
)
.executes(context -> {
String username = MinecraftClient.getInstance().getSession().getUsername();
Command<FabricClientCommandSource> cmd = Scheduler.queueOpenScreenCommand(() -> new ProfileViewerScreen(username));
return cmd.run(context);
});
.executes(Scheduler.queueOpenScreenCommand(() -> new ProfileViewerScreen(MinecraftClient.getInstance().getSession().getUsername())));
dispatcher.register(literalArgumentBuilder);
dispatcher.register(ClientCommandManager.literal(SkyblockerMod.NAMESPACE).then(literalArgumentBuilder));
});
Expand Down
46 changes: 43 additions & 3 deletions src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import it.unimi.dsi.fastutil.ints.AbstractInt2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
Expand All @@ -14,6 +15,7 @@
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -73,18 +75,56 @@ public void scheduleCyclic(Runnable task, int period, boolean multithreaded) {
}
}

/**
* Creates a command that queues a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
*
* @param screenFactory the factory of the screen to open
* @return the command
*/
public static Command<FabricClientCommandSource> queueOpenScreenFactoryCommand(Function<CommandContext<FabricClientCommandSource>, Screen> screenFactory) {
return context -> queueOpenScreen(screenFactory.apply(context));
}

/**
* Creates a command that queues a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
*
* @param screenSupplier the supplier of the screen to open
* @return the command
*/
public static Command<FabricClientCommandSource> queueOpenScreenCommand(Supplier<Screen> screenSupplier) {
return context -> INSTANCE.queueOpenScreen(screenSupplier);
return context -> queueOpenScreen(screenSupplier.get());
}

/**
* Creates a command that queues a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
*
* @param screen the screen to open
* @return the command
*/
public static Command<FabricClientCommandSource> queueOpenScreenCommand(Screen screen) {
return context -> queueOpenScreen(screen);
}

/**
* Schedules a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
*
* @deprecated Use {@link #queueOpenScreen(Screen)} instead
* @param screenSupplier the supplier of the screen to open
* @see #queueOpenScreenCommand(Supplier)
*/
public int queueOpenScreen(Supplier<Screen> screenSupplier) {
MinecraftClient.getInstance().send(() -> MinecraftClient.getInstance().setScreen(screenSupplier.get()));
@Deprecated(forRemoval = true)
public static int queueOpenScreen(Supplier<Screen> screenSupplier) {
return queueOpenScreen(screenSupplier.get());
}

/**
* Schedules a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
*
* @param screen the screen to open
* @see #queueOpenScreenFactoryCommand(Function)
*/
public static int queueOpenScreen(Screen screen) {
MinecraftClient.getInstance().send(() -> MinecraftClient.getInstance().setScreen(screen));
return Command.SINGLE_SUCCESS;
}

Expand Down

0 comments on commit d824171

Please sign in to comment.