1
1
package net .earthcomputer .clientcommands .command ;
2
2
3
+ import com .mojang .blaze3d .systems .RenderSystem ;
3
4
import com .mojang .brigadier .CommandDispatcher ;
4
5
import com .mojang .brigadier .arguments .StringArgumentType ;
5
6
import com .mojang .brigadier .exceptions .CommandSyntaxException ;
12
13
import net .fabricmc .loader .api .FabricLoader ;
13
14
import net .minecraft .SharedConstants ;
14
15
import net .minecraft .client .MinecraftClient ;
16
+ import net .minecraft .client .gui .screen .ingame .AbstractInventoryScreen ;
17
+ import net .minecraft .client .util .math .MatrixStack ;
15
18
import net .minecraft .command .CommandSource ;
16
19
import net .minecraft .datafixer .TypeReferences ;
17
20
import net .minecraft .entity .player .PlayerInventory ;
18
21
import net .minecraft .item .ItemStack ;
19
22
import net .minecraft .nbt .*;
23
+ import net .minecraft .screen .PlayerScreenHandler ;
20
24
import net .minecraft .screen .slot .Slot ;
21
25
import net .minecraft .server .command .ServerCommandSource ;
26
+ import net .minecraft .text .LiteralText ;
22
27
import net .minecraft .text .TranslatableText ;
28
+ import net .minecraft .util .Formatting ;
23
29
import net .minecraft .util .Util ;
24
30
import org .apache .logging .log4j .LogManager ;
25
31
import org .apache .logging .log4j .Logger ;
36
42
37
43
public class KitCommand {
38
44
39
- private static final Logger logger = LogManager .getLogger ("clientcommands" );
45
+ private static final Logger LOGGER = LogManager .getLogger ("clientcommands" );
40
46
41
47
private static final SimpleCommandExceptionType SAVE_FAILED_EXCEPTION = new SimpleCommandExceptionType (new TranslatableText ("commands.ckit.saveFile.failed" ));
42
48
@@ -55,7 +61,7 @@ public class KitCommand {
55
61
try {
56
62
loadFile ();
57
63
} 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!" );
59
65
}
60
66
}
61
67
@@ -82,7 +88,11 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
82
88
.executes (ctx -> load (ctx .getSource (), StringArgumentType .getString (ctx , "name" ), true )))
83
89
.executes (ctx -> load (ctx .getSource (), StringArgumentType .getString (ctx , "name" ), false ))))
84
90
.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" ))))));
86
96
}
87
97
88
98
private static int create (ServerCommandSource source , String name ) throws CommandSyntaxException {
@@ -151,6 +161,24 @@ private static int list(ServerCommandSource source) {
151
161
return kits .size ();
152
162
}
153
163
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
+
154
182
private static void saveFile () throws CommandSyntaxException {
155
183
try {
156
184
CompoundTag rootTag = new CompoundTag ();
@@ -192,3 +220,30 @@ private static void loadFile() throws IOException {
192
220
}
193
221
}
194
222
}
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