Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity docs #171

Open
wants to merge 70 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
312442d
rework registries and resource location documentation
IchHabeHunger54 Jan 29, 2024
aafbf1b
rework side documentation
IchHabeHunger54 Jan 29, 2024
a748304
rework events and lifecycle documentation
IchHabeHunger54 Jan 29, 2024
4b2d021
fix broken links
IchHabeHunger54 Jan 29, 2024
c49ff55
fix some mistakes regarding `DeferredRegister.Blocks`
IchHabeHunger54 Jan 31, 2024
242abf0
add side checks in the event handlers
IchHabeHunger54 Jan 31, 2024
a4971bd
fix some of the mistakes in registries.md
IchHabeHunger54 Jan 31, 2024
9950d51
remove mention of registry int id syncing (it is discouraged)
IchHabeHunger54 Feb 21, 2024
f64b8fe
Merge remote-tracking branch 'upstream/main'
IchHabeHunger54 Feb 21, 2024
a1bd87d
update datapack registry datagen
IchHabeHunger54 Feb 21, 2024
31b5bf2
registry querying
IchHabeHunger54 Feb 21, 2024
ff5d5b1
add back int ID syncing, with a big disclaimer
IchHabeHunger54 Feb 21, 2024
965be16
update sync disclaimer
IchHabeHunger54 Feb 21, 2024
2e65131
address most feedback by ChampionAsh
IchHabeHunger54 Feb 22, 2024
1ed292c
Update docs/blocks/index.md
IchHabeHunger54 Feb 22, 2024
e2fbc31
Update docs/concepts/registries.md
IchHabeHunger54 Feb 22, 2024
40c17a9
implement most of XFact's feedback
IchHabeHunger54 Feb 23, 2024
7a24414
fix datapack registry creation
IchHabeHunger54 Feb 23, 2024
55873a1
remove recommendation of separate DR and DH classes
IchHabeHunger54 Feb 23, 2024
cdf58f2
fix an oversight
IchHabeHunger54 Feb 23, 2024
380db40
Update docs/blocks/index.md
IchHabeHunger54 Feb 23, 2024
2a369a7
BootstapContext :screm:
IchHabeHunger54 Feb 25, 2024
b1e1966
Merge remote-tracking branch 'origin/main'
IchHabeHunger54 Feb 25, 2024
b5a78bb
fix two wrong mentions of Registries
IchHabeHunger54 Feb 26, 2024
b6dcbb9
fix exception name
IchHabeHunger54 Feb 26, 2024
8d54466
Merge remote-tracking branch 'upstream/main'
IchHabeHunger54 Mar 3, 2024
90c3b4a
Merge remote-tracking branch 'upstream/main'
IchHabeHunger54 Mar 3, 2024
1d17916
Merge remote-tracking branch 'refs/remotes/upstream/main'
IchHabeHunger54 Aug 8, 2024
99dd0e6
add Champ's previous work on entities
IchHabeHunger54 Oct 2, 2024
a9b38d5
Merge remote-tracking branch 'origin/main'
IchHabeHunger54 Oct 2, 2024
41f583b
Merge branch 'refs/heads/main' into feature/entities
IchHabeHunger54 Oct 2, 2024
a9937e3
basic structure setup and entitytype documentation
IchHabeHunger54 Oct 2, 2024
4dd1142
entity class hierarchy
IchHabeHunger54 Oct 3, 2024
f615ade
damaging and ticking entities
IchHabeHunger54 Oct 3, 2024
773ce6a
picking entities
IchHabeHunger54 Oct 3, 2024
ec869d5
add docs for the left click and middle click pipelines
IchHabeHunger54 Oct 3, 2024
12392bc
Merge remote-tracking branch 'refs/remotes/upstream/main' into featur…
IchHabeHunger54 Oct 28, 2024
461206c
living entity docs
IchHabeHunger54 Oct 28, 2024
859d031
data and networking article
IchHabeHunger54 Oct 28, 2024
8963971
use better TODO markers
IchHabeHunger54 Oct 29, 2024
d4466af
built-in attribute list
IchHabeHunger54 Oct 30, 2024
25cc2c1
default attributes
IchHabeHunger54 Oct 30, 2024
0bcad78
attribute modifiers
IchHabeHunger54 Oct 30, 2024
3f53b81
custom spawn data
IchHabeHunger54 Oct 30, 2024
87938c6
custom attributes
IchHabeHunger54 Oct 30, 2024
54600a4
fix broken link to attributes page
IchHabeHunger54 Oct 30, 2024
930fca6
living entity hierarchy
IchHabeHunger54 Oct 30, 2024
e0e8020
spawning
IchHabeHunger54 Nov 5, 2024
6316767
projectiles
IchHabeHunger54 Nov 15, 2024
83436ba
Merge remote-tracking branch 'refs/remotes/upstream/main'
IchHabeHunger54 Nov 15, 2024
d1120f5
Merge branch 'refs/heads/main' into feature/entities
IchHabeHunger54 Nov 15, 2024
74eef1c
apply 1.21.3 changes to interactions.md, and fix outdated mention of …
IchHabeHunger54 Nov 15, 2024
6c4cd53
Merge remote-tracking branch 'refs/remotes/upstream/main' into featur…
IchHabeHunger54 Nov 19, 2024
7c12eff
consistently apply code and list formatting
IchHabeHunger54 Nov 19, 2024
84d7574
mobcategory
IchHabeHunger54 Nov 20, 2024
551bac0
link to and from the data attachments article
IchHabeHunger54 Nov 20, 2024
ca9bc33
add links to the entity articles
IchHabeHunger54 Nov 20, 2024
79bb313
escape characters
IchHabeHunger54 Nov 20, 2024
44951b9
entity attachments
IchHabeHunger54 Nov 20, 2024
2986514
apply feedback, part 1
IchHabeHunger54 Nov 21, 2024
e7ca864
fix wrong name for ArmorHurtEvent
IchHabeHunger54 Nov 21, 2024
79078a2
entity renderers, pt 1
IchHabeHunger54 Nov 26, 2024
7b35abe
renderer hierarchy and 1.21.3 update
IchHabeHunger54 Nov 26, 2024
110be93
entity layers, part 1
IchHabeHunger54 Nov 29, 2024
56eef02
entity layers, part 2
IchHabeHunger54 Dec 1, 2024
6652351
Merge remote-tracking branch 'upstream/main' into feature/entities
IchHabeHunger54 Dec 1, 2024
362280b
use mermaid diagrams
IchHabeHunger54 Dec 1, 2024
a1746fe
address the leftover comments
IchHabeHunger54 Dec 2, 2024
887ed98
address luke's comment
IchHabeHunger54 Dec 2, 2024
8e8774d
address xfact's comments
IchHabeHunger54 Dec 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/blockentities/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ Be aware that menus that directly interface with `Container`s must `#copy()` the

## `Container`s on `Entity`s

`Container`s on `Entity`s are finicky: whether an entity has a container or not cannot be universally determined. It all depends on what entity you are handling, and as such can require a lot of special-casing.
`Container`s on [`Entity`s][entity] are finicky: whether an entity has a container or not cannot be universally determined. It all depends on what entity you are handling, and as such can require a lot of special-casing.

If you are creating an entity yourself, there is nothing stopping you from implementing `Container` on it directly, though be aware that you will not be able to use superclasses such as `SimpleContainer` (since `Entity` is the superclass).

Expand Down Expand Up @@ -307,6 +307,7 @@ When iterating over the inventory contents, it is recommended to iterate over `i
[blockentity]: index.md
[component]: ../resources/client/i18n.md#components
[datacomponent]: ../items/datacomponents.md
[entity]: ../entities/index.md
[item]: ../items/index.md
[itemstack]: ../items/index.md#itemstacks
[menu]: ../gui/menus.md
3 changes: 2 additions & 1 deletion docs/blockentities/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Block Entities

Block entities allow the storage of data on [blocks][block] in cases where [block states][blockstate] are not suitable. This is especially the case for data with a non-finite amount of options, such as inventories. Block entities are stationary and bound to a block, but otherwise share many similarities with entities, hence the name.
Block entities allow the storage of data on [blocks][block] in cases where [block states][blockstate] are not suitable. This is especially the case for data with a non-finite amount of options, such as inventories. Block entities are stationary and bound to a block, but otherwise share many similarities with [entities], hence the name.

:::note
If you have a finite and reasonably small amount (= a few hundred at most) of possible states for your block, you might want to consider using [block states][blockstate] instead.
Expand Down Expand Up @@ -237,6 +237,7 @@ It is important that you do safety checks, as the `BlockEntity` might already be
[blockreg]: ../blocks/index.md#basic-blocks
[blockstate]: ../blocks/states.md
[dataattachments]: ../datastorage/attachments.md
[entities]: ../entities/index.md
[modbus]: ../concepts/events.md#event-buses
[nbt]: ../datastorage/nbt.md
[networking]: ../networking/index.md
Expand Down
89 changes: 60 additions & 29 deletions docs/blocks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ After registering the block, all references to the new `my_block` should use thi

```java
level.getBlockState(position) // returns the blockstate placed in the given level (world) at the given position
//highlight-next-line
.is(MyBlockRegistrationClass.MY_BLOCK);
//highlight-next-line
.is(MyBlockRegistrationClass.MY_BLOCK);
```

This approach also has the convenient effect that `block1 == block2` works and can be used instead of Java's `equals` method (using `equals` still works, of course, but is pointless since it compares by reference anyway).
Expand Down Expand Up @@ -69,16 +69,16 @@ So for example, a simple implementation would look something like this:
```java
//BLOCKS is a DeferredRegister.Blocks
public static final DeferredBlock<Block> MY_BETTER_BLOCK = BLOCKS.register(
"my_better_block",
registryName -> new Block(BlockBehaviour.Properties.of()
//highlight-start
.setId(ResourceKey.create(Registries.BLOCK, registryName))
.destroyTime(2.0f)
.explosionResistance(10.0f)
.sound(SoundType.GRAVEL)
.lightLevel(state -> 7)
//highlight-end
));
"my_better_block",
registryName -> new Block(BlockBehaviour.Properties.of()
//highlight-start
.setId(ResourceKey.create(Registries.BLOCK, registryName))
.destroyTime(2.0f)
.explosionResistance(10.0f)
.sound(SoundType.GRAVEL)
.lightLevel(state -> 7)
//highlight-end
));
```

For further documentation, see the source code of `BlockBehaviour.Properties`. For more examples, or to look at the values used by Minecraft, have a look at the `Blocks` class.
Expand All @@ -104,7 +104,6 @@ If the block subclass only takes in the `BlockBehaviour.Properties`, then `Block
```java
// For some block subclass
public class SimpleBlock extends Block {

public SimpleBlock(BlockBehavior.Properties properties) {
// ...
}
Expand All @@ -129,7 +128,6 @@ If the block subclass contains more parameters, then [`RecordCodecBuilder#mapCod
```java
// For some block subclass
public class ComplexBlock extends Block {

public ComplexBlock(int value, BlockBehavior.Properties properties) {
// ...
}
Expand Down Expand Up @@ -170,28 +168,28 @@ We already discussed how to create a `DeferredRegister.Blocks` [above], as well
public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks("yourmodid");

public static final DeferredBlock<Block> EXAMPLE_BLOCK = BLOCKS.register(
"example_block", registryName -> new Block(
BlockBehaviour.Properties.of()
// The ID must be set on the block
.setId(ResourceKey.create(Registries.BLOCK, registryName))
)
"example_block", registryName -> new Block(
BlockBehaviour.Properties.of()
// The ID must be set on the block
.setId(ResourceKey.create(Registries.BLOCK, registryName))
)
);

// Same as above, except that the block properties are constructed eagerly.
// setId is also called internally on the properties object.
public static final DeferredBlock<Block> EXAMPLE_BLOCK = BLOCKS.registerBlock(
"example_block",
Block::new, // The factory that the properties will be passed into.
BlockBehaviour.Properties.of() // The properties to use.
"example_block",
Block::new, // The factory that the properties will be passed into.
BlockBehaviour.Properties.of() // The properties to use.
);
```

If you want to use `Block::new`, you can leave out the factory entirely:

```java
public static final DeferredBlock<Block> EXAMPLE_BLOCK = BLOCKS.registerSimpleBlock(
"example_block",
BlockBehaviour.Properties.of() // The properties to use.
"example_block",
BlockBehaviour.Properties.of() // The properties to use.
);
```

Expand All @@ -209,7 +207,7 @@ In several situations, multiple methods of `Block` are used at different times.

### Placing a Block

Block placement logic is called from `BlockItem#useOn` (or some subclass's implementation thereof, such as in `PlaceOnWaterBlockItem`, which is used for lily pads). For more information on how the game gets there, see the [Interaction Pipeline][interactionpipeline]. In practice, this means that as soon as a `BlockItem` is right-clicked (for example a cobblestone item), this behavior is called.
Block placement logic is called from `BlockItem#useOn` (or some subclass's implementation thereof, such as in `PlaceOnWaterBlockItem`, which is used for lily pads). For more information on how the game gets there, see [Right-Clicking Items][rightclick]. In practice, this means that as soon as a `BlockItem` is right-clicked (for example a cobblestone item), this behavior is called.

- Several prerequisites are checked, for example that you are not in spectator mode, that all required feature flags for the block are enabled or that the target position is not outside the world border. If at least one of these checks fails, the pipeline ends.
- `BlockBehaviour#canBeReplaced` is called for the block currently at the position where the block is attempted to be placed. If it returns `false`, the pipeline ends. Prominent cases that return `true` here are tall grass or snow layers.
Expand Down Expand Up @@ -241,11 +239,10 @@ while (leftClickIsBeingHeld()) {
}
```

The following subsections further break down these stages into actual method calls.
The following subsections further break down these stages into actual method calls. For information about how the game gets from left-clicking to this pipeline, see [Left-Clicking an Item][leftclick].

#### The "Initiating" Stage

- Client-only: `InputEvent.InteractionKeyMappingTriggered` is fired with the left mouse button and the main hand. If the event is canceled, the pipeline ends.
- Several prerequisites are checked, for example that you are not in spectator mode, that all required feature flags for the `ItemStack` in your main hand are enabled or that the block in question is not outside the world border. If at least one of these checks fails, the pipeline ends.
- `PlayerInteractEvent.LeftClickBlock` is fired. If the event is canceled, the pipeline ends.
- Note that when the event is canceled on the client, no packets are sent to the server and thus no logic runs on the server.
Expand Down Expand Up @@ -281,6 +278,38 @@ The following subsections further break down these stages into actual method cal
- Server-only: `BlockDropsEvent` is fired. If the event is canceled, then nothing is dropped when the block breaks. Otherwise, every `ItemEntity` in `BlockDropsEvent#getDrops` is added to the current level.
- Server-only: `Block#popExperience` is called with the result of the previous `IBlockExtension#getExpDrop` call, if that call returned a value greater than 0.

#### Mining Speed

The mining speed is calculated as f from the block's hardness, the used [tool]'s speed, and several entity [attributes] according to the following rules:
IchHabeHunger54 marked this conversation as resolved.
Show resolved Hide resolved

```java
// This will return the tool's mining speed, or 1 if the held item is either empty, not a tool,
// or not applicable for the block being broken.
float destroySpeed = item.getDestroySpeed();
// If we have an applicable tool, add the minecraft:mining_efficiency attribute as an additive modifier.
if (destroySpeed > 1) {
destroySpeed += player.getAttributeValue(Attributes.MINING_EFFICIENCY);
}
// Apply effects from haste, conduit power, and slowness multiplicatively.
if (player.hasEffect(MobEffects.DIG_SPEED)) { destroySpeed *= ...; }
if (player.hasEffect(MobEffects.CONDUIT_POWER)) { destroySpeed *= ...; }
if (player.hasEffect(MobEffects.DIG_SLOWDOWN)) { destroySpeed *= ...; }
// Add the minecraft:block_break_speed attribute as a multiplicative modifier.
destroySpeed *= player.getAttributeValue(Attributes.BLOCK_BREAK_SPEED);
// If the player is underwater, apply the underwater mining speed penalty multiplicatively.
if (player.isEyeInFluid(FluidTags.WATER)) {
destroySpeed *= player.getAttributeValue(Attributes.SUBMERGED_MINING_SPEED);
}
// If the player is trying to break a block in mid-air, make the player mine 5 times slower.
if (!player.onGround()) {
destroySpeed /= 5;
}
destroySpeed = /* The PlayerEvent.BreakSpeed event is fired here, allowing modders to further modify this value. */;
return destroySpeed;
```

The exact code for this can be found in `Player#getDigSpeed` for reference.

### Ticking

Ticking is a mechanism that updates (ticks) parts of the game every 1 / 20 seconds, or 50 milliseconds ("one tick"). Blocks provide different ticking methods that are called in different ways.
Expand All @@ -306,19 +335,21 @@ Random ticks occur every tick for a set amount of blocks in a chunk. That set am
Random ticking is used by a wide range of mechanics in Minecraft, such as plant growth, ice and snow melting, or copper oxidizing.

[above]: #one-block-to-rule-them-all
[attributes]: ../entities/attributes.md
[below]: #deferredregisterblocks-helpers
[blockentities]: ../blockentities/index.md
[blockstates]: states.md
[bsfile]: ../resources/client/models/index.md#blockstate-files
[codec]: ../datastorage/codecs.md#records
[events]: ../concepts/events.md
[interactionpipeline]: ../items/interactionpipeline.md
[item]: ../items/index.md
[leftclick]: ../items/interactions.md#left-clicking-an-item
[model]: ../resources/client/models/index.md
[randomtick]: #random-ticking
[registration]: ../concepts/registries.md#methods-for-registering
[resources]: ../resources/index.md#assets
[rightclick]: ../items/interactions.md#right-clicking-an-item
[sounds]: ../resources/client/sounds.md
[textures]: ../resources/client/textures.md
[tool]: ../items/tools.md
[usingblocks]: #using-blocks
[usingblockstates]: states.md#using-blockstates
1 change: 1 addition & 0 deletions docs/concepts/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ graph TD;
PlayerEvent-->CanPlayerSleepEvent;

class Event,BlockEvent,EntityEvent,LivingEvent,PlayerEvent red;
class BlockDropsEvent,CanPlayerSleepEvent blue;
```

### Cancellable Events
Expand Down
5 changes: 3 additions & 2 deletions docs/datastorage/attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ To sync block entity, chunk, or entity attachments to a client, you need to [sen

## Copying data on player death

By default, entity data attachments are not copied on player death. To automatically copy an attachment on player death, set `copyOnDeath` in the attachment builder.
By default, [entity] data attachments are not copied on player death. To automatically copy an attachment on player death, set `copyOnDeath` in the attachment builder.

More complex handling can be implemented via `PlayerEvent.Clone` by reading the data from the original entity and assigning it to the new entity. In this event, the `#isWasDeath` method can be used to distinguish between respawning after death and returning from the End. This is important because the data will already exist when returning from the End, so care has to be taken to not duplicate values in this case.

Expand All @@ -113,6 +113,7 @@ NeoForge.EVENT_BUS.register(PlayerEvent.Clone.class, event -> {
});
```

[saveddata]: saveddata.md
[datacomponents]: ../items/datacomponents.md
[entity]: ../entities/index.md
[network]: ../networking/index.md
[saveddata]: saveddata.md
3 changes: 2 additions & 1 deletion docs/datastorage/nbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ tag.get("Tag");

## Usages of NBT

NBT is used in a lot of places in Minecraft. Some of the most common examples include [`BlockEntity`][blockentity]s and `Entity`s.
NBT is used in a lot of places in Minecraft. Some of the most common examples include [`BlockEntity`][blockentity]s and [`Entity`s][entity].

:::note
`ItemStack`s abstract away the usage of NBT into [data components][datacomponents].
Expand All @@ -102,4 +102,5 @@ NBT is used in a lot of places in Minecraft. Some of the most common examples in
[blockentity]: ../blockentities/index.md
[datapack]: ../resources/index.md#data
[datacomponents]: ../items/datacomponents.md
[entity]: ../entities/index.md
[nbtwiki]: https://minecraft.wiki/w/NBT_format
4 changes: 4 additions & 0 deletions docs/entities/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Entities",
"position": 5
}
Loading
Loading