|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2026 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.chestsearch; |
| 9 | + |
| 10 | +import com.google.gson.JsonElement; |
| 11 | +import com.mojang.serialization.JsonOps; |
| 12 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 13 | +import net.minecraft.resources.Identifier; |
| 14 | +import net.minecraft.world.item.Item; |
| 15 | +import net.minecraft.world.item.ItemStack; |
| 16 | +import net.wurstclient.WurstClient; |
| 17 | + |
| 18 | +public final class ChestSearchItemStacks |
| 19 | +{ |
| 20 | + private ChestSearchItemStacks() |
| 21 | + {} |
| 22 | + |
| 23 | + public static JsonElement encode(ItemStack stack) |
| 24 | + { |
| 25 | + if(stack == null || stack.isEmpty() || WurstClient.MC.player == null) |
| 26 | + return null; |
| 27 | + |
| 28 | + try |
| 29 | + { |
| 30 | + return ItemStack.CODEC |
| 31 | + .encodeStart(WurstClient.MC.player.registryAccess() |
| 32 | + .createSerializationContext(JsonOps.INSTANCE), stack) |
| 33 | + .result().orElse(null); |
| 34 | + }catch(Throwable t) |
| 35 | + { |
| 36 | + return null; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public static ItemStack decode(ChestEntry.ItemEntry item) |
| 41 | + { |
| 42 | + if(item == null) |
| 43 | + return ItemStack.EMPTY; |
| 44 | + |
| 45 | + if(item.nbt != null && WurstClient.MC.player != null) |
| 46 | + try |
| 47 | + { |
| 48 | + ItemStack stack = ItemStack.CODEC |
| 49 | + .decode( |
| 50 | + WurstClient.MC.player.registryAccess() |
| 51 | + .createSerializationContext(JsonOps.INSTANCE), |
| 52 | + item.nbt) |
| 53 | + .result().map(pair -> pair.getFirst()) |
| 54 | + .orElse(ItemStack.EMPTY); |
| 55 | + if(!stack.isEmpty()) |
| 56 | + { |
| 57 | + stack.setCount(Math.max(1, item.count)); |
| 58 | + return stack; |
| 59 | + } |
| 60 | + }catch(Throwable ignored) |
| 61 | + {} |
| 62 | + |
| 63 | + try |
| 64 | + { |
| 65 | + Identifier id = Identifier.tryParse(item.itemId); |
| 66 | + if(id == null) |
| 67 | + return ItemStack.EMPTY; |
| 68 | + Item mcItem = BuiltInRegistries.ITEM.getValue(id); |
| 69 | + if(mcItem == null) |
| 70 | + return ItemStack.EMPTY; |
| 71 | + return new ItemStack(mcItem, Math.max(1, item.count)); |
| 72 | + }catch(Throwable t) |
| 73 | + { |
| 74 | + return ItemStack.EMPTY; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments