Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package org.spongepowered.common.block;

import static com.google.common.base.Preconditions.checkNotNull;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -260,14 +262,26 @@ public Optional<BlockEntityArchetype> createArchetype() {
throw new UnsupportedOperationException("Not implemented yet, please fix when this is called");
}

@Override
public DataContainer rawData() {
if (this.compound == null) {
return DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
}
return NBTTranslator.INSTANCE.translate(this.compound);
}

@Override
public BlockSnapshot withRawData(final DataView container) throws InvalidDataException {
return BuilderImpl.pooled().buildContent(container).orElseThrow(InvalidDataException::new);
checkNotNull(container, "Raw data cannot be null!");
final BuilderImpl builder = this.createBuilder();
builder.addUnsafeCompound(NBTTranslator.INSTANCE.translate(container));
return builder.build();
}

@Override
public boolean validateRawData(final DataView container) {
return BuilderImpl.pooled().buildContent(container).isPresent();
checkNotNull(container, "Raw data cannot be null!");
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ public BlockEntityType blockEntityType() {
return this.type;
}

@Override
public DataContainer blockEntityData() {
return NBTTranslator.INSTANCE.translateFrom(this.compound);
}

@Override
public Optional<BlockEntity> apply(final ServerLocation location) {
final BlockState currentState = location.block();
Expand Down Expand Up @@ -131,7 +126,7 @@ public DataContainer toContainer() {
.set(Queries.CONTENT_VERSION, this.contentVersion())
.set(Constants.Sponge.BlockEntityArchetype.BLOCK_ENTITY_TYPE, this.type)
.set(Constants.Sponge.BlockEntityArchetype.BLOCK_STATE, this.blockState)
.set(Constants.Sponge.BlockEntityArchetype.BLOCK_ENTITY_DATA, this.blockEntityData())
.set(Constants.Sponge.BlockEntityArchetype.BLOCK_ENTITY_DATA, this.rawData())
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public <V> BlockEntityArchetype.Builder add(final Key<? extends Value<V>> key, f
public BlockEntityArchetype.Builder from(final BlockEntityArchetype value) {
this.type = value.blockEntityType();
this.blockState = value.state();
this.data = value.blockEntityData();
this.data = value.rawData();
return this;
}

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/spongepowered/common/data/AbstractArchetype.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import net.minecraft.nbt.CompoundTag;
import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.data.persistence.DataView;
import org.spongepowered.api.data.persistence.InvalidDataException;
import org.spongepowered.api.world.Archetype;
Expand Down Expand Up @@ -70,20 +71,25 @@ public boolean equals(final Object o) {
this.compound.equals(that.compound);
}

@Override
public DataContainer rawData() {
return NBTTranslator.INSTANCE.translate(this.compound);
}

@Override
public void setRawData(final DataView container) throws InvalidDataException {
checkNotNull(container, "Raw data cannot be null!");
final CompoundTag copy = NBTTranslator.INSTANCE.translate(container);
final boolean valid = this.getValidator().validate(copy);
if (valid) {
this.compound = copy;
final CompoundTag compoundTag = NBTTranslator.INSTANCE.translate(container);
if (this.getValidator().validate(compoundTag)) {
this.compound = compoundTag;
} else {
throw new InvalidDataException("Invalid data for " + this.getValidationType());
}
}

@Override
public boolean validateRawData(final DataView container) {
checkNotNull(container, "Raw data cannot be null!");
return this.getValidator().validate(container);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static WeightedSerializableObject<EntityArchetype> getNextEntity(final B
}

private static void setNextEntity(final SpawnerBlockEntity entity, final WeightedSerializableObject<EntityArchetype> value) {
final CompoundTag compound = NBTTranslator.INSTANCE.translate(value.get().entityData());
final CompoundTag compound = NBTTranslator.INSTANCE.translate(value.get().rawData());
if (!compound.contains(Constants.Entity.ENTITY_TYPE_ID)) {
final ResourceKey key = (ResourceKey) (Object) net.minecraft.world.entity.EntityType.getKey((net.minecraft.world.entity.EntityType<?>) value.get()
.type());
Expand Down Expand Up @@ -155,7 +155,7 @@ private static void setEntities(final BaseSpawnerAccessor logic, final WeightedT
}
final WeightedObject<EntityArchetype> object = (WeightedObject<EntityArchetype>) entry;

final CompoundTag compound = NBTTranslator.INSTANCE.translate(object.get().entityData());
final CompoundTag compound = NBTTranslator.INSTANCE.translate(object.get().rawData());
if (!compound.contains(Constants.Entity.ENTITY_TYPE_ID)) {
final ResourceKey key = (ResourceKey) (Object) net.minecraft.world.entity.EntityType.getKey((net.minecraft.world.entity.EntityType<?>) object
.get().type());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,14 @@ public Optional<Vector3d> getPosition() {

@Override
public DataContainer data$getDataContainer() {
return this.entityData();
return this.rawData();
}

@Override
public void data$setDataContainer(final DataContainer container) {
this.compound = NBTTranslator.INSTANCE.translate(Objects.requireNonNull(container, "DataContainer cannot be null!"));
}

@Override
public DataContainer entityData() {
return NBTTranslator.INSTANCE.translateFrom(this.compound);
}

@Override
public Optional<org.spongepowered.api.entity.Entity> apply(final ServerLocation location) {
if (!PlatformHooks.INSTANCE.getGeneralHooks().onServerThread()) {
Expand Down Expand Up @@ -210,7 +205,7 @@ public DataContainer toContainer() {
return DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED)
.set(Queries.CONTENT_VERSION, this.contentVersion())
.set(Constants.Sponge.EntityArchetype.ENTITY_TYPE, this.type)
.set(Constants.Sponge.EntityArchetype.ENTITY_DATA, this.entityData());
.set(Constants.Sponge.EntityArchetype.ENTITY_DATA, this.rawData());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public SpongeEntityArchetypeBuilder reset() {
@Override
public EntityArchetype.Builder from(final EntityArchetype value) {
this.entityType = value.type();
this.compound = NBTTranslator.INSTANCE.translate(value.entityData());
this.compound = NBTTranslator.INSTANCE.translate(value.rawData());
SpongeEntityArchetypeBuilder.stripCompound(this.compound);
// TODO Copy over the pending manipulator data?
this.manipulator = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package org.spongepowered.common.entity;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import net.minecraft.nbt.CompoundTag;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -155,15 +157,26 @@ public DataContainer toContainer() {
return container;
}

@Override
public DataContainer rawData() {
if (this.compound == null) {
return DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
}
return NBTTranslator.INSTANCE.translate(this.compound);
}

@Override
public boolean validateRawData(final DataView container) {
return new SpongeEntitySnapshotBuilder().buildContent(container).isPresent();
checkNotNull(container, "Raw data cannot be null!");
return true;
}

@Override
public EntitySnapshot withRawData(final DataView container) throws InvalidDataException {
final Optional<EntitySnapshot> snap = new SpongeEntitySnapshotBuilder().buildContent(container);
return snap.orElseThrow(InvalidDataException::new);
checkNotNull(container, "Raw data cannot be null!");
final SpongeEntitySnapshotBuilder builder = this.createBuilder();
builder.unsafeCompound(NBTTranslator.INSTANCE.translate(container));
return builder.build();
}

@Override
Expand Down
37 changes: 14 additions & 23 deletions src/main/java/org/spongepowered/common/fluid/SpongeFluidStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package org.spongepowered.common.fluid;

import static com.google.common.base.Preconditions.checkNotNull;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.ResourceKey;
Expand All @@ -40,9 +42,6 @@
import org.spongepowered.common.data.holder.SpongeMutableDataHolder;
import org.spongepowered.common.util.Constants;

import java.util.Optional;


public class SpongeFluidStack implements FluidStack, SpongeMutableDataHolder {

private FluidType fluidType;
Expand Down Expand Up @@ -89,32 +88,24 @@ public int volume() {
return new SpongeFluidStackSnapshotBuilder().from(this).build();
}

@Override
public DataContainer rawData() {
if (this.extraData == null) {
return DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
}
return extraData.copy(DataView.SafetyMode.NO_DATA_CLONED);
}

@Override
public boolean validateRawData(final DataView container) {
return container.contains(Queries.CONTENT_VERSION, Constants.Fluids.FLUID_TYPE, Constants.Fluids.FLUID_VOLUME);
checkNotNull(container, "Raw data cannot be null!");
return true;
}

@Override
public void setRawData(final @NonNull DataView container) throws InvalidDataException {
try {
final int contentVersion = container.getInt(Queries.CONTENT_VERSION).get();
if (contentVersion != this.contentVersion()) {
throw new InvalidDataException("Older content found! Cannot set raw data of older content!");
}
final String rawFluid = container.getString(Constants.Fluids.FLUID_TYPE).get();
final int volume = container.getInt(Constants.Fluids.FLUID_VOLUME).get();
final Optional<FluidType> fluidType = Sponge.game().registry(RegistryTypes.FLUID_TYPE).findValue(ResourceKey.resolve(rawFluid));
if (!fluidType.isPresent()) {
throw new InvalidDataException("Unknown FluidType found! Requested: " + rawFluid + "but got none.");
}
this.fluidType = fluidType.get();
this.volume = volume;
if (container.contains(Constants.Sponge.UNSAFE_NBT)) {
this.extraData = container.getView(Constants.Sponge.UNSAFE_NBT).get().copy();
}
} catch (final Exception e) {
throw new InvalidDataException("DataContainer contained invalid data!", e);
}
checkNotNull(container, "Raw data cannot be null!");
extraData = container.copy(DataView.SafetyMode.ALL_DATA_CLONED);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package org.spongepowered.common.fluid;

import static com.google.common.base.Preconditions.checkNotNull;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.ResourceKey;
Expand Down Expand Up @@ -121,13 +123,23 @@ public boolean equals(final Object obj) {
return new SpongeFluidStackSnapshot(this.fluidType, this.volume, this.extraData);
}

@Override
public DataContainer rawData() {
if (this.extraData == null) {
return DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
}
return extraData.copy(DataView.SafetyMode.NO_DATA_CLONED);
}

@Override
public boolean validateRawData(final DataView container) {
return container.contains(Queries.CONTENT_VERSION, Constants.Fluids.FLUID_TYPE, Constants.Fluids.FLUID_VOLUME);
checkNotNull(container, "Raw data cannot be null!");
return this.createStack().validateRawData(container);
}

@Override
public @NonNull FluidStackSnapshot withRawData(final @NonNull DataView container) throws InvalidDataException {
checkNotNull(container, "Raw data cannot be null!");
final FluidStack stack = this.createStack();
stack.setRawData(container);
return stack.createSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,17 @@ public void setCreator(final @Nullable UUID uuid) {
}
}

@Override
public DataContainer rawData() {
if (this.compound == null) {
return DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
}
return NBTTranslator.INSTANCE.translate(this.compound);
}

@Override
public ItemStackSnapshot withRawData(DataView container) throws InvalidDataException {
checkNotNull(container, "Raw data cannot be null!");
final ItemStack copy = this.privateStack.copy();
copy.setRawData(container);
return copy.createSnapshot();
Expand All @@ -316,8 +325,8 @@ public ItemStackSnapshot mergeWith(ItemStackSnapshot that, MergeFunction functio

@Override
public boolean validateRawData(DataView container) {
final ItemStack copy = this.privateStack.copy();
return copy.validateRawData(container);
checkNotNull(container, "Raw data cannot be null!");
return this.privateStack.validateRawData(container);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public DataView addTo(final Schematic schematic, final DataView data) {
final DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
final Vector3i pos = entry.getKey();
final BlockEntityArchetype archetype = entry.getValue();
final DataContainer entityData = archetype.blockEntityData();
final DataContainer entityData = archetype.rawData();
final int[] apos = new int[]{pos.x() - xMin, pos.y() - yMin, pos.z() - zMin};
container.set(Constants.Sponge.Schematic.BLOCKENTITY_POS, apos);
container.set(Constants.Sponge.Schematic.BLOCKENTITY_DATA, entityData);
Expand Down Expand Up @@ -528,7 +528,7 @@ public DataView addTo(final Schematic schematic, final DataView data) {
requiredMods.add(key.namespace());
}
container.set(Constants.Sponge.Schematic.ENTITIES_ID, key.toString());
final DataContainer entityData = entry.archetype().entityData();
final DataContainer entityData = entry.archetype().rawData();
container.set(Constants.Sponge.Schematic.BLOCKENTITY_DATA, entityData);
return container;
}).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,14 @@ public Optional<LocatableBlock> without(final Key<?> key) {
return this.blockState.without(key).map(state -> LocatableBlock.builder().from(this).state(state).build());
}

@Override
public DataContainer rawData() {
return this.blockState.rawData();
}

@Override
public LocatableBlock withRawData(final DataView container) throws InvalidDataException {
checkNotNull(container, "Raw data cannot be null!");
return LocatableBlock.builder().from(this).state(this.blockState.withRawData(container)).build();
}

Expand All @@ -168,6 +174,7 @@ public LocatableBlock mergeWith(final LocatableBlock that, final MergeFunction f

@Override
public boolean validateRawData(final DataView container) {
checkNotNull(container, "Raw data cannot be null!");
return this.blockState.validateRawData(container);
}

Expand Down
Loading