|
| 1 | +package me.matl114.bukkit; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMap; |
| 4 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 5 | +import com.mojang.datafixers.util.Pair; |
| 6 | +import com.mojang.serialization.*; |
| 7 | +import com.mojang.serialization.codecs.RecordCodecBuilder; |
| 8 | +import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap; |
| 9 | +import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; |
| 10 | +import me.matl114.utils.ItemStackUtils; |
| 11 | +import me.matl114.versioned.api.VItem; |
| 12 | +import me.matl114.versioned.api.VNbt; |
| 13 | +import net.minecraft.SharedConstants; |
| 14 | +import net.minecraft.component.ComponentChanges; |
| 15 | +import net.minecraft.component.ComponentMapImpl; |
| 16 | +import net.minecraft.component.ComponentType; |
| 17 | +import net.minecraft.component.DataComponentTypes; |
| 18 | +import net.minecraft.component.type.CustomModelDataComponent; |
| 19 | +import net.minecraft.item.Item; |
| 20 | +import net.minecraft.item.ItemStack; |
| 21 | +import net.minecraft.item.Items; |
| 22 | +import net.minecraft.nbt.*; |
| 23 | +import net.minecraft.registry.Registries; |
| 24 | +import net.minecraft.text.Style; |
| 25 | +import net.minecraft.util.Identifier; |
| 26 | +import net.minecraft.util.dynamic.Codecs; |
| 27 | + |
| 28 | +import java.util.*; |
| 29 | + |
| 30 | +public final class CraftItemStack extends BukkitItemStack { |
| 31 | + Map<String, String> compoundTag; |
| 32 | + String item; |
| 33 | + int count; |
| 34 | + int version; |
| 35 | + int dataVersion; |
| 36 | + ItemStack display; |
| 37 | + public CraftItemStack(String item, int count, Map<String, String> compoundTag, int dataVersion, int version) { |
| 38 | + this.item = item; |
| 39 | + this.count = count; |
| 40 | + this.dataVersion = dataVersion; |
| 41 | + this.compoundTag = compoundTag; |
| 42 | + this.display = buildDisplay0(compoundTag); |
| 43 | + } |
| 44 | + |
| 45 | + public static final Codec<ComponentChanges> CODEC_DISPLAY_CHANGES = |
| 46 | + Codec.<ComponentChanges>of( |
| 47 | + ComponentChanges.CODEC, |
| 48 | + Codec.<String, Dynamic<?>>unboundedMap(Codec.STRING, Codec.PASSTHROUGH) |
| 49 | + .map(s -> { |
| 50 | + if (s.isEmpty()) { |
| 51 | + return ComponentChanges.EMPTY; |
| 52 | + } else { |
| 53 | + Reference2ObjectMap<ComponentType<?>, Optional<?>> reference2ObjectMap = new Reference2ObjectArrayMap<>(s.size()); |
| 54 | + |
| 55 | + for (Map.Entry<String, Dynamic<?>> entry : s.entrySet()) { |
| 56 | + String string = entry.getKey(); |
| 57 | + String realKey ; |
| 58 | + boolean removal = false; |
| 59 | + if(string.startsWith("!")){ |
| 60 | + realKey = string.substring(1); |
| 61 | + removal = true; |
| 62 | + }else { |
| 63 | + realKey = string; |
| 64 | + } |
| 65 | + ComponentType<?> type = Registries.DATA_COMPONENT_TYPE.get(Identifier.tryParse(realKey)); |
| 66 | + if(type != null) { |
| 67 | + if(removal){ |
| 68 | + reference2ObjectMap.put(type, Optional.empty()); |
| 69 | + }else { |
| 70 | + Codec<?> codec = type.getCodecOrThrow(); |
| 71 | + codec = VItem.getInstance().getVersionCompatCodecs().getOrDefault(type, codec); |
| 72 | + var dataResult = codec.decode(entry.getValue()); |
| 73 | + if(dataResult.isSuccess()){ |
| 74 | + reference2ObjectMap.put(type, dataResult.result().map(Pair::getFirst)); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return new ComponentChanges(reference2ObjectMap); |
| 80 | + } |
| 81 | + |
| 82 | + }) |
| 83 | + ); |
| 84 | + private ItemStack buildDisplay0(Map<String, String> tag){ |
| 85 | + if (!Objects.equals(item, "minecraft:air")) { |
| 86 | + try{ |
| 87 | + Item item = Registries.ITEM.get(Identifier.tryParse(this.item)); |
| 88 | + // if not air then it is unknown item |
| 89 | + item = item == Items.AIR ? Items.BARRIER : item; |
| 90 | + ItemStack stack = new ItemStack(item, count); |
| 91 | + NbtCompound tagCompound = new NbtCompound(); |
| 92 | + for (var entry : tag.entrySet()) { |
| 93 | + try{ |
| 94 | + final NbtElement componentTag = VNbt.getInstance().readNbtNoRegistry(entry.getValue()); |
| 95 | + tagCompound.put(entry.getKey(), componentTag); |
| 96 | + }catch (Throwable ignoreFormatError){ |
| 97 | + } |
| 98 | + } |
| 99 | + ComponentChanges displayChanges = CODEC_DISPLAY_CHANGES.decode(ItemStackUtils.registry().getOps(NbtOps.INSTANCE), tagCompound).result().map(Pair::getFirst).orElse(ComponentChanges.EMPTY); |
| 100 | + stack.applyChanges(displayChanges); |
| 101 | + |
| 102 | + return stack; |
| 103 | + }catch (Throwable throwable){ |
| 104 | + return new ItemStack(Items.BARRIER); |
| 105 | + } |
| 106 | + |
| 107 | + }else return ItemStack.EMPTY; |
| 108 | + } |
| 109 | + |
| 110 | + public ItemStack buildDisplay(){ |
| 111 | + return display.copy(); |
| 112 | + } |
| 113 | + |
| 114 | + public static CraftItemStack deserializeModern(Map<String, Object> args) { |
| 115 | + final int version = args.getOrDefault("schema_version", 1) instanceof Number val ? val.intValue() : -1; |
| 116 | + //from paper1.21.10 |
| 117 | + String id = "minecraft:air"; int cnt = 0; Map<String, String> compoundTag = Map.of();int dataversion = 0; |
| 118 | + for (var entry : args.entrySet()) { |
| 119 | + switch (entry.getKey()) { |
| 120 | + case "id" -> { |
| 121 | + id = (String) entry.getValue(); |
| 122 | + } |
| 123 | + case "count" -> { |
| 124 | + cnt = ((Number) entry.getValue()).intValue(); |
| 125 | + } |
| 126 | + case "components" -> { |
| 127 | + |
| 128 | + if (entry.getValue() instanceof Map map0) { |
| 129 | + compoundTag = map0; |
| 130 | + } else { |
| 131 | + throw new IllegalArgumentException("components must be a Map"); |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + case "DataVersion" -> { |
| 136 | + dataversion = ((Number) entry.getValue()).intValue(); |
| 137 | + } |
| 138 | + default -> { |
| 139 | + // Ignore |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + return new CraftItemStack(id, cnt, compoundTag, dataversion, version); |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + public Map<String, Object> serialize() { |
| 149 | + final Map<String, Object> ret = new LinkedHashMap<>(); |
| 150 | + ret.put("id", this.item); |
| 151 | + ret.put("count", this.count); |
| 152 | + ret.put("components", this.compoundTag); |
| 153 | + ret.put("DataVersion", this.dataVersion); |
| 154 | + ret.put("schema_version", this.version); |
| 155 | + return ret; |
| 156 | + } |
| 157 | +} |
0 commit comments