Posted November 28, 20222 yr Hi there, I was hoping someone may be able to help, I have some fairly straightforward( I think?) code to attempt to read a structure template from an nbt resource. Getting Optional.empty returned from the getResource() call, and I can't figure out why I've triple checked the resource location, it is correct (and it worked when getting from Minecraft.getInstance().getResourceManager().getResource(location) on the client side, but I need this to work server side) Is there something im missing in terms of getting the server side Resource Manager? I previously tried what used to be the method when I was working in 1.12.2 which was using the StructureTemplateManager.get(location) to directly load the template from the resource location but this also returned Optional.empty for me. MinecraftServer mcServer = pLevel.getServer(); ServerLevel serverLevel = pLevel.getServer().getLevel(Level.OVERWORLD); StructureTemplateManager manager = serverLevel.getStructureManager(); ResourceLocation location = new ResourceLocation(XolCore.modId() + ":structures/" +schematic); 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); } Edited November 28, 20222 yr by TheRealJaws Query is solved
November 28, 20222 yr Quote and it worked when getting from Minecraft.getInstance().getResourceManager().getResource(location) on the client side, but I need this to work server side If it works on the client side that means the file must be in the assets folder? i.e. part of the resource pack. Structures should be in the data folder, the data pack. Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
November 28, 20222 yr Author On 11/28/2022 at 10:05 PM, warjort said: If it works on the client side that means the file must be in the assets folder? i.e. part of the resource pack. Structures should be in the data folder, the data pack. Yes I realized this (I was working with 1.12 before) and I moved it into the data folder already, still having the same issue. Any clue? I'm pretty lost on this, thanks for you reply!
November 28, 20222 yr Author The exact path I have is resources/xolcore/structures/test_schem.nbt xolcore is the modid/namespace, "test_schem" is being passed in as the schematic String
November 28, 20222 yr You need to show exactly what you are doing. All we have is a random code snippet that shows none of the relevant information. i.e. what the ResourceLocation actually is and what your file strcuture looks like. Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
November 28, 20222 yr src/main/resources/data/etc. Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
November 28, 20222 yr Author 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); } }
November 28, 20222 yr Author Please let me know if any other information is needed, item reg entry is this public static final RegistryObject<Item> TEST_SCHEMATIC = Register.item("test_schematic", () -> new ItemSchematicPlacer(new XolItemConfig(),"test_schem", 0,0,0,new ArrayList<Integer>(Arrays.asList(2,4,6,8,10))));
November 28, 20222 yr If you look at StructureTemplateManager.loadFromResource() it adds structures/ on the front and .nbt on the end of the ResourceLocation's path. So for the 2 apis I think your ResourceLocation should be either ResourceManager xolcore:structures/test_schem.nbt StructureTemplateManager xolcore:test_schem Edited November 28, 20222 yr by warjort Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
November 28, 20222 yr Author On 11/28/2022 at 10:38 PM, warjort said: If you look at StructureTemplateManager.loadFromResource() it adds structures/ on the front and .nbt on the end of the ResourceLocation's path. So for the 2 apis I think your ResourceLocation should be either ResourceManager xolcore:structures/test_schem.nbt StructureTemplateManager xolcore:test_schem Wow thanks (I feel stupid), the first way works, wasnt able to get it to work using StructureTemplateManager but thats okay. Cheers, Jaws
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.