User Tools

Site Tools


Sidebar

Setup

Basics

Items and Item Groups

Blocks and Block Entities

Entities

World Generation

Miscellaneous

Advanced

Documentation

Examples


Fabric Сontributors

If you'd like to contribute to Fabric, you might be interested in these links:


Extremely Strange People


Wiki Meta

  • Wiki Meta - Starting point for contributing to the wiki
  • Wiki Agenda - See what is on the current agenda, and what other contributors are currently working on.
tutorial:locate

Locate

While testing new StructureFeature additions, it's nice to be able to find it quickly without pumping up the rarity to every chunk. Thankfully, vanilla has a mechanic for doing this: /locate!

To add your feature to the locate command, you'll need

  • a StructureFeature– normal Feature additions will not work.
  • a mixin to LocateCommand
  • to add your feature to the Feature.STRUCTURES list

Mixin Time

Create a mixin to LocateCommand. You'll need to inject into register at RETURN and add in your feature:

@Inject(method = "register", at = @At(value = "RETURN"))
private static void onRegister(CommandDispatcher<ServerCommandSource> dispatcher, CallbackInfo info) {
    dispatcher.register(literal("locate").requires(source -> source.hasPermissionLevel(2))
          .then(literal("TutorialStructure").executes(ctx -> execute(ctx.getSource(), "Tutorial_Jigsaw"))));
}

We're tagging onto the existing /locate command here. “TutorialStructure” is the name that appears in the locate command suggestion list, as well as the name you'll need to type in to find your structure. “Tutorial_Jigsaw” is essentially the locate ID of your structure. It'll have to match another string we use next.

Note that:

  • the literal label can have no spaces
  • the execute string is converted to lowercase for comparison and can have spaces
  • the literal label does not need to be the same as the execute string

The finalized mixin class:

  1. import com.mojang.brigadier.CommandDispatcher;
  2. import net.minecraft.server.command.LocateCommand;
  3. import net.minecraft.server.command.ServerCommandSource;
  4. import org.spongepowered.asm.mixin.Mixin;
  5. import org.spongepowered.asm.mixin.Shadow;
  6. import org.spongepowered.asm.mixin.injection.At;
  7. import org.spongepowered.asm.mixin.injection.Inject;
  8. import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
  9.  
  10. import static net.minecraft.server.command.CommandManager.literal;
  11.  
  12. @Mixin(LocateCommand.class)
  13. public abstract class LocateCommandMixin {
  14.  
  15. private LocateCommandMixin() {
  16. // NO-OP
  17. }
  18.  
  19. @Shadow
  20. private static int execute(ServerCommandSource source, String name) {
  21. throw new AssertionError();
  22. }
  23.  
  24. @Inject(method = "register", at = @At(value = "RETURN"))
  25. private static void onRegister(CommandDispatcher<ServerCommandSource> dispatcher, CallbackInfo info) {
  26. dispatcher.register(literal("locate").requires(source -> source.hasPermissionLevel(2))
  27. .then(literal("TutorialStructure").executes(ctx -> execute(ctx.getSource(), "Tutorial_Jigsaw"))));
  28. }
  29. }

Adding to STRUCTURES

Vanilla Minecraft has a hardcoded list of locatable Structure Features in the Feature class. Fun, right? Thankfully it's public, so we can add to it directly:

Feature.STRUCTURES.put("tutorial_jigsaw", FEATURE);

“tutorial_jigsaw” should match the lowercased version of the ID we used in the previous section. FEATURE is an instance of your StructureFeature registered from the FEATURE registry. An example would be like so:

public static final StructureFeature<DefaultFeatureConfig> FEATURE = Registry.register(
	Registry.FEATURE,
	new Identifier("tutorial", "example_feature"),
	new ExampleFeature(DefaultFeatureConfig::deserialize)
);

Finished!

Go in game, and type /locate TutorialStructure. Assuming your structure was properly generated, you'll get a message in chat leading you to the closest one.

tutorial/locate.txt · Last modified: 2019/10/20 23:17 by draylar