Bits and Bobs. And Sherds

This week I worked on a lot of small tasks. Some new features, a bugfix or two, some minor tweaks, and butterfly sherds. It’s nice to work on small things sometimes. It feels like you’ve achieved a lot more than usual.

Client Tracking Range


While playing with the Butterflies Mod installed, I noticed that butterflies would stop moving at a certain distance. It wouldn’t be until you got closer that they would start flying around again. I think what caused this was the clientTrackingRange.

This is the range at which the server will still send entity updates to the client. What this means is that entities outside of this range will not send updates to the client. It follows that in a single player game that this also means the entities will stop updating.

The default value for this is 5 chunks, but for most animals in Minecraft it is set to 10 chunks. So I’ve altered the value for butterflies as well in the EntityTypeRegistry.

    /**
     * Register the butterflies.
     */
    private static RegistryObject<EntityType<? extends Butterfly>> registerButterfly(int butterflyIndex) {

        String registryId = Butterfly.getRegistryId(butterflyIndex);

        return INSTANCE.register(registryId,
                () -> EntityType.Builder.of(Butterfly::new, MobCategory.CREATURE)
                        .sized(0.3f, 0.2f)
                        .clientTrackingRange(10)
                        .build(Butterfly.getRegistryId(butterflyIndex)));
    }

Since chrysalises/eggs don’t update, and you can only see caterpillars when you get close, I decided to leave the range for these entities at 5 chunks. Now we can see butterflies fluttering about from a distance!

Loot Table Fix


Switching from Forge to Neoforge inevitably leads to some bugs. I started playing with the 1.20.4 version of the mod and I noticed that every block was dropping the infested apple. This was obviously a bug, and it was caused by forgetting to update forge to neoforge.

  "type": "butterflies:oak_leaves_loot",
  "conditions": [
    {
      "condition": "forge:loot_table_id",
      "loot_table_id": "minecraft:blocks/oak_leaves"
    }
  ]

The fix is easy, and now the loot tables in the 1.20.4 version of the mod work as designed:

  "type": "butterflies:oak_leaves_loot",
  "conditions": [
    {
      "condition": "neoforge:loot_table_id",
      "loot_table_id": "minecraft:blocks/oak_leaves"
    }
  ]

Wandering Trader


One idea I came up with was to add some rare trades to the Wandering Trader that would allow players to buy some rare butterflies. This is really easy to do – you just need to listen for a WandererTradesEvent rather than a VillagerTradesEvent.

    /**
     * Used to add/modify trades to wandering traders.
     * @param event The event information.
     */
    private void onWandererTrades(WandererTradesEvent event) {
        List<VillagerTrades.ItemListing> rareTrades = event.getRareTrades();

        Collection<ButterflyData> butterflies = ButterflyData.getButterflyDataCollection();

        rareTrades.add(new SellingItemTrade(itemRegistry.getZhuangziBook().get(), 35, 1, 30));

        List<RegistryObject<Item>> bottledButterflies = itemRegistry.getBottledButterflies();

        for (ButterflyData butterfly : butterflies) {
            if (butterfly.type() != ButterflyData.ButterflyType.SPECIAL) {
                int i = butterfly.butterflyIndex();
                if (Objects.requireNonNull(butterfly.rarity()) == ButterflyData.Rarity.RARE) {
                    rareTrades.add(new SellingItemTrade(bottledButterflies.get(i).get(), 32, 1, 30));
                }
            }
        }
    }

Now players have a chance at finding some of the rarer butterflies without having to rely on random spawns in rare biomes.

A Helping Hand


User Andypsl8 surprise me last week with a pull request adding Chinese translation strings to the mod. It’s great to see that people are taking enough interest in the mod to actually want to contribute to it. I’m extremely grateful for this, and now if you want to play with butterflies in Chinese you can!

As soon as I get round to making a new release, of course…

Too Many Infested Apples


In my spare time I will play Minecraft with my mod installed. This is good for two reasons: I get to play the game with the butterflies, which is fun; and I also get to spot problems with the mod that I can fix from time to time.

In this case I was building a log cabin using a nearby tree farm and I ended up with way too many infested apples. I originally envisioned these items to be almost rare, but they still felt like they were a common drop as they were originally implemented. So I’ve decreased the drop rate of infested apples tenfold, which should hopefully make them feel like a rare treat in future versions.

Butterfly Pottery Sherd


To add some flavour to the mod, I wanted to create a Butterfly Pottery Sherd so that players could create pots with butterflies on them. Adding the item is easy, but getting it to be recognised as a pattern that pottery can use takes a few extra steps.

Create the Item

Thankfully, for pottery sherds we don’t need a special item. We can just create it using the base item class, add the model, texture, translation strings, and so on. I won’t go into detail here since I’ve talked about adding items already, but I’ve created an Item Checklist that can be used for adding a new item to Minecraft. The new sherd goes under Ingredients in the Creative Tab, the same as all the other sherds in the game.

Register the Pattern

Adding the item is cool and all, but we won’t be able to add it to the side of a pot without a bit more legwork. We need to register the pattern itself. To do this, we need a new registry. I added a simple registry for decorated pot patterns.

/**
 * Registers pottery patterns.
 */
public class DecoratedPotPatternsRegistry {

    // An instance of a deferred registry we use to register items.
    private final DeferredRegister<String> deferredRegister;

    // The butterfly pot pattern.
    private RegistryObject<String> butterflyPotPattern;

    /**
     * Construction
     * @param modEventBus The event bus to register with.
     */
    public DecoratedPotPatternsRegistry(IEventBus modEventBus) {
        this.deferredRegister = DeferredRegister.create(Registries.DECORATED_POT_PATTERNS, ButterfliesMod.MOD_ID);
        this.deferredRegister.register(modEventBus);
    }

    /**
     * Register the items.
     */
    public void initialise() {
        butterflyPotPattern = deferredRegister.register("butterfly_pottery_pattern", () -> "butterfly_pottery_pattern");
    }

    /**
     * Accessor for butterfly pot pattern.
     * @return The butterfly pot pattern.
     */
    public RegistryObject<String> getButterflyPotPattern() {
        return butterflyPotPattern;
    }
}

Note that the DeferredRegister uses a registry from the Registries class rather than ForgeRegistries as in other classes.

Patterns will need a texture, which you can add in the following location:

\resources\assets\<MOD_ID>\textures\entity\decorated_pot\<PATTERN_ID>.png

This only registers the pattern, however. We also need to link this pattern to the item we created. We need to set this up in response to an FMLCommonSetupEvent:

    /**
     * Common setup event where we register brewing recipes.
     * @param event The event class.
     */
    private void commonSetup(FMLCommonSetupEvent event) {

        // Brewing Monarch Butterflies into poison (snipped).

        // Butterfly Sherd Pattern.
        Map<Item, ResourceKey<String>> itemToPotTextureMap = Maps.newHashMap(DecoratedPotPatterns.ITEM_TO_POT_TEXTURE);
        itemToPotTextureMap.put(itemRegistry.getButterflyPotterySherd().get(),
                                decoratedPotPatternsRegistry.getButterflyPotPattern().getKey());
        DecoratedPotPatterns.ITEM_TO_POT_TEXTURE = itemToPotTextureMap;
    }

There is one problem here, however. ITEM_TO_POT_TEXTURE is marked private and final. To fix that we need to update our accesstransformer.cfg and add a new line to allow us access to this property.

public-f net.minecraft.world.level.block.entity.DecoratedPotPatterns f_271367_ # ITEM_TO_POT_TEXTURE

Tags

We’re almost there. There is one more thing we need to do. The item is now linked to the new pattern, but it cannot be used in crafting recipes. We need to add it to Minecraft’s pottery sherds tags, so we have to create a new file, \resources\data\minecraft\tags\items\decorated_pot_sherds.json:

{
  "values": [
    "butterflies:butterfly_pottery_sherd"
  ]
}

And the Rest

After this, it’s all done. We can now craft decorated pots that have butterflies on them!

I finished this up with a few details. A loot modifier so that they can drop from archeology, as well as a chance for them to appear as a rare trade for Wandering Traders. This gives a couple of ways for players to actually find these new sherds.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.