User Tools

Site Tools


tutorial:mixin_accessors

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
tutorial:mixin_accessors [2020/08/29 15:18] – Add how to add ores to the end biomes siglongtutorial:mixin_accessors [2020/08/30 10:15] – Restore old revision siglong
Line 1: Line 1:
-====== Adding Ores to Worlds ====== +====== Mixin Accessors ======
-A lot of mods add their own ores, and you'll need a way to place them in existing biomes for players to find. In this tutorial, we'll look at adding ores to existing biomes. There are 2 steps that are required to add ores to biomes. +
-  * Make a ConfiguredFeatures. This defines how your ore block is spawned. +
-  * Register your feature by using [[tutorial:mixin_introduction|mixin]] into Minecraft class where default features are listed. +
  
-We'll assume you've already created your own ore block at this point. Quartz Ore will serve as our replacement throughout this tutorial. Replace references to Quartz Ore with your ore when appropriate.+===== Introduction ===== 
 +Mixin Accessors allow you to access fields and methods that are not visible (private) or final.
  
-==== Adding to the overworld ==== +==== Accessor ==== 
-In this section, our goal will be spawning the ore in the overworld.+''@Accessor'' allows you to access fields. Suppose we want to access ''itemUseCooldown'' field of ''MinecraftClient'' class.
  
-=== Making ConfiguredFeatures === +=== Getting value from the field === 
-First we need to create a ConfiguredFeatures. Make sure to register your ConfiguredFeature at ''onInitialize''. Feel free to change the values to suit your mod.+<code java> 
 +@Mixin(MinecraftClient.class) 
 +public interface MinecraftClientAccessor { 
 +    @Accessor("itemUseCooldown"
 +    public int getItemUseCooldown(); 
 +
 +</code>
  
-<code java [enable_line_numbers="true"]> +Usage:
-public class ExampleMod implements ModInitializer { +
-  public static ConfiguredFeature<?, ?> ORE_QUARTZ_OVERWORLD = Feature.ORE +
-      .configure(new OreFeatureConfig( +
-        OreFeatureConfig.Rules.BASE_STONE_OVERWORLD,  +
-        Blocks.NETHER_QUARTZ_ORE.getDefaultState(), +
-        9)) // vein size +
-      .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig( +
-        0, // bottom offset +
-        0, // min y level +
-        64))) // max y level +
-      .spreadHorizontally() +
-      .repeat(20); // number of veins per chunk+
  
-  @Override +<code java> 
-  public void onInitialize() +int itemUseCooldown = ((MinecraftClientAccessorMinecraftClient.getInstance()).getItemUseCooldown();
-    Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, new Identifier("tutorial", "ore_quartz_overworld"), ORE_QUARTZ_OVERWORLD); +
-  } +
-}+
 </code> </code>
  
-=== Registering the feature === +=== Setting a value to the field === 
-Vanilla ore features that spawn in the overworld biomes are listed in ''DefaultBiomeFeature.addDefaultOres''. We modify this method to add our ore to the overworld via [[tutorial:mixin_introduction|mixin]]. +<code java> 
- +@Mixin(MinecraftClient.class) 
-<code java [enable_line_numbers="true"]+public interface MinecraftClientAccessor 
-@Mixin(DefaultBiomeFeatures.class) +    @Accessor("itemUseCooldown") 
-public class DefaultBiomeFeaturesMixin +    public void setItemUseCooldown(int itemUseCooldown);
-  @Inject(method = "addDefaultOres(Lnet/minecraft/world/biome/GenerationSettings$Builder;)V", at = @At("TAIL")+
-  private static void addDefaultOres(GenerationSettings.Builder builder, CallbackInfo ci) { +
-    builder.feature(GenerationStep.Feature.UNDERGROUND_ORES, ExampleMod.ORE_QUARTZ_OVERWORLD); +
-  }+
 } }
 </code> </code>
  
-=== Result === +Usage:
-You should see quartz ore spawning in the overworld. You can use fill command to remove stone blocks surrounding you like this''/fill ~-4 0 ~-4 ~4 ~ ~4 minecraft:air replace minecraft:stone''.+
  
-{{tutorial:ores.png?800}}+<code java> 
 +((MinecraftClientAccessor) MinecraftClient.getInstance()).setItemUseCooldown(100); 
 +</code>
  
-==== Adding to the end ==== +==== Invoker ==== 
-In this section, based on the code in the previous section, we will add the ore to the end biomes.+''@Invoker'' allows you to access methods. Suppose we want to invoke ''teleportTo'' method of ''EndermanEntity'' class.
  
-=== Making a ConfiguredFeatures === +<code java> 
-We replace '' OreFeatureConfig.Rules.BASE_STONE_OVERWORLD'' with ''new BlockMatchRuleTest(Blocks.END_STONE)'' because endstone is used as a base block in the end biomes. +@Mixin(EndermanEntity.class
- +public interface EndermanEntityInvoker { 
-<code java [enable_line_numbers="true"]+  @Invoker("teleportTo"
-public class ExampleMod implements ModInitializer { +  public boolean invokeTeleportTo(double xdouble ydouble z);
-  public static ConfiguredFeature<?, ?> ORE_QUARTZ_END = Feature.ORE +
-      .configure(new OreFeatureConfig( +
-        new BlockMatchRuleTest(Blocks.END_STONE), /* We use endstone! */ +
-        Blocks.NETHER_QUARTZ_ORE.getDefaultState(), +
-        9)) +
-      .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig( +
-        0, +
-        0, +
-        64))) +
-      .spreadHorizontally() +
-      .repeat(20); +
- +
-  @Override +
-  public void onInitialize() { +
-    Registry.register(BuiltinRegistries.CONFIGURED_FEATUREnew Identifier("tutorial""ore_quartz_end"), ORE_QUARTZ_END); +
-  }+
 } }
 </code> </code>
  
-=== Registering the feature === +Usage:
-Considering that no ore is generated in the end biomes on vanilla minecraft, ''DefaultBiomeFeature.addDefaultOres'' can't be used for adding an ore to the end biomes. Instead, we inject into ''DefaultBiomeCreator.method_31065'' because this method is used to create every end biomes.+
  
-<code java [enable_line_numbers="true"]+<code java> 
-@Mixin(DefaultBiomeCreator.class) +EndermanEntity enderman = ...; 
-public class DefaultBiomeCreatorMixin { +((EndermanEntityInvokerenderman).invokeTeleportTo(0.0D70.0D0.0D);
-  @Inject(method = "method_31065(Lnet/minecraft/world/biome/GenerationSettings$Builder;)Lnet/minecraft/world/biome/Biome;", at = @At("HEAD")+
-  private static void addEndOres(GenerationSettings.Builder builderCallbackInfoReturnable<Biome> cir) { +
-    builder.feature(GenerationStep.Feature.UNDERGROUND_ORESExampleMod.ORE_QUARTZ_END); +
-  } +
-}+
 </code> </code>
  
tutorial/mixin_accessors.txt · Last modified: 2022/04/29 09:54 by solidblock