Protection API

Keep stacks your mod protects out of easy place, the printer and what they borrow.

For a favorites or lock mod. You say which of the player's slots hold a stack BuildPrint must leave alone, and easy place, the printer, easy break, the excavator and container filling never place from it, use it up, dig with it or move it. Per slot, not per item, so two stacks of one item can be treated differently. From BuildPrint 0.1.41.

Java
// Once, from your client initializer, when BuildPrint is loaded.
BuildprintProtection.register("mymod", slot ->
        MyFavorites.isProtected(slot) ? BuildprintProtection.KEEP : null);

// No longer needed.
BuildprintProtection.release("mymod");

Levels #

AnswerWhat BuildPrint does with the slot
"KEEP"Never places from it, uses it up, digs or works with it, takes it to fill a container, or moves it into or out of the hotbar.
nullNothing: BuildPrint uses the stack as usual.

Your answer can follow the stack #

BuildPrint asks again each time it looks, so the answer can depend on the stack as it is now.

  • To keep the last item of a stack, answer KEEP once the stack is down to one.
  • To spare a tool that is about to break, answer KEEP for its slot while it is.
  • A level BuildPrint does not know counts as null, so later versions can add finer ones without stopping an older BuildPrint. Case does not matter.
Keep the last item, spare tools about to breakJava
BuildprintProtection.register("mymod", slot -> {
    ItemStack stack = Minecraft.getInstance().player.getInventory().getItem(slot);
    if (!MyFavorites.isProtected(slot)) return null;
    if (stack.isDamageableItem() && stack.getMaxDamage() - stack.getDamageValue() <= 10) return "KEEP";
    return stack.getCount() <= 1 ? "KEEP" : null;
});

Good to know #

  • Slots are the player's Inventory indices: 0 to 8 the hotbar, 9 to 35 the main inventory. BuildPrint only builds from those, so it does not ask about the armor or the offhand.
  • The answer is asked often, on the client thread, several times a tick while the printer runs. Keep it to a lookup.
  • One answer per owner, your mod id: a second call replaces the first. When two mods answer for one slot, either one keeping it is enough.
  • When your answer throws, BuildPrint logs it once and leaves your mod out until you register again.
  • When a block is only carried in stacks you keep, the line above the player's hotbar names your mod as the reason, so they know where to look.
  • Only plain Java types, so you can call it by reflection. A missing class means a BuildPrint older than 0.1.41.

With Sorted #

What you register with Sorted's SortedProtection means "keep it where it is", so BuildPrint does not move such a stack, but it still places from it where it sits in the hotbar. Register here too for "don't build with it". The two are independent; register with both when your mod means both.

Reference #

BuildprintProtectionWhat it does
register(String owner, IntFunction<String> inventorySlot)Protect slots: the function gets an inventory index (0 to 35) and returns KEEP or null. Null instead of a function removes yours.
release(String owner)Take away what that owner registered.
keeps(int inventorySlot)Whether any mod keeps that slot from BuildPrint now.
keeper(int inventorySlot)The id of the mod that keeps it, or null. With two, the one that registered first.
isAvailable()In a world. Registering works either way.

Something missing or wrong on this page? Say so on Discord.