EDIT: I've looked at the Strucutre Block class and i found out how to load a structure. But for some reason if i generate a new world and (for example) try to load this structure right-clicking an item it seems like the game can't find the structure (wich is stored in the structures folder into resources). This is the dummy item class i use to "spawn" the structure
public class ItemMW extends Item {
public ItemMW(CreativeTabs tab) {
this.setCreativeTab(tab);
}
@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn,
EnumHand hand) {
if (!worldIn.isRemote)
this.func_189714_c(playerIn.getPosition(), worldIn);
return super.onItemRightClick(itemStackIn, worldIn, playerIn, hand);
}
public boolean func_189714_c(BlockPos pos, World world) {
WorldServer worldserver = (WorldServer) world;
MinecraftServer minecraftserver = world.getMinecraftServer();
TemplateManager templatemanager = worldserver.getStructureTemplateManager();
Template template = templatemanager.func_189942_b(minecraftserver, new ResourceLocation(MW.MODID + ":" + "structures/pagoda.nbt"));
if(template == null)
{
System.out.println("NO STRUCTURE");
return false;
}
BlockPos blockpos2 = template.getSize();
IBlockState iblockstate = world.getBlockState(pos);
world.notifyBlockUpdate(pos, iblockstate, iblockstate, 3);
PlacementSettings placementsettings = (new PlacementSettings()).setMirror(Mirror.NONE)
.setRotation(Rotation.NONE).setIgnoreEntities(false).setChunk((ChunkPos) null)
.setReplacedBlock((Block) null).setIgnoreStructureBlock(false);
template.addBlocksToWorldChunk(world, pos.add(0, 1, 0), placementsettings);
return true;
}
}
The NBT file is stored into resources-assets-mw-structures-pagoda.nbt (mw is the mod id)
EDIT 2: Found out a way to make it works. The problem was (as i suspected) in the ResourceLocation, that for some reason pointed at a wrong location. By changing that i can spawn my structure and also integrating this function into (for example) a biome class i can spawn it randomly in the world using the nbt file
public void loadStructure(BlockPos pos, World world, String name) {
WorldServer worldserver = (WorldServer) world;
MinecraftServer minecraftserver = world.getMinecraftServer();
TemplateManager templatemanager = worldserver.getStructureTemplateManager();
ResourceLocation loc = new ResourceLocation(MW.MODID, name);
Template template = templatemanager.func_189942_b(minecraftserver, loc);
if (template != null) {
IBlockState iblockstate = world.getBlockState(pos);
world.notifyBlockUpdate(pos, iblockstate, iblockstate, 3);
PlacementSettings placementsettings = (new PlacementSettings()).setMirror(Mirror.NONE)
.setRotation(Rotation.NONE).setIgnoreEntities(false).setChunk((ChunkPos) null)
.setReplacedBlock((Block) null).setIgnoreStructureBlock(false);
template.addBlocksToWorldChunk(world, pos.add(0, 1, 0), placementsettings);
}
}