src/main/resources/data/xolcore/structures/test_schem.nbt is the location,
full file is below, this is an item for placing structures that I am porting from 1.12, the use function is the function of note here
package xol.core.item.basic;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.Particle;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Rotations;
import net.minecraft.core.Vec3i;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
import xol.core.XolCore;
import xol.core.util.item.XolItemConfig;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Optional;
import java.util.function.Predicate;
public class ItemSchematicPlacer extends ItemBasic {
public static final StructurePlaceSettings settings = (new StructurePlaceSettings()).setIgnoreEntities(true).setMirror(Mirror.NONE).setRotation(Rotation.NONE);
private String schematic = "";
private int xOffset = 0;
private int yOffset = 0;
private int zOffset = 0;
private ArrayList<Integer> heights = null;
private SoundEvent playSound = null;
private Particle particle = null;
public ItemSchematicPlacer(XolItemConfig config, String schem){
super(config);
config.stacksTo(1);
this.schematic = schem;
}
public ItemSchematicPlacer(XolItemConfig config, String schem,int x, int y, int z) {
this(config,schem);
this.xOffset = x;
this.yOffset = y;
this.zOffset = z;
}
public ItemSchematicPlacer(XolItemConfig config, String schem,int x, int y, int z,ArrayList<Integer> heights) {
this(config,schem,x,y,z);
this.heights = heights;
}
public ItemSchematicPlacer(XolItemConfig config, String schem,ArrayList<Integer> heights) {
this(config,schem);
this.heights = heights;
}
public ItemSchematicPlacer(XolItemConfig config, String schem,int x, int y, int z,ArrayList<Integer> heights, SoundEvent sound, Particle particle) {
this(config,schem,x,y,z,heights);
this.playSound = sound;
this.particle = particle;
}
@Override
public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) {
ItemStack stack = pPlayer.getItemInHand(pUsedHand);
if (!pLevel.isClientSide() && !schematic.equals("")){
MinecraftServer mcServer = pLevel.getServer();
ServerLevel serverLevel = pLevel.getServer().getLevel(Level.OVERWORLD);
StructureTemplateManager manager = serverLevel.getStructureManager();
ResourceLocation location = new ResourceLocation(XolCore.modId(), "structures/" +schematic);
Optional<StructureTemplate> templatecheck= manager.get(location);
CompoundTag tag;
try {
ResourceManager resourceManager = mcServer.getResourceManager();
Optional<Resource> rs = resourceManager.getResource(location);
tag = NbtIo.readCompressed(rs.get().open());
} catch (Exception e) {
return super.use(pLevel, pPlayer, pUsedHand);
}
StructureTemplate template = manager.readStructure(tag);
if (template == null){
return super.use(pLevel, pPlayer, pUsedHand);
}
if (!stack.hasTag()){
stack.setTag(new CompoundTag());
}
Direction facing = pPlayer.getDirection();
BlockPos pos = pPlayer.blockPosition();
Rotation rot = Rotation.NONE;
if(stack.getTag().contains("rot")) {
switch(stack.getTag().getInt("rot")) {
case 0:
rot = Rotation.NONE;
break;
case 1:
rot = Rotation.CLOCKWISE_180;
break;
case 2:
rot = Rotation.CLOCKWISE_90;
break;
case 3:
rot = Rotation.COUNTERCLOCKWISE_90;
break;
default:
rot = Rotation.NONE;
}
}
else {
int rotSet;
switch (facing) {
case NORTH:
rot = Rotation.NONE;
rotSet = 0;
break;
case SOUTH:
rot = Rotation.CLOCKWISE_180;
rotSet = 1;
break;
case EAST:
rot = Rotation.CLOCKWISE_90;
rotSet = 2;
break;
case WEST:
rotSet = 3;
rot = Rotation.COUNTERCLOCKWISE_90;
break;
default:
rotSet = 0;
rot = Rotation.NONE;
break;
}
stack.getTag().putInt("rot", rotSet);
}
if (stack.getTag().contains("posX")) {
pos = new BlockPos(stack.getTag().getInt("posX"),stack.getTag().getInt("posY"),stack.getTag().getInt("posZ"));
}else {
pos = pos.offset(xOffset, yOffset, zOffset);
stack.getTag().putInt("posX",pos.getX());
stack.getTag().putInt("posY",pos.getY());
stack.getTag().putInt("posZ",pos.getZ());
}
settings.setRotation(rot);
if (this.heights == null) {
template.placeInWorld(serverLevel.getLevel(),pos,new BlockPos(0,0,0),settings,pLevel.random,0);
if (particle!=null) {
Vec3i size = template.getSize();
BlockPos lowerBound;
BlockPos upperBound;
switch(rot) {
case NONE:
lowerBound = pos;
upperBound = pos.offset(size.getX(),size.getY(),size.getZ());
break;
case CLOCKWISE_180:
lowerBound = pos.offset(-size.getX(),0,-size.getZ());
upperBound = pos.offset(0,size.getY(),0);
break;
case CLOCKWISE_90:
lowerBound = pos.offset(-size.getX(),0,0);
upperBound = pos.offset(0,size.getY(),size.getZ());
break;
case COUNTERCLOCKWISE_90:
lowerBound = pos.offset(0,0,-size.getZ());
upperBound = pos.offset(size.getX(),size.getY(),0);
break;
default:
lowerBound = pos;
upperBound = pos.offset(size.getX(),size.getY(),size.getZ());
break;
}
for (BlockPos templatePos: BlockPos.betweenClosed(lowerBound,upperBound)){
if (pLevel.random.nextInt(10)==0) {
/*
CustomParticleConfig config = new CustomParticleConfig();
config.createInstance()
.setSpread(0.0F, 0.0F, 0.0F)
.setParticle(particle)
.setCount(1)
.setIgnoreRange(true);
ParticleSource.spawnParticle(worldIn, config, templatePos.getX(), templatePos.getY(), templatePos.getZ());*/
}
}
}
if (playSound != null) {
pLevel.playSound(null, pPlayer.blockPosition(), playSound, SoundSource.PLAYERS, 1, 1);
}
}else {
CompoundTag compound = stack.getTag();
int curHeight = compound.getInt("height");
int index = compound.getInt("index");
if (index < this.heights.size()) {
int height = this.heights.get(index);
Vec3i size = template.getSize();
BoundingBox newBounds =new BoundingBox(pos.getX()-size.getX()*2,pos.getY()+curHeight,pos.getZ()-size.getZ()*2,
size.getX()*2+pos.getX(),curHeight+height+pos.getY(),size.getZ()*2+pos.getZ());
settings.setBoundingBox(newBounds);
if (particle!=null) {
BlockPos lowerBound;
BlockPos upperBound;
switch(rot) {
case NONE:
lowerBound = pos;
upperBound = pos.offset(size.getX(),0,size.getZ());
break;
case CLOCKWISE_180:
lowerBound = pos.offset(-size.getX(),0,-size.getZ());
upperBound = pos.offset(0,0,0);
break;
case CLOCKWISE_90:
lowerBound = pos.offset(-size.getX(),0,0);
upperBound = pos.offset(0,0,size.getZ());
break;
case COUNTERCLOCKWISE_90:
lowerBound = pos.offset(0,0,-size.getZ());
upperBound = pos.offset(size.getX(),0,0);
break;
default:
lowerBound = pos;
upperBound = pos.offset(size.getX(),0,size.getZ());
break;
}
for (BlockPos templatePos: BlockPos.betweenClosed(lowerBound.offset(0,curHeight,0),upperBound.offset(0,curHeight+height,0))){
if (pLevel.random.nextInt(30)==0) {
/*
CustomParticleConfig config = new CustomParticleConfig();
config.createInstance()
.setSpread(0.0F, 0.0F, 0.0F)
.setParticle(particle)
.setCount(1)
.setIgnoreRange(true);
ParticleSource.spawnParticle(worldIn, config, templatePos.getX(), templatePos.getY(), templatePos.getZ());*/
}
}
}
template.placeInWorld(serverLevel.getLevel(),pos,new BlockPos(0,0,0),settings,pLevel.random,0);
compound.putInt("height", height+curHeight);
compound.putInt("index", index+1);
}
pLevel.playSound(null, pPlayer.blockPosition(), playSound, SoundSource.PLAYERS, 1, 1);
}
}
return super.use(pLevel, pPlayer, pUsedHand);
}
}