8
8
import com .mojang .datafixers .DataFixer ;
9
9
import com .mojang .logging .LogUtils ;
10
10
import com .mojang .serialization .Dynamic ;
11
+ import net .earthcomputer .clientcommands .ClientCommands ;
11
12
import net .fabricmc .fabric .api .client .command .v2 .FabricClientCommandSource ;
12
- import net .fabricmc .loader .api .FabricLoader ;
13
13
import net .minecraft .ChatFormatting ;
14
14
import net .minecraft .SharedConstants ;
15
15
import net .minecraft .Util ;
16
16
import net .minecraft .client .Minecraft ;
17
17
import net .minecraft .client .gui .GuiGraphics ;
18
18
import net .minecraft .client .gui .screens .inventory .AbstractContainerScreen ;
19
- import net .minecraft .client .renderer .RenderType ;
19
+ import net .minecraft .client .renderer .RenderPipelines ;
20
+ import net .minecraft .core .HolderLookup ;
20
21
import net .minecraft .nbt .CompoundTag ;
21
22
import net .minecraft .nbt .ListTag ;
22
23
import net .minecraft .nbt .NbtIo ;
23
24
import net .minecraft .nbt .NbtOps ;
24
25
import net .minecraft .nbt .Tag ;
25
26
import net .minecraft .network .chat .Component ;
27
+ import net .minecraft .util .ProblemReporter ;
26
28
import net .minecraft .util .datafix .fixes .References ;
29
+ import net .minecraft .world .ContainerHelper ;
30
+ import net .minecraft .world .ItemStackWithSlot ;
27
31
import net .minecraft .world .entity .player .Inventory ;
28
32
import net .minecraft .world .inventory .InventoryMenu ;
29
33
import net .minecraft .world .inventory .Slot ;
30
34
import net .minecraft .world .item .ItemStack ;
35
+ import net .minecraft .world .level .storage .TagValueInput ;
36
+ import net .minecraft .world .level .storage .TagValueOutput ;
37
+ import net .minecraft .world .level .storage .ValueInput ;
31
38
import org .slf4j .Logger ;
32
39
33
40
import java .io .File ;
42
49
import static net .minecraft .commands .SharedSuggestionProvider .*;
43
50
44
51
public class KitCommand {
45
-
46
52
private static final Logger LOGGER = LogUtils .getLogger ();
47
53
48
54
private static final SimpleCommandExceptionType SAVE_FAILED_EXCEPTION = new SimpleCommandExceptionType (Component .translatable ("commands.ckit.saveFile.failed" ));
@@ -52,8 +58,6 @@ public class KitCommand {
52
58
private static final SimpleCommandExceptionType NOT_CREATIVE_EXCEPTION = new SimpleCommandExceptionType (Component .translatable ("commands.ckit.load.notCreative" ));
53
59
private static final DynamicCommandExceptionType NOT_FOUND_EXCEPTION = new DynamicCommandExceptionType (arg -> Component .translatable ("commands.ckit.notFound" , arg ));
54
60
55
- private static final Path configPath = FabricLoader .getInstance ().getConfigDir ().resolve ("clientcommands" );
56
-
57
61
private static final Map <String , ListTag > kits = new HashMap <>();
58
62
59
63
static {
@@ -95,7 +99,7 @@ private static int create(FabricClientCommandSource source, String name) throws
95
99
if (kits .containsKey (name )) {
96
100
throw ALREADY_EXISTS_EXCEPTION .create (name );
97
101
}
98
- kits .put (name , source .getPlayer (). getInventory ().save ( new ListTag ()));
102
+ kits .put (name , saveInventory ( source .registryAccess (), source . getPlayer ().getInventory ()));
99
103
saveFile ();
100
104
source .sendFeedback (Component .translatable ("commands.ckit.create.success" , name ));
101
105
return Command .SINGLE_SUCCESS ;
@@ -114,7 +118,7 @@ private static int edit(FabricClientCommandSource source, String name) throws Co
114
118
if (!kits .containsKey (name )) {
115
119
throw NOT_FOUND_EXCEPTION .create (name );
116
120
}
117
- kits .put (name , source .getPlayer (). getInventory ().save ( new ListTag ()));
121
+ kits .put (name , saveInventory ( source .registryAccess (), source . getPlayer ().getInventory ()));
118
122
saveFile ();
119
123
source .sendFeedback (Component .translatable ("commands.ckit.edit.success" , name ));
120
124
return Command .SINGLE_SUCCESS ;
@@ -131,7 +135,7 @@ private static int load(FabricClientCommandSource source, String name, boolean o
131
135
}
132
136
133
137
Inventory tempInv = new Inventory (source .getPlayer (), source .getPlayer ().equipment );
134
- tempInv . load ( kit );
138
+ loadInventory ( source . registryAccess (), tempInv , kit );
135
139
List <Slot > slots = source .getPlayer ().inventoryMenu .slots ;
136
140
for (int i = 0 ; i < slots .size (); i ++) {
137
141
if (slots .get (i ).container == source .getPlayer ().getInventory ()) {
@@ -165,7 +169,7 @@ private static int preview(FabricClientCommandSource source, String name) throws
165
169
}
166
170
167
171
Inventory tempInv = new Inventory (source .getPlayer (), source .getPlayer ().equipment );
168
- tempInv . load ( kit );
172
+ loadInventory ( source . registryAccess (), tempInv , kit );
169
173
/*
170
174
After executing a command, the current screen will be closed (the chat hud).
171
175
And if you open a new screen in a command, that new screen will be closed
@@ -181,12 +185,12 @@ private static void saveFile() throws CommandSyntaxException {
181
185
CompoundTag rootTag = new CompoundTag ();
182
186
CompoundTag compoundTag = new CompoundTag ();
183
187
kits .forEach (compoundTag ::put );
184
- rootTag .putInt ("DataVersion" , SharedConstants .getCurrentVersion ().getDataVersion ().getVersion ());
188
+ rootTag .putInt ("DataVersion" , SharedConstants .getCurrentVersion ().dataVersion ().version ());
185
189
rootTag .put ("Kits" , compoundTag );
186
- Path newFile = File .createTempFile ("kits" , ".dat" , configPath .toFile ()).toPath ();
190
+ Path newFile = File .createTempFile ("kits" , ".dat" , ClientCommands . CONFIG_DIR .toFile ()).toPath ();
187
191
NbtIo .write (rootTag , newFile );
188
- Path backupFile = configPath .resolve ("kits.dat_old" );
189
- Path currentFile = configPath .resolve ("kits.dat" );;
192
+ Path backupFile = ClientCommands . CONFIG_DIR .resolve ("kits.dat_old" );
193
+ Path currentFile = ClientCommands . CONFIG_DIR .resolve ("kits.dat" );;
190
194
Util .safeReplaceFile (currentFile , newFile , backupFile );
191
195
} catch (IOException e ) {
192
196
throw SAVE_FAILED_EXCEPTION .create ();
@@ -195,11 +199,11 @@ private static void saveFile() throws CommandSyntaxException {
195
199
196
200
private static void loadFile () throws IOException {
197
201
kits .clear ();
198
- CompoundTag rootTag = NbtIo .read (configPath .resolve ("kits.dat" ));
202
+ CompoundTag rootTag = NbtIo .read (ClientCommands . CONFIG_DIR .resolve ("kits.dat" ));
199
203
if (rootTag == null ) {
200
204
return ;
201
205
}
202
- final int currentVersion = SharedConstants .getCurrentVersion ().getDataVersion ().getVersion ();
206
+ final int currentVersion = SharedConstants .getCurrentVersion ().dataVersion ().version ();
203
207
final int fileVersion = rootTag .getIntOr ("DataVersion" , 0 );
204
208
CompoundTag compoundTag = rootTag .getCompoundOrEmpty ("Kits" );
205
209
DataFixer dataFixer = Minecraft .getInstance ().getFixerUpper ();
@@ -217,6 +221,23 @@ private static void loadFile() throws IOException {
217
221
});
218
222
}
219
223
}
224
+
225
+ private static void loadInventory (HolderLookup .Provider registries , Inventory inventory , ListTag items ) {
226
+ CompoundTag nbt = new CompoundTag ();
227
+ nbt .put (ContainerHelper .TAG_ITEMS , items );
228
+ try (ProblemReporter .ScopedCollector collector = new ProblemReporter .ScopedCollector (LOGGER )) {
229
+ ValueInput input = TagValueInput .create (collector , registries , nbt );
230
+ inventory .load (input .listOrEmpty (ContainerHelper .TAG_ITEMS , ItemStackWithSlot .CODEC ));
231
+ }
232
+ }
233
+
234
+ private static ListTag saveInventory (HolderLookup .Provider registries , Inventory inventory ) {
235
+ try (ProblemReporter .ScopedCollector collector = new ProblemReporter .ScopedCollector (LOGGER )) {
236
+ TagValueOutput output = TagValueOutput .createWithContext (collector , registries );
237
+ inventory .save (output .list (ContainerHelper .TAG_ITEMS , ItemStackWithSlot .CODEC ));
238
+ return output .buildResult ().getListOrEmpty (ContainerHelper .TAG_ITEMS );
239
+ }
240
+ }
220
241
}
221
242
222
243
class PreviewScreen extends AbstractContainerScreen <InventoryMenu > {
@@ -227,20 +248,19 @@ public PreviewScreen(InventoryMenu menu, Inventory inventory, String name) {
227
248
228
249
@ Override
229
250
protected void renderLabels (GuiGraphics graphics , int mouseX , int mouseY ) {
230
- graphics .drawString (this .font , this .title , this .titleLabelX , this .titleLabelY , 0x404040 , false );
251
+ graphics .drawString (this .font , this .title , this .titleLabelX , this .titleLabelY , 0xff404040 , false );
231
252
}
232
253
233
254
@ Override
234
255
public void render (GuiGraphics graphics , int mouseX , int mouseY , float delta ) {
235
- this .renderBackground (graphics , mouseX , mouseY , delta );
236
256
super .render (graphics , mouseX , mouseY , delta );
237
257
238
258
this .renderTooltip (graphics , mouseX , mouseY );
239
259
}
240
260
241
261
@ Override
242
262
protected void renderBg (GuiGraphics graphics , float delta , int mouseX , int mouseY ) {
243
- graphics .blit (RenderType :: guiTextured , INVENTORY_LOCATION , this .leftPos , this .topPos , 0 , 0 , this .imageWidth , this .imageHeight , 256 , 256 );
263
+ graphics .blit (RenderPipelines . GUI_TEXTURED , INVENTORY_LOCATION , this .leftPos , this .topPos , 0 , 0 , this .imageWidth , this .imageHeight , 256 , 256 );
244
264
}
245
265
246
266
@ Override
0 commit comments