88import com .mojang .datafixers .DataFixer ;
99import com .mojang .logging .LogUtils ;
1010import com .mojang .serialization .Dynamic ;
11+ import net .earthcomputer .clientcommands .ClientCommands ;
1112import net .fabricmc .fabric .api .client .command .v2 .FabricClientCommandSource ;
12- import net .fabricmc .loader .api .FabricLoader ;
1313import net .minecraft .ChatFormatting ;
1414import net .minecraft .SharedConstants ;
1515import net .minecraft .Util ;
1616import net .minecraft .client .Minecraft ;
1717import net .minecraft .client .gui .GuiGraphics ;
1818import 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 ;
2021import net .minecraft .nbt .CompoundTag ;
2122import net .minecraft .nbt .ListTag ;
2223import net .minecraft .nbt .NbtIo ;
2324import net .minecraft .nbt .NbtOps ;
2425import net .minecraft .nbt .Tag ;
2526import net .minecraft .network .chat .Component ;
27+ import net .minecraft .util .ProblemReporter ;
2628import net .minecraft .util .datafix .fixes .References ;
29+ import net .minecraft .world .ContainerHelper ;
30+ import net .minecraft .world .ItemStackWithSlot ;
2731import net .minecraft .world .entity .player .Inventory ;
2832import net .minecraft .world .inventory .InventoryMenu ;
2933import net .minecraft .world .inventory .Slot ;
3034import 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 ;
3138import org .slf4j .Logger ;
3239
3340import java .io .File ;
4249import static net .minecraft .commands .SharedSuggestionProvider .*;
4350
4451public class KitCommand {
45-
4652 private static final Logger LOGGER = LogUtils .getLogger ();
4753
4854 private static final SimpleCommandExceptionType SAVE_FAILED_EXCEPTION = new SimpleCommandExceptionType (Component .translatable ("commands.ckit.saveFile.failed" ));
@@ -52,8 +58,6 @@ public class KitCommand {
5258 private static final SimpleCommandExceptionType NOT_CREATIVE_EXCEPTION = new SimpleCommandExceptionType (Component .translatable ("commands.ckit.load.notCreative" ));
5359 private static final DynamicCommandExceptionType NOT_FOUND_EXCEPTION = new DynamicCommandExceptionType (arg -> Component .translatable ("commands.ckit.notFound" , arg ));
5460
55- private static final Path configPath = FabricLoader .getInstance ().getConfigDir ().resolve ("clientcommands" );
56-
5761 private static final Map <String , ListTag > kits = new HashMap <>();
5862
5963 static {
@@ -95,7 +99,7 @@ private static int create(FabricClientCommandSource source, String name) throws
9599 if (kits .containsKey (name )) {
96100 throw ALREADY_EXISTS_EXCEPTION .create (name );
97101 }
98- kits .put (name , source .getPlayer (). getInventory ().save ( new ListTag ()));
102+ kits .put (name , saveInventory ( source .registryAccess (), source . getPlayer ().getInventory ()));
99103 saveFile ();
100104 source .sendFeedback (Component .translatable ("commands.ckit.create.success" , name ));
101105 return Command .SINGLE_SUCCESS ;
@@ -114,7 +118,7 @@ private static int edit(FabricClientCommandSource source, String name) throws Co
114118 if (!kits .containsKey (name )) {
115119 throw NOT_FOUND_EXCEPTION .create (name );
116120 }
117- kits .put (name , source .getPlayer (). getInventory ().save ( new ListTag ()));
121+ kits .put (name , saveInventory ( source .registryAccess (), source . getPlayer ().getInventory ()));
118122 saveFile ();
119123 source .sendFeedback (Component .translatable ("commands.ckit.edit.success" , name ));
120124 return Command .SINGLE_SUCCESS ;
@@ -131,7 +135,7 @@ private static int load(FabricClientCommandSource source, String name, boolean o
131135 }
132136
133137 Inventory tempInv = new Inventory (source .getPlayer (), source .getPlayer ().equipment );
134- tempInv . load ( kit );
138+ loadInventory ( source . registryAccess (), tempInv , kit );
135139 List <Slot > slots = source .getPlayer ().inventoryMenu .slots ;
136140 for (int i = 0 ; i < slots .size (); i ++) {
137141 if (slots .get (i ).container == source .getPlayer ().getInventory ()) {
@@ -165,7 +169,7 @@ private static int preview(FabricClientCommandSource source, String name) throws
165169 }
166170
167171 Inventory tempInv = new Inventory (source .getPlayer (), source .getPlayer ().equipment );
168- tempInv . load ( kit );
172+ loadInventory ( source . registryAccess (), tempInv , kit );
169173 /*
170174 After executing a command, the current screen will be closed (the chat hud).
171175 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 {
181185 CompoundTag rootTag = new CompoundTag ();
182186 CompoundTag compoundTag = new CompoundTag ();
183187 kits .forEach (compoundTag ::put );
184- rootTag .putInt ("DataVersion" , SharedConstants .getCurrentVersion ().getDataVersion ().getVersion ());
188+ rootTag .putInt ("DataVersion" , SharedConstants .getCurrentVersion ().dataVersion ().version ());
185189 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 ();
187191 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" );;
190194 Util .safeReplaceFile (currentFile , newFile , backupFile );
191195 } catch (IOException e ) {
192196 throw SAVE_FAILED_EXCEPTION .create ();
@@ -195,11 +199,11 @@ private static void saveFile() throws CommandSyntaxException {
195199
196200 private static void loadFile () throws IOException {
197201 kits .clear ();
198- CompoundTag rootTag = NbtIo .read (configPath .resolve ("kits.dat" ));
202+ CompoundTag rootTag = NbtIo .read (ClientCommands . CONFIG_DIR .resolve ("kits.dat" ));
199203 if (rootTag == null ) {
200204 return ;
201205 }
202- final int currentVersion = SharedConstants .getCurrentVersion ().getDataVersion ().getVersion ();
206+ final int currentVersion = SharedConstants .getCurrentVersion ().dataVersion ().version ();
203207 final int fileVersion = rootTag .getIntOr ("DataVersion" , 0 );
204208 CompoundTag compoundTag = rootTag .getCompoundOrEmpty ("Kits" );
205209 DataFixer dataFixer = Minecraft .getInstance ().getFixerUpper ();
@@ -217,6 +221,23 @@ private static void loadFile() throws IOException {
217221 });
218222 }
219223 }
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+ }
220241}
221242
222243class PreviewScreen extends AbstractContainerScreen <InventoryMenu > {
@@ -227,20 +248,19 @@ public PreviewScreen(InventoryMenu menu, Inventory inventory, String name) {
227248
228249 @ Override
229250 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 );
231252 }
232253
233254 @ Override
234255 public void render (GuiGraphics graphics , int mouseX , int mouseY , float delta ) {
235- this .renderBackground (graphics , mouseX , mouseY , delta );
236256 super .render (graphics , mouseX , mouseY , delta );
237257
238258 this .renderTooltip (graphics , mouseX , mouseY );
239259 }
240260
241261 @ Override
242262 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 );
244264 }
245265
246266 @ Override
0 commit comments