Skip to content

Instantly share code, notes, and snippets.

@Ivorforce
Created June 5, 2017 23:26
Show Gist options
  • Save Ivorforce/601d4d698c5f7734dea08d85774b776e to your computer and use it in GitHub Desktop.
Save Ivorforce/601d4d698c5f7734dea08d85774b776e to your computer and use it in GitHub Desktop.
public class CommandGenerateStructure extends CommandBase
{
@Nonnull
@Override
public String getName()
{
return RCConfig.commandPrefix + "gen";
}
public int getRequiredPermissionLevel()
{
return 2;
}
@Nonnull
@Override
@ParametersAreNonnullByDefault
public String getUsage(ICommandSender var1)
{
return ServerTranslations.usage("commands.strucGen.usage");
}
@Override
@ParametersAreNonnullByDefault
public void execute(MinecraftServer server, ICommandSender commandSender, String[] args) throws CommandException
{
if (args.length <= 0)
throw ServerTranslations.wrongUsageException("commands.strucGen.usage");
String structureName = args[0];
Structure<?> structure = StructureRegistry.INSTANCE.get(structureName);
WorldServer world = RCCommands.tryParseDimension(commandSender, args, 3);
if (structure == null)
throw ServerTranslations.commandException("commands.strucGen.noStructure", structureName);
BlockSurfacePos pos;
pos = RCCommands.tryParseSurfaceBlockPos(commandSender, args, 1, false);
GenerationType generationType;
if (args.length > 4)
generationType = structure.generationType(args[4]);
else
generationType = structure.<GenerationType>generationTypes(NaturalGeneration.class).stream()
.findFirst().orElse(structure.generationTypes(GenerationType.class).stream().findFirst().orElse(null));
Placer placer = generationType.placer();
if (structure instanceof GenericStructure)
{
GenericStructure genericStructureInfo = (GenericStructure) structure;
StructureGenerator<GenericStructure.InstanceData> generator = new StructureGenerator<>(genericStructureInfo).world(world)
.randomPosition(pos, placer).fromCenter(true);
Optional<BlockPos> lowerCoord = generator.lowerCoord();
if (lowerCoord.isPresent())
OperationRegistry.queueOperation(new OperationGenerateStructure(genericStructureInfo, generationType.id(), generator.transform(), lowerCoord.get(), false).withStructureID(structureName).prepare(world), commandSender);
else
throw ServerTranslations.commandException("commands.strucGen.noPlace");
}
else
{
if (new StructureGenerator<>(structure).world(world).generationInfo(generationType)
.structureID(structureName).randomPosition(pos, placer).fromCenter(true).generate() == null)
throw ServerTranslations.commandException("commands.strucGen.noPlace");
}
}
@Nonnull
@Override
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos pos)
{
if (args.length == 2 || args.length == 3)
return getTabCompletionCoordinateXZ(args, 0, pos);
if (args.length == 1)
return getListOfStringsMatchingLastWord(args, StructureRegistry.INSTANCE.ids());
else if (args.length == 4)
return RCCommands.completeDimension(args);
else if (args.length == 5)
{
String structureName = args[0];
Structure<?> structure = StructureRegistry.INSTANCE.get(structureName);
if (structure instanceof GenericStructure)
return getListOfStringsMatchingLastWord(args, structure.generationTypes(GenerationType.class).stream().map(GenerationType::id).collect(Collectors.toList()));
}
// else if (args.length == 6)
// return getListOfStringsMatchingLastWord(args, "0", "2", "5");
// else if (args.length == 7)
// return getListOfStringsMatchingLastWord(args, "fade", "up", "down", "fog");
return Collections.emptyList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment