Skip to content

Items and entities

These types answer questions about items, entities, and the level they are in, drop items, mark an entity, and give a machine a player to act as, in place of the vanilla and loader calls that do so: enchantments, food, and item entities change shape in later Minecraft versions, each loader makes its own fake players, and a mark set through Stope keeps a mod’s code off the entity’s own methods.

Type Summary Status Since Side Thread
ArmorStats The armor, toughness, and knockback resistance a stack adds in one equipment slot. Experimental 2.11.0 both any
FakePlayers Fake players: server players a machine acts as. Experimental 2.10.0 server server
ItemDrops Item drops at entities. Experimental 2.10.0 server server
ItemEntities Item entity lookups. Experimental 2.10.0 server server
ItemKind A kind of item, asked about with ItemTraits#isKind. Experimental 2.11.0 both any
ItemTraits Traits of an item stack. Experimental 2.10.0 both any
LevelEntities The entities a level ticks. Experimental 2.10.0 server server
LevelReads Whether mobs may change the world, and whether it rains at a position. Experimental 2.10.0 both any
ScoreboardTags An entity’s scoreboard tags: the strings /tag edits. Experimental 2.10.0 server server

With villager, a villager that just died, in a MobKilledCallback listener:

Drop a dead villager's food instead of losing it
ItemDrops.dropFromInventory(villager, stack ->
!ItemTraits.hasVanishingCurse(stack) && (ItemTraits.isFood(stack) || ItemTraits.isPlant(stack)));

ArmorStats: Experimental · Since 2.11.0 · Side: both · Thread: any

ItemKind: Experimental · Since 2.11.0 · Side: both · Thread: any

ItemTraits: Experimental · Since 2.10.0 · Side: both · Thread: any

ItemTraits answers the questions a mod sorts, matches, and compares stacks by, from a stack’s item and its stored data. With worn and found, two stacks:

Decide whether a found armor piece beats the one worn
Optional<EquipmentSlot> slot = ItemTraits.wornSlot(found);
if (slot.isEmpty() || ItemTraits.hasBindingCurse(found)) return false;
ArmorStats now = ItemTraits.armorStats(worn, slot.get());
ArmorStats candidate = ItemTraits.armorStats(found, slot.get());
if (candidate.armor() != now.armor()) return candidate.armor() > now.armor();
// Equal armor: the piece with less of its life used up wins.
return ItemTraits.damage(found) * ItemTraits.maxDamage(worn) < ItemTraits.damage(worn) * ItemTraits.maxDamage(found);

Use ItemTraits#isKind where a mod would test the item’s class, and compare two stacks’ ItemTraits#dataKey to match them whatever their wear. With held and spare, two stacks:

Decide whether a spare tool can replace the one held
boolean samePickaxe = ItemTraits.isKind(held, ItemKind.PICKAXE)
&& ItemStack.isSameItem(held, spare)
&& ItemTraits.dataKey(held).equals(ItemTraits.dataKey(spare));

With refill, the mod’s method that brings a matching stack into a slot, in client initialization:

Refill a held stack only when it was eaten or drunk
ItemReplacedCallback.EVENT.register((hand, inventorySlot, lastStack, replacement) -> {
// A bucket filled at a pond is replaced too, and is no use that wants another bucket.
if (ItemTraits.isEatenOrDrunk(lastStack)) refill(inventorySlot, lastStack);
});

Experimental · Since 2.10.0 · Side: server · Thread: server

ItemDrops drops items at an entity’s feet, as vanilla drops a mob’s loot. With chicken, a live chicken, on the server thread:

Shed a feather from a chicken
ItemDrops.dropAt(chicken, new ItemStack(Items.FEATHER))
.ifPresent(feather -> EntityData.putInt(feather, id("shed"), 1));

Experimental · Since 2.10.0 · Side: server · Thread: server

LevelEntities walks the entities a level ticks, so a mod’s timer runs only where vanilla would run the entity’s own. With DUE, a list the mod keeps, and COLLECT, a field holding chicken -> { if (featherDue(chicken)) DUE.add(chicken); }, in a LevelTickCallback listener that runs once a second:

Drop a feather from each chicken that is due one
if (level.getGameTime() % 20 != 0 || !LevelEntities.entitiesTick(level)) return;
LevelEntities.forEachTicking(level, Chicken.class, COLLECT);
for (Chicken chicken : DUE) ItemDrops.dropAt(chicken, new ItemStack(Items.FEATHER));
DUE.clear();
  • A level with no player and no forced chunk stops ticking its entities, so a timer that runs without LevelEntities#entitiesTick gets ahead of the animals it counts for.

Experimental · Since 2.10.0 · Side: both · Thread: any

LevelReads answers two questions vanilla works out from several values. With mob, a mob that just stepped on a block in level, and above, the space over that block, in a MobFootfallCallback listener:

Wear the ground only in the rain, and only where mobs may change the world
if (!LevelReads.isRainingAt(level, above)) return;
if (!LevelReads.mobGriefing(level, mob)) return;

Experimental · Since 2.10.0 · Side: server · Thread: server

ScoreboardTags marks an entity with a scoreboard tag, the kind /tag edits, and reads the mark back. Use it for a yes-or-no mark that commands and players may also see; use EntityData for a value. With BRED, a constant holding "mymod.bred", in your mod’s common initializer:

Mark each baby a player bred
AnimalBredCallback.EVENT.register((level, parent, partner, baby, player) ->
ScoreboardTags.add(baby, BRED));

With cow, a cow, on the server thread:

Check the mark
boolean bred = ScoreboardTags.has(cow, BRED);
  • A selector such as @e[tag=mymod.bred] finds the entities that have the tag.

Experimental · Since 2.10.0 · Side: server · Thread: server

FakePlayers gives a machine a player to break and place blocks as, so claim mods can refuse it. In the machine’s block entity, with owner, the UUID of the player who placed it, front, the position it faces, set when it was placed, and fake, a ServerPlayer field, in its server tick with serverLevel, its level as a ServerLevel:

Break the block in front of a machine as its owner's fake player
if (fake == null) fake = FakePlayers.get(serverLevel, new GameProfile(owner, "[MyMachine]"));
boolean broke = FakePlayers.breakBlock(fake, front);