Skip to content

Commit 345406e

Browse files
committed
Back up custom attribute modifier ids
1 parent 4a159bf commit 345406e

File tree

1 file changed

+62
-30
lines changed

1 file changed

+62
-30
lines changed

common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/BlockItemPacketRewriter1_21.java

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.viaversion.nbt.tag.ByteTag;
2121
import com.viaversion.nbt.tag.CompoundTag;
22+
import com.viaversion.nbt.tag.ListTag;
2223
import com.viaversion.nbt.tag.StringTag;
2324
import com.viaversion.nbt.tag.Tag;
2425
import com.viaversion.viabackwards.api.rewriters.BackwardsStructuredItemRewriter;
@@ -35,6 +36,9 @@
3536
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
3637
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
3738
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;
3842
import com.viaversion.viaversion.api.minecraft.item.data.Enchantments;
3943
import com.viaversion.viaversion.api.minecraft.item.data.JukeboxPlayable;
4044
import com.viaversion.viaversion.api.type.Types;
@@ -271,51 +275,79 @@ private void backupInconvertibleData(final Item item) {
271275
final StructuredDataContainer data = item.dataContainer();
272276
data.setIdLookup(protocol, true);
273277

278+
final CompoundTag backupTag = new CompoundTag();
279+
274280
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);
277297
}
278298

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);
290306
}
291-
tag.putBoolean("show_in_tooltip", jukeboxPlayable.showInTooltip());
292307

293-
saveTag(createCustomTag(item), tag, "jukebox_playable");
308+
if (!backupTag.isEmpty()) {
309+
saveTag(createCustomTag(item), backupTag, "inconvertible_data");
310+
}
294311
}
295312

296313
private void restoreInconvertibleData(final Item item) {
297314
final StructuredDataContainer data = item.dataContainer();
298315
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)) {
300317
return;
301318
}
302319

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+
}
315349
}
316350

317-
final JukeboxPlayable jukeboxPlayable = new JukeboxPlayable(song, tag.getBoolean("show_in_tooltip"));
318-
data.set(StructuredDataKey.JUKEBOX_PLAYABLE1_21, jukeboxPlayable);
319351
removeCustomTag(data, customData);
320352
}
321353

0 commit comments

Comments
 (0)