下面列出了net.minecraft.util.datafix.FixTypes#net.minecraft.world.gen.structure.template.Template 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider) {
if (ConfigHolder.abandonedBaseRarity == 0 ||
world.getWorldType() == WorldType.FLAT ||
world.provider.getDimensionType() != DimensionType.OVERWORLD ||
!world.getWorldInfo().isMapFeaturesEnabled()) {
return; //do not generate in flat worlds, or in non-surface worlds
}
BlockPos randomPos = new BlockPos(chunkX * 16 + 8, 0, chunkZ * 16 + 8);
if (random.nextInt(ConfigHolder.abandonedBaseRarity) == 0) {
int variantNumber = random.nextInt(3);
Rotation rotation = Rotation.values()[random.nextInt(Rotation.values().length)];
ResourceLocation templateId = new ResourceLocation(GTValues.MODID, "abandoned_base/abandoned_base_1_" + variantNumber);
Template template = TemplateManager.getBuiltinTemplate(world, templateId);
BlockPos originPos = template.getZeroPositionWithTransform(randomPos, Mirror.NONE, rotation);
originPos = TemplateManager.calculateAverageGroundLevel(world, originPos, template.getSize());
template.addBlocksToWorld(world, originPos, new PlacementSettings().setRotation(rotation));
}
}
public static Template getBuiltinTemplate(World world, ResourceLocation templateId) {
if (templateMap.containsKey(templateId)) {
return templateMap.get(templateId);
}
Template template = new Template();
String resourcePath = "/assets/" + templateId.getResourceDomain() + "/structures/" + templateId.getResourcePath() + ".nbt";
InputStream inputStream = TemplateManager.class.getResourceAsStream(resourcePath);
if (inputStream != null) {
try {
NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(inputStream);
if (!nbttagcompound.hasKey("DataVersion", 99)) {
nbttagcompound.setInteger("DataVersion", 500);
}
DataFixer dataFixer = world.getMinecraftServer().getDataFixer();
template.read(dataFixer.process(FixTypes.STRUCTURE, nbttagcompound));
} catch (IOException exception) {
GTLog.logger.error("Failed to load builtin template {}", templateId, exception);
} finally {
IOUtils.closeQuietly(inputStream);
}
} else {
GTLog.logger.warn("Failed to find builtin structure with path {}", resourcePath);
}
templateMap.put(templateId, template);
return template;
}
default boolean buildStructure(World world, BlockPos pos) {
if (world.isRemote) return true;
for (Template.BlockInfo info : getStructure().blockInfos()) {
if (info.blockState == null) continue;
BlockPos realPos = info.pos.add(pos).subtract(getStructure().getOrigin());
IBlockState state = world.getBlockState(realPos);
if (state != info.blockState) {
if (state.getBlock() == ModBlocks.CREATIVE_MANA_BATTERY && info.blockState.getBlock() == ModBlocks.MANA_BATTERY) {
continue;
}
world.setBlockState(realPos, info.blockState);
}
}
return true;
}
@NotNull
@Override
public ComponentBookMark createBookmarkComponent(@NotNull IBookGui book, int bookmarkIndex) {
WizardryStructure structure = ModStructures.structureManager.getStructure(location);
HashMap<List<IBlockState>, Integer> map = new HashMap<>();
if (structure != null)
for (Template.BlockInfo info : structure.blockInfos()) {
if (info.blockState.getBlock() == Blocks.AIR) continue;
List<IBlockState> list = new ArrayList<>();
list.add(info.blockState);
map.put(list, map.getOrDefault(list, 0) + 1);
}
return new ComponentMaterialsBar(book, bookmarkIndex, new StructureMaterials(map));
}
private static Template loadTemplate(InputStream is)
{
if (is == null)
return null;
try
{
NBTTagCompound nbt = CompressedStreamTools.readCompressed(is);
Template template = new Template();
template.read(nbt);
return template;
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (is != null)
IOUtils.closeQuietly(is);
}
return null;
}
@Override
public void generate(World world, BlockPos pos)
{
PlacementSettings settings = new PlacementSettings();
Template temp = null;
String suffix = world.provider.getDimensionType().getSuffix();
String opts = world.getWorldInfo().getGeneratorOptions() + suffix;
if (!Strings.isNullOrEmpty(opts))
temp = StructureUtil.loadTemplate(new ResourceLocation(opts), (WorldServer)world, true);
if (temp == null)
temp = StructureUtil.loadTemplate(new ResourceLocation("/config/", this.fileName + suffix), (WorldServer)world, !Strings.isNullOrEmpty(suffix));
if (temp == null)
return; //If we're not in the overworld, and we don't have a template...
BlockPos spawn = StructureUtil.findSpawn(temp, settings);
if (spawn != null)
{
pos = pos.subtract(spawn);
world.setSpawnPoint(pos);
}
temp.addBlocksToWorld(world, pos, settings, 0); //Push to world, with no neighbor notifications!
world.getPendingBlockUpdates(new StructureBoundingBox(pos, pos.add(temp.getSize())), true); //Remove block updates, so that sand doesn't fall!
}
/**
* Will return a list of blocks that are incorrect. If this list is empty, the structure is complete.
*/
default Set<BlockPos> testStructure(World world, BlockPos pos) {
Set<BlockPos> errors = new HashSet<>();
for (Template.BlockInfo info : getStructure().blockInfos()) {
if (info.blockState == null) continue;
if (info.blockState.getMaterial() == Material.AIR || info.blockState.getBlock() == Blocks.STRUCTURE_VOID)
continue;
BlockPos realPos = info.pos.add(pos).subtract(getStructure().getOrigin());
IBlockState state = world.getBlockState(realPos);
if (state != info.blockState) {
if (state.getBlock() == ModBlocks.CREATIVE_MANA_BATTERY && info.blockState.getBlock() == ModBlocks.MANA_BATTERY) {
continue;
}
if (info.blockState.getBlock() instanceof BlockStairs && state.getBlock() instanceof BlockStairs
&& info.blockState.getBlock() == state.getBlock()
&& info.blockState.getValue(BlockStairs.HALF) == state.getValue(BlockStairs.HALF)
&& info.blockState.getValue(BlockStairs.SHAPE) == state.getValue(BlockStairs.SHAPE)) {
if (info.blockState.getValue(BlockStairs.FACING) != state.getValue(BlockStairs.FACING))
world.setBlockState(realPos, info.blockState);
continue;
}
errors.add(realPos);
}
}
return errors;
}
public static Template loadTemplate(ResourceLocation loc, WorldServer world, boolean allowNull)
{
boolean config = "/config/".equals(loc.getResourceDomain());
File file = new File(YUNoMakeGoodMap.instance.getStructFolder(), loc.getResourcePath() + ".nbt");
if (config && file.exists())
{
try
{
return loadTemplate(new FileInputStream(file));
}
catch (FileNotFoundException e) //literally cant happen but whatever..
{
e.printStackTrace();
return allowNull ? null : getDefault(world);
}
}
else
{
ResourceLocation res = config ? new ResourceLocation(YUNoMakeGoodMap.MODID + ":" + loc.getResourcePath()) : loc;
Template ret = loadTemplate(StructureLoader.class.getResourceAsStream("/assets/" + res.getResourceDomain() + "/structures/" + res.getResourcePath() + ".nbt")); //We're on the server we don't have Resource Packs.
if (ret != null)
return ret;
//Cant find it, lets load the one shipped with this mod.
(new FileNotFoundException(file.toString())).printStackTrace();
return allowNull ? null : getDefault(world);
}
}
public static BlockPos findSpawn(Template temp, PlacementSettings settings)
{
for (Entry<BlockPos, String> e : temp.getDataBlocks(new BlockPos(0,0,0), settings).entrySet())
{
if ("SPAWN_POINT".equals(e.getValue()))
return e.getKey();
}
return null;
}
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
{
if (args.length != 2)
throw new WrongUsageException(getUsage(sender));
EntityPlayer player = getPlayer(server, sender, args[1]);
if (player != null)
{
PlacementSettings settings = new PlacementSettings();
WorldServer world = (WorldServer) sender.getEntityWorld();
int platformNumber = SpawnPlatformSavedData.get(world).addAndGetPlatformNumber();
BlockPos pos = getPositionOfPlatform(world, platformNumber);
Template temp = StructureUtil.loadTemplate(new ResourceLocation(args[0]), world, true);
BlockPos spawn = StructureUtil.findSpawn(temp, settings);
spawn = spawn == null ? pos : spawn.add(pos);
sender.sendMessage(new TextComponentString("Building \"" + args[0] + "\" at " + pos.toString()));
temp.addBlocksToWorld(world, pos, settings, 2); //Push to world, with no neighbor notifications!
world.getPendingBlockUpdates(new StructureBoundingBox(pos, pos.add(temp.getSize())), true); //Remove block updates, so that sand doesn't fall!
if (player instanceof EntityPlayerMP) {
((EntityPlayerMP) player).setPositionAndUpdate(spawn.getX() + 0.5, spawn.getY() + 1.6, spawn.getZ() + 0.5);
}
player.setSpawnChunk(spawn, true, world.provider.getDimension());
}
else
{
throw new WrongUsageException(getUsage(sender));
}
}
private void loadTemplate(TemplateManager manager) {
Template template = manager.getTemplate(null, new ResourceLocation(TofuMain.MODID, "tofucastle/" + this.templateName));
PlacementSettings placementsettings = (new PlacementSettings()).setIgnoreEntities(true).setRotation(this.rotation).setMirror(this.mirror);
this.setup(template, this.templatePosition, placementsettings);
}
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
{
if (args.length < 1)
throw new WrongUsageException(getUsage(sender));
String cmd = args[0].toLowerCase(Locale.ENGLISH);
if ("list".equals(cmd))
{
sender.sendMessage(new TextComponentString("Known Platforms:"));
for (ResourceLocation rl : getPlatforms())
{
sender.sendMessage(new TextComponentString(" " + rl.toString()));
}
}
else if ("spawn".equals(cmd) || "preview".equals(cmd))
{
if (args.length < 2)
throw new WrongUsageException(getUsage(sender));
Entity ent = sender.getCommandSenderEntity();
PlacementSettings settings = new PlacementSettings();
WorldServer world = (WorldServer)sender.getEntityWorld();
if (args.length >= 3)
{
//TODO: Preview doesnt quite work correctly with rotations....
String rot = args[2].toLowerCase(Locale.ENGLISH);
if ("0".equals(rot) || "none".equals(rot))
settings.setRotation(Rotation.NONE);
else if ("90".equals(rot))
settings.setRotation(Rotation.CLOCKWISE_90);
else if ("180".equals(rot))
settings.setRotation(Rotation.CLOCKWISE_180);
else if ("270".equals(rot))
settings.setRotation(Rotation.COUNTERCLOCKWISE_90);
else
throw new WrongUsageException("Only rotations none, 0, 90, 180, and 270 allowed.");
}
BlockPos pos;
if (args.length >= 6)
pos = CommandBase.parseBlockPos(sender, args, 3, false);
else if (ent != null)
pos = ent.getPosition();
else
throw new WrongUsageException("Must specify a position if the command sender is not an entity");
Template temp = StructureUtil.loadTemplate(new ResourceLocation(args[1]), world, true);
BlockPos spawn = StructureUtil.findSpawn(temp, settings);
if (spawn != null)
pos = pos.subtract(spawn);
if ("spawn".equals(cmd))
{
sender.sendMessage(new TextComponentString("Building \"" + args[1] +"\" at " + pos.toString()));
temp.addBlocksToWorld(world, pos, settings, 2); //Push to world, with no neighbor notifications!
world.getPendingBlockUpdates(new StructureBoundingBox(pos, pos.add(temp.getSize())), true); //Remove block updates, so that sand doesn't fall!
}
else
{
BlockPos tpos = pos.down();
if (spawn != null)
tpos = tpos.add(spawn);
sender.sendMessage(new TextComponentString("Previewing \"" + args[1] +"\" at " + pos.toString()));
world.setBlockState(tpos, Blocks.STRUCTURE_BLOCK.getDefaultState().withProperty(BlockStructure.MODE, TileEntityStructure.Mode.LOAD));
TileEntityStructure te = (TileEntityStructure)world.getTileEntity(tpos);
if (spawn != null)
te.setPosition(te.getPosition().subtract(spawn));
te.setSize(temp.getSize());
te.setMode(Mode.LOAD);
te.markDirty();
}
}
else
throw new WrongUsageException(getUsage(sender));
}