Skip to content

Commit 9659380

Browse files
authored
Add /ckit preview <name> (#232)
* Add /ckit preview <name> * Optimise imports * Implement requested changes
1 parent aeff0a4 commit 9659380

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

src/main/java/net/earthcomputer/clientcommands/command/KitCommand.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.earthcomputer.clientcommands.command;
22

3+
import com.mojang.blaze3d.systems.RenderSystem;
34
import com.mojang.brigadier.CommandDispatcher;
45
import com.mojang.brigadier.arguments.StringArgumentType;
56
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@@ -12,14 +13,19 @@
1213
import net.fabricmc.loader.api.FabricLoader;
1314
import net.minecraft.SharedConstants;
1415
import net.minecraft.client.MinecraftClient;
16+
import net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen;
17+
import net.minecraft.client.util.math.MatrixStack;
1518
import net.minecraft.command.CommandSource;
1619
import net.minecraft.datafixer.TypeReferences;
1720
import net.minecraft.entity.player.PlayerInventory;
1821
import net.minecraft.item.ItemStack;
1922
import net.minecraft.nbt.*;
23+
import net.minecraft.screen.PlayerScreenHandler;
2024
import net.minecraft.screen.slot.Slot;
2125
import net.minecraft.server.command.ServerCommandSource;
26+
import net.minecraft.text.LiteralText;
2227
import net.minecraft.text.TranslatableText;
28+
import net.minecraft.util.Formatting;
2329
import net.minecraft.util.Util;
2430
import org.apache.logging.log4j.LogManager;
2531
import org.apache.logging.log4j.Logger;
@@ -36,7 +42,7 @@
3642

3743
public class KitCommand {
3844

39-
private static final Logger logger = LogManager.getLogger("clientcommands");
45+
private static final Logger LOGGER = LogManager.getLogger("clientcommands");
4046

4147
private static final SimpleCommandExceptionType SAVE_FAILED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.ckit.saveFile.failed"));
4248

@@ -55,7 +61,7 @@ public class KitCommand {
5561
try {
5662
loadFile();
5763
} catch (IOException e) {
58-
logger.info("Could not load kits file, hence /ckit will not work!");
64+
LOGGER.info("Could not load kits file, hence /ckit will not work!");
5965
}
6066
}
6167

@@ -82,7 +88,11 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
8288
.executes(ctx -> load(ctx.getSource(), StringArgumentType.getString(ctx, "name"), true)))
8389
.executes(ctx -> load(ctx.getSource(), StringArgumentType.getString(ctx, "name"), false))))
8490
.then(literal("list")
85-
.executes(ctx -> list(ctx.getSource()))));
91+
.executes(ctx -> list(ctx.getSource())))
92+
.then(literal("preview")
93+
.then(argument("name", StringArgumentType.string())
94+
.suggests((ctx, builder) -> CommandSource.suggestMatching(kits.keySet(), builder))
95+
.executes(ctx -> preview(ctx.getSource(), StringArgumentType.getString(ctx, "name"))))));
8696
}
8797

8898
private static int create(ServerCommandSource source, String name) throws CommandSyntaxException {
@@ -151,6 +161,24 @@ private static int list(ServerCommandSource source) {
151161
return kits.size();
152162
}
153163

164+
private static int preview(ServerCommandSource source, String name) throws CommandSyntaxException {
165+
ListTag kit = kits.get(name);
166+
if (kit == null) {
167+
throw NOT_FOUND_EXCEPTION.create(name);
168+
}
169+
170+
PlayerInventory tempInv = new PlayerInventory(client.player);
171+
tempInv.deserialize(kit);
172+
/*
173+
After executing a command, the current screen will be closed (the chat hud).
174+
And if you open a new screen in a command, that new screen will be closed
175+
instantly along with the chat hud. Slightly delaying the opening of the
176+
screen fixes this issue.
177+
*/
178+
client.send(() -> client.openScreen(new PreviewScreen(new PlayerScreenHandler(tempInv, true, client.player), tempInv, name)));
179+
return 0;
180+
}
181+
154182
private static void saveFile() throws CommandSyntaxException {
155183
try {
156184
CompoundTag rootTag = new CompoundTag();
@@ -192,3 +220,30 @@ private static void loadFile() throws IOException {
192220
}
193221
}
194222
}
223+
224+
class PreviewScreen extends AbstractInventoryScreen<PlayerScreenHandler> {
225+
226+
public PreviewScreen(PlayerScreenHandler playerScreenHandler, PlayerInventory inventory, String name) {
227+
super(playerScreenHandler, inventory, new LiteralText(name).styled(style -> style.withColor(Formatting.RED)));
228+
this.passEvents = true;
229+
this.titleX = 80;
230+
}
231+
232+
protected void drawForeground(MatrixStack matrices, int mouseX, int mouseY) {
233+
this.textRenderer.draw(matrices, this.title, (float) this.titleX, (float) this.titleY, 0x404040);
234+
}
235+
236+
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
237+
this.drawStatusEffects = false;
238+
this.renderBackground(matrices);
239+
super.render(matrices, mouseX, mouseY, delta);
240+
241+
this.drawMouseoverTooltip(matrices, mouseX, mouseY);
242+
}
243+
244+
protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) {
245+
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
246+
this.client.getTextureManager().bindTexture(BACKGROUND_TEXTURE);
247+
this.drawTexture(matrices, this.x, this.y, 0, 0, this.backgroundWidth, this.backgroundHeight);
248+
}
249+
}

0 commit comments

Comments
 (0)