|
19 | 19 |
|
20 | 20 | import com.viaversion.nbt.tag.ByteTag;
|
21 | 21 | import com.viaversion.nbt.tag.CompoundTag;
|
| 22 | +import com.viaversion.nbt.tag.ListTag; |
22 | 23 | import com.viaversion.nbt.tag.StringTag;
|
23 | 24 | import com.viaversion.nbt.tag.Tag;
|
24 | 25 | import com.viaversion.viabackwards.api.rewriters.BackwardsStructuredItemRewriter;
|
|
35 | 36 | import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
|
36 | 37 | import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
|
37 | 38 | import com.viaversion.viaversion.api.minecraft.item.Item;
|
| 39 | +import com.viaversion.viaversion.api.minecraft.item.data.AttributeModifiers1_21; |
| 40 | +import com.viaversion.viaversion.api.minecraft.item.data.AttributeModifiers1_21.AttributeModifier; |
| 41 | +import com.viaversion.viaversion.api.minecraft.item.data.AttributeModifiers1_21.ModifierData; |
38 | 42 | import com.viaversion.viaversion.api.minecraft.item.data.Enchantments;
|
39 | 43 | import com.viaversion.viaversion.api.minecraft.item.data.JukeboxPlayable;
|
40 | 44 | import com.viaversion.viaversion.api.type.Types;
|
@@ -271,51 +275,79 @@ private void backupInconvertibleData(final Item item) {
|
271 | 275 | final StructuredDataContainer data = item.dataContainer();
|
272 | 276 | data.setIdLookup(protocol, true);
|
273 | 277 |
|
| 278 | + final CompoundTag backupTag = new CompoundTag(); |
| 279 | + |
274 | 280 | final JukeboxPlayable jukeboxPlayable = data.get(StructuredDataKey.JUKEBOX_PLAYABLE1_21);
|
275 |
| - if (jukeboxPlayable == null) { |
276 |
| - return; |
| 281 | + if (jukeboxPlayable != null) { |
| 282 | + final CompoundTag tag = new CompoundTag(); |
| 283 | + if (jukeboxPlayable.song().hasHolder()) { |
| 284 | + final Holder<JukeboxPlayable.JukeboxSong> songHolder = jukeboxPlayable.song().holder(); |
| 285 | + tag.put("song", holderToTag(songHolder, (song, songTag) -> { |
| 286 | + saveSoundEventHolder(songTag, song.soundEvent()); |
| 287 | + songTag.put("description", song.description()); |
| 288 | + songTag.putFloat("length_in_seconds", song.lengthInSeconds()); |
| 289 | + songTag.putInt("comparator_output", song.comparatorOutput()); |
| 290 | + })); |
| 291 | + } else { |
| 292 | + tag.putString("song_identifier", jukeboxPlayable.song().key()); |
| 293 | + } |
| 294 | + tag.putBoolean("show_in_tooltip", jukeboxPlayable.showInTooltip()); |
| 295 | + |
| 296 | + backupTag.put("jukebox_playable", tag); |
277 | 297 | }
|
278 | 298 |
|
279 |
| - final CompoundTag tag = new CompoundTag(); |
280 |
| - if (jukeboxPlayable.song().hasHolder()) { |
281 |
| - final Holder<JukeboxPlayable.JukeboxSong> songHolder = jukeboxPlayable.song().holder(); |
282 |
| - tag.put("song", holderToTag(songHolder, (song, songTag) -> { |
283 |
| - saveSoundEventHolder(songTag, song.soundEvent()); |
284 |
| - songTag.put("description", song.description()); |
285 |
| - songTag.putFloat("length_in_seconds", song.lengthInSeconds()); |
286 |
| - songTag.putInt("comparator_output", song.comparatorOutput()); |
287 |
| - })); |
288 |
| - } else { |
289 |
| - tag.putString("song_identifier", jukeboxPlayable.song().key()); |
| 299 | + final AttributeModifiers1_21 attributeModifiers = data.get(StructuredDataKey.ATTRIBUTE_MODIFIERS1_21); |
| 300 | + if (attributeModifiers != null) { |
| 301 | + final ListTag<StringTag> attributeIds = new ListTag<>(StringTag.class); |
| 302 | + for (final AttributeModifier modifier : attributeModifiers.modifiers()) { |
| 303 | + attributeIds.add(new StringTag(modifier.modifier().id())); |
| 304 | + } |
| 305 | + backupTag.put("attribute_modifiers", attributeIds); |
290 | 306 | }
|
291 |
| - tag.putBoolean("show_in_tooltip", jukeboxPlayable.showInTooltip()); |
292 | 307 |
|
293 |
| - saveTag(createCustomTag(item), tag, "jukebox_playable"); |
| 308 | + if (!backupTag.isEmpty()) { |
| 309 | + saveTag(createCustomTag(item), backupTag, "inconvertible_data"); |
| 310 | + } |
294 | 311 | }
|
295 | 312 |
|
296 | 313 | private void restoreInconvertibleData(final Item item) {
|
297 | 314 | final StructuredDataContainer data = item.dataContainer();
|
298 | 315 | final CompoundTag customData = data.get(StructuredDataKey.CUSTOM_DATA);
|
299 |
| - if (customData == null || !(customData.remove(nbtTagName("jukebox_playable")) instanceof CompoundTag tag)) { |
| 316 | + if (customData == null || !(customData.remove(nbtTagName("inconvertible_data")) instanceof CompoundTag tag)) { |
300 | 317 | return;
|
301 | 318 | }
|
302 | 319 |
|
303 |
| - final EitherHolder<JukeboxPlayable.JukeboxSong> song; |
304 |
| - final String songIdentifier = tag.getString("song_identifier"); |
305 |
| - if (songIdentifier != null) { |
306 |
| - song = EitherHolder.of(songIdentifier); |
307 |
| - } else { |
308 |
| - song = EitherHolder.of(restoreHolder(tag, "song", songTag -> { |
309 |
| - final Holder<SoundEvent> soundEvent = restoreSoundEventHolder(songTag); |
310 |
| - final Tag description = songTag.get("description"); |
311 |
| - final float lengthInSeconds = songTag.getFloat("length_in_seconds"); |
312 |
| - final int comparatorOutput = songTag.getInt("comparator_output"); |
313 |
| - return new JukeboxPlayable.JukeboxSong(soundEvent, description, lengthInSeconds, comparatorOutput); |
314 |
| - })); |
| 320 | + final CompoundTag jukeboxPlayableTag = tag.getCompoundTag("jukebox_playable"); |
| 321 | + if (jukeboxPlayableTag != null) { |
| 322 | + final EitherHolder<JukeboxPlayable.JukeboxSong> song; |
| 323 | + final String songIdentifier = tag.getString("song_identifier"); |
| 324 | + if (songIdentifier != null) { |
| 325 | + song = EitherHolder.of(songIdentifier); |
| 326 | + } else { |
| 327 | + song = EitherHolder.of(restoreHolder(tag, "song", songTag -> { |
| 328 | + final Holder<SoundEvent> soundEvent = restoreSoundEventHolder(songTag); |
| 329 | + final Tag description = songTag.get("description"); |
| 330 | + final float lengthInSeconds = songTag.getFloat("length_in_seconds"); |
| 331 | + final int comparatorOutput = songTag.getInt("comparator_output"); |
| 332 | + return new JukeboxPlayable.JukeboxSong(soundEvent, description, lengthInSeconds, comparatorOutput); |
| 333 | + })); |
| 334 | + } |
| 335 | + |
| 336 | + final JukeboxPlayable jukeboxPlayable = new JukeboxPlayable(song, tag.getBoolean("show_in_tooltip")); |
| 337 | + data.set(StructuredDataKey.JUKEBOX_PLAYABLE1_21, jukeboxPlayable); |
| 338 | + } |
| 339 | + |
| 340 | + final ListTag<StringTag> attributeIds = tag.getListTag("attribute_modifiers", StringTag.class); |
| 341 | + final AttributeModifiers1_21 attributeModifiers = data.get(StructuredDataKey.ATTRIBUTE_MODIFIERS1_21); |
| 342 | + if (attributeIds != null && attributeModifiers != null && attributeIds.size() == attributeModifiers.modifiers().length) { |
| 343 | + for (int i = 0; i < attributeIds.size(); i++) { |
| 344 | + final String id = attributeIds.get(i).getValue(); |
| 345 | + final AttributeModifier modifier = attributeModifiers.modifiers()[i]; |
| 346 | + final ModifierData updatedModifierData = new ModifierData(id, modifier.modifier().amount(), modifier.modifier().operation()); |
| 347 | + attributeModifiers.modifiers()[i] = new AttributeModifier(modifier.attribute(), updatedModifierData, modifier.slotType()); |
| 348 | + } |
315 | 349 | }
|
316 | 350 |
|
317 |
| - final JukeboxPlayable jukeboxPlayable = new JukeboxPlayable(song, tag.getBoolean("show_in_tooltip")); |
318 |
| - data.set(StructuredDataKey.JUKEBOX_PLAYABLE1_21, jukeboxPlayable); |
319 | 351 | removeCustomTag(data, customData);
|
320 | 352 | }
|
321 | 353 |
|
|
0 commit comments