diff --git a/src/main/java/com/simibubi/create/content/schematics/SchematicProcessor.java b/src/main/java/com/simibubi/create/content/schematics/SchematicProcessor.java new file mode 100644 index 000000000..f6d1a61d3 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/schematics/SchematicProcessor.java @@ -0,0 +1,59 @@ +package com.simibubi.create.content.schematics; + +import com.mojang.datafixers.Dynamic; +import com.mojang.datafixers.types.DynamicOps; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IWorldReader; +import net.minecraft.world.World; +import net.minecraft.world.gen.feature.template.IStructureProcessorType; +import net.minecraft.world.gen.feature.template.PlacementSettings; +import net.minecraft.world.gen.feature.template.StructureProcessor; +import net.minecraft.world.gen.feature.template.Template; + +import javax.annotation.Nullable; +import java.util.Optional; + +public class SchematicProcessor extends StructureProcessor { + public static final SchematicProcessor INSTANCE = new SchematicProcessor(); + + @Nullable + @Override + public Template.BlockInfo process(IWorldReader world, BlockPos pos, Template.BlockInfo rawInfo, Template.BlockInfo info, PlacementSettings settings, @Nullable Template template) { + if (info.nbt != null) { + TileEntity te = info.state.createTileEntity(world); + if (te != null && te.onlyOpsCanSetNbt()) { + return new Template.BlockInfo(info.pos, info.state, null); + } + } + return info; + } + + @Nullable + @Override + public Template.EntityInfo processEntity(IWorldReader world, BlockPos pos, Template.EntityInfo rawInfo, Template.EntityInfo info, PlacementSettings settings, Template template) { + return EntityType.readEntityType(info.nbt) + .flatMap(type -> { + if (world instanceof World) { + Entity e = type.create((World) world); + if (e != null && !e.ignoreItemEntityData()) { + return Optional.of(info); + } + } + return Optional.empty(); + }) + .orElse(null); + } + + @Override + protected IStructureProcessorType getType() { + return dynamic -> INSTANCE; + } + + @Override + protected Dynamic serialize0(DynamicOps ops) { + return new Dynamic<>(ops, ops.emptyMap()); + } +} diff --git a/src/main/java/com/simibubi/create/content/schematics/item/SchematicItem.java b/src/main/java/com/simibubi/create/content/schematics/item/SchematicItem.java index 2ddf14b10..ac7771002 100644 --- a/src/main/java/com/simibubi/create/content/schematics/item/SchematicItem.java +++ b/src/main/java/com/simibubi/create/content/schematics/item/SchematicItem.java @@ -12,6 +12,7 @@ import javax.annotation.Nonnull; import org.apache.commons.io.IOUtils; import com.simibubi.create.AllItems; +import com.simibubi.create.content.schematics.SchematicProcessor; import com.simibubi.create.content.schematics.client.SchematicEditScreen; import com.simibubi.create.foundation.gui.ScreenOpener; import com.simibubi.create.foundation.utility.Lang; @@ -94,6 +95,7 @@ public class SchematicItem extends Item { PlacementSettings settings = new PlacementSettings(); settings.setRotation(Rotation.valueOf(tag.getString("Rotation"))); settings.setMirror(Mirror.valueOf(tag.getString("Mirror"))); + settings.addProcessor(SchematicProcessor.INSTANCE); return settings; }