Skip to content

Commit 7a03360

Browse files
committed
Clean up
1 parent ff9504d commit 7a03360

4 files changed

Lines changed: 33 additions & 37 deletions

File tree

src/main/java/org/spongepowered/common/item/ItemStackDataComponentsUpdater.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public DataView update(final DataView content) {
5656
final String type = content.getString(Constants.ItemStack.V2.TYPE).get();
5757

5858
final DataContainer updated = DataContainer.createNew();
59-
updated.set(Constants.ItemStack.TYPE, type);
60-
updated.set(Constants.ItemStack.COUNT, count);
59+
updated.set(Constants.ItemStack.V3.TYPE, type);
60+
updated.set(Constants.ItemStack.V3.COUNT, count);
6161

6262
final DataContainer components = DataContainer.createNew();
63-
content.getInt(Constants.ItemStack.V2.DAMAGE_VALUE).filter(dmg -> dmg != 0).ifPresent(dmg -> components.set(Constants.ItemStack.DAMAGE, dmg));
63+
content.getInt(Constants.ItemStack.V2.DAMAGE_VALUE).filter(dmg -> dmg != 0).ifPresent(dmg -> components.set(Constants.ItemStack.V3.DAMAGE, dmg));
6464

6565
content.getView(Constants.Sponge.UNSAFE_NBT).ifPresent(unsafe -> {
6666
// Get unsafe nbt data
@@ -77,7 +77,7 @@ public DataView update(final DataView content) {
7777
newComponents.values(false).forEach(components::set);
7878
});
7979

80-
updated.set(Constants.ItemStack.COMPONENTS, components);
80+
updated.set(Constants.ItemStack.V3.COMPONENTS, components);
8181
updated.set(Queries.CONTENT_VERSION, this.outputVersion());
8282

8383
return updated;

src/main/java/org/spongepowered/common/item/ItemStackDataVersionUpdater.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,12 @@
2424
*/
2525
package org.spongepowered.common.item;
2626

27-
import com.mojang.serialization.Dynamic;
28-
import net.minecraft.SharedConstants;
29-
import net.minecraft.nbt.CompoundTag;
30-
import net.minecraft.nbt.NbtOps;
31-
import net.minecraft.util.datafix.DataFixers;
32-
import net.minecraft.util.datafix.fixes.References;
3327
import org.spongepowered.api.data.persistence.DataContainer;
3428
import org.spongepowered.api.data.persistence.DataContentUpdater;
3529
import org.spongepowered.api.data.persistence.DataView;
3630
import org.spongepowered.api.data.persistence.Queries;
3731
import org.spongepowered.api.item.ItemTypes;
3832
import org.spongepowered.api.registry.RegistryTypes;
39-
import org.spongepowered.common.data.persistence.NBTTranslator;
4033
import org.spongepowered.common.util.Constants;
4134

4235
public final class ItemStackDataVersionUpdater implements DataContentUpdater {
@@ -55,29 +48,25 @@ public int outputVersion() {
5548

5649
@Override
5750
public DataView update(final DataView content) {
58-
final boolean isAir = content.getRegistryValue(Constants.ItemStack.TYPE, RegistryTypes.ITEM_TYPE)
51+
final boolean isAir = content.getRegistryValue(Constants.ItemStack.V3.TYPE, RegistryTypes.ITEM_TYPE)
5952
.map(i -> i == ItemTypes.AIR.get())
6053
.orElse(false);
6154

6255
final DataContainer updated = DataContainer.createNew();
6356
updated.set(Queries.CONTENT_VERSION, this.outputVersion());
64-
updated.set(Constants.ItemStack.DATA_VERSION, SharedConstants.getCurrentVersion().getDataVersion().getVersion());
57+
updated.set(Constants.ItemStack.V4.DATA_VERSION, 3833); // The version from the previous ItemStackDataComponentsUpdater.
6558

6659
if (isAir) {
67-
return updated.set(Constants.ItemStack.DATA, DataContainer.createNew());
60+
return updated.set(Constants.ItemStack.V4.DATA, DataContainer.createNew());
6861
}
6962

70-
final CompoundTag itemStackTag = new CompoundTag();
71-
itemStackTag.putString("id", content.getString(Constants.ItemStack.TYPE).get());
72-
itemStackTag.putInt("count", content.getInt(Constants.ItemStack.COUNT).get());
63+
final DataContainer data = DataContainer.createNew()
64+
.set(Constants.ItemStack.V3.TYPE, content.getString(Constants.ItemStack.V3.TYPE).get())
65+
.set(Constants.ItemStack.V3.COUNT, content.getInt(Constants.ItemStack.V3.COUNT).get());
7366

74-
content.getView(Constants.ItemStack.COMPONENTS).ifPresent(components ->
75-
itemStackTag.put("components", NBTTranslator.INSTANCE.translate(components)));
67+
content.getView(Constants.ItemStack.V3.COMPONENTS)
68+
.ifPresent(components -> data.set(Constants.ItemStack.V3.COMPONENTS, components));
7669

77-
var dataFixed = DataFixers.getDataFixer().update(References.ITEM_STACK, new Dynamic<>(NbtOps.INSTANCE, itemStackTag), 3833, SharedConstants.getCurrentVersion().getDataVersion().getVersion());
78-
79-
updated.set(Constants.ItemStack.DATA, NBTTranslator.INSTANCE.translate((CompoundTag) dataFixed.getValue()));
80-
81-
return updated;
70+
return updated.set(Constants.ItemStack.V4.DATA, data);
8271
}
8372
}

src/main/java/org/spongepowered/common/item/SpongeItemStack.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ public static DataContainer getDataContainer(final net.minecraft.world.item.Item
279279
final CompoundTag stackData = (CompoundTag) mcStack.saveOptional(SpongeCommon.server().registryAccess());
280280
return DataContainer.createNew()
281281
.set(Queries.CONTENT_VERSION, ((ItemStack) (Object) mcStack).contentVersion())
282-
.set(Constants.ItemStack.DATA_VERSION, SharedConstants.getCurrentVersion().getDataVersion().getVersion())
283-
.set(Constants.ItemStack.DATA, NBTTranslator.INSTANCE.translate(stackData));
282+
.set(Constants.ItemStack.V4.DATA_VERSION, SharedConstants.getCurrentVersion().getDataVersion().getVersion())
283+
.set(Constants.ItemStack.V4.DATA, NBTTranslator.INSTANCE.translate(stackData));
284284
}
285285

286286
@NotNull
@@ -328,17 +328,17 @@ public static Optional<ItemStack> createItemStack(final DataView container) {
328328
final DataUpdaterDelegate delegate = new DataUpdaterDelegate(builder.build(), version, Constants.ItemStack.Data.CURRENT_VERSION);
329329
final DataView updatedContainer = delegate.update(container);
330330

331-
if (!updatedContainer.contains(Constants.ItemStack.DATA_VERSION, Constants.ItemStack.DATA)) {
331+
if (!updatedContainer.contains(Constants.ItemStack.V4.DATA_VERSION, Constants.ItemStack.V4.DATA)) {
332332
return Optional.empty();
333333
}
334334

335-
final CompoundTag stackData = updatedContainer.getView(Constants.ItemStack.DATA)
335+
final CompoundTag stackData = updatedContainer.getView(Constants.ItemStack.V4.DATA)
336336
.map(NBTTranslator.INSTANCE::translate)
337337
.orElseThrow(() -> new InvalidDataException("Unable retrieve item stack data"));
338338
if (stackData.isEmpty()) {
339339
return Optional.of(ItemStack.empty());
340340
}
341-
final int dataVersion = updatedContainer.getInt(Constants.ItemStack.DATA_VERSION).get();
341+
final int dataVersion = updatedContainer.getInt(Constants.ItemStack.V4.DATA_VERSION).get();
342342
final Dynamic<Tag> fixedData = DataFixers.getDataFixer().update(
343343
References.ITEM_STACK,
344344
new Dynamic<>(NbtOps.INSTANCE, stackData),

src/main/java/org/spongepowered/common/util/Constants.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,14 +1192,6 @@ public static final class Fluids {
11921192

11931193
public static final class ItemStack {
11941194

1195-
public static final DataQuery DATA = of("Data");
1196-
public static final DataQuery DATA_VERSION = of("DataVersion");
1197-
public static final DataQuery COUNT = of("count");
1198-
public static final DataQuery TYPE = of("id");
1199-
public static final DataQuery COMPONENTS = of("components");
1200-
public static final DataQuery CUSTOM_DATA = of("minecraft:custom_data");
1201-
public static final DataQuery DAMAGE = of("minecraft:damage");
1202-
12031195
@Deprecated
12041196
public static final class V2 {
12051197

@@ -1209,6 +1201,21 @@ public static final class V2 {
12091201
public static final DataQuery TYPE = of("ItemType");
12101202
}
12111203

1204+
@Deprecated
1205+
public static final class V3 {
1206+
1207+
public static final DataQuery COUNT = of("count");
1208+
public static final DataQuery TYPE = of("id");
1209+
public static final DataQuery COMPONENTS = of("components");
1210+
public static final DataQuery DAMAGE = of("minecraft:damage");
1211+
}
1212+
1213+
public static final class V4 {
1214+
1215+
public static final DataQuery DATA = of("Data");
1216+
public static final DataQuery DATA_VERSION = of("DataVersion");
1217+
}
1218+
12121219
// Previously only ItemStackSnapshot
12131220
public static final class Data {
12141221

0 commit comments

Comments
 (0)