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.
At a glance
Section titled “At a glance”| 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 |
Quick start
Section titled “Quick start”With villager, a villager that just died, in a MobKilledCallback listener:
ItemDrops.dropFromInventory(villager, stack -> !ItemTraits.hasVanishingCurse(stack) && (ItemTraits.isFood(stack) || ItemTraits.isPlant(stack)));ItemTraits
Section titled “ItemTraits”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:
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:
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:
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);});ItemDrops
Section titled “ItemDrops”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:
ItemDrops.dropAt(chicken, new ItemStack(Items.FEATHER)) .ifPresent(feather -> EntityData.putInt(feather, id("shed"), 1));LevelEntities
Section titled “LevelEntities”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:
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#entitiesTickgets ahead of the animals it counts for.
LevelReads
Section titled “LevelReads”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:
if (!LevelReads.isRainingAt(level, above)) return;if (!LevelReads.mobGriefing(level, mob)) return;ScoreboardTags
Section titled “ScoreboardTags”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:
AnimalBredCallback.EVENT.register((level, parent, partner, baby, player) -> ScoreboardTags.add(baby, BRED));With cow, a cow, on the server thread:
boolean bred = ScoreboardTags.has(cow, BRED);- A selector such as
@e[tag=mymod.bred]finds the entities that have the tag.
FakePlayers
Section titled “FakePlayers”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:
if (fake == null) fake = FakePlayers.get(serverLevel, new GameProfile(owner, "[MyMachine]"));boolean broke = FakePlayers.breakBlock(fake, front);See also
Section titled “See also”- Block behavior and rendering:
BlockPlacement, which places the block a plant item would. - Item events: events for an item that is picked up or about to despawn.
- Mob events: the death events a drop runs from, and the breeding event a mark is added in.
- Lifecycle events: the level tick a walk runs from.