Skip to content

Commit

Permalink
address xfact's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
IchHabeHunger54 committed Dec 14, 2024
1 parent 887ed98 commit 8e8774d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/blocks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ The following subsections further break down these stages into actual method cal

#### 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:
The mining speed is calculated from the block's hardness, the used [tool]'s speed, and several entity [attributes] according to the following rules:

```java
// This will return the tool's mining speed, or 1 if the held item is either empty, not a tool,
Expand Down
20 changes: 12 additions & 8 deletions docs/entities/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ sidebar_position: 2
---
# Data and Networking

One of the most important use cases of entities is to store data of some sort. All entities store some default data, such as their type and their position. This article will explain how to add your own data, as well as how to synchronize that data.
An entity without data is quite useless, as such storing data on an entity is essential. All entities store some default data, such as their type and their position. This article will explain how to add your own data, as well as how to synchronize that data.

The most simple way to add data is as a field in your `Entity` class. You can then interact with this data in any way you wish. However, this quickly becomes very annoying as soon as you have to synchronize that data. This is because most entity logic is run on the server only, and it is only occasionally (depending on the [`EntityType`][entitytype]'s `clientUpdateInterval` value) that an update is sent to the client; this is also the cause for easily noticeable entity "lags" when the server's tick speed is too slow.

As such, vanilla introduces a few systems to help with that. These systems generally exist in parallel and can be replaced with one another, this is due to legacy reasons.
As such, vanilla introduces a few systems to help with that, each of which serves a specific purpose.

## `SynchedEntityData`

`SynchedEntityData` is a system used for both storing and syncing values over the network. It is split into three classes:
`SynchedEntityData` is a system used for storing values at runtime and syncing them over the network. It is split into three classes:

- `EntityDataSerializer`s are basically wrappers around a [`StreamCodec`][streamcodec].
- They are a registry, meaning that if you want to add new `EntityDataSerializer`s, they must be added by [registration].
- Minecraft uses a hard-coded map of serializers. NeoForge transforms this map into a registry, meaning that if you want to add new `EntityDataSerializer`s, they must be added by [registration].
- Minecraft defines various default `EntityDataSerializer`s in the `EntityDataSerializers` class.
- `EntityDataAccessor`s are held by the entity and used to get and set the data values.
- `EntityDataAccessor`s are held by the entity and are used to get and set the data values.
- `SynchedEntityData` itself holds all `EntityDataAccessor`s for an entity, and automatically calls on the `EntityDataSerializer`s to sync values as needed.

To get started, create an `EntityDataAccessor` in your entity class:
Expand All @@ -34,6 +34,10 @@ public class MyEntity extends Entity {
}
```

:::danger
While the compiler will allow you to use a class other than the owning class as the first parameter in `SynchedEntityData#defineId()`, this can and will lead to hard-to-debug issues and as such should be avoided.
:::

We must then define default values in the `defineSynchedData` method, like so:

```java
Expand All @@ -57,7 +61,7 @@ this.getEntityData().set(MY_DATA, 1);

## `readAdditionalSaveData` and `addAdditionalSaveData`

This method works by loading/saving your values from/to an [NBT tag][nbt], like so:
These two methods are used to read and write data to disk. They work by loading/saving your values from/to an [NBT tag][nbt], like so:

```java
// Assume that an `int data` exists in the class.
Expand All @@ -80,11 +84,11 @@ Additionally, you can send your own packets upon spawning. To do so, override `I

## Data Attachments

Entities have been patched to extend `AttachmentHolder` and as such support data storage via [data attachments][attachment]. Please see the linked article for more information.
Entities have been patched to extend `AttachmentHolder` and as such support data storage via [data attachments][attachment]. Its main use is to define custom data on entities that are not your own, i.e., entities added by Minecraft or other mods. Please see the linked article for more information.

## Custom Network Messages

Of course, you can also always opt to use a custom packet to send additional information when needed. Please refer to the [Networking articles][networking] for more information.
For syncing, you can also always opt to use a custom packet to send additional information when needed. Please refer to the [Networking articles][networking] for more information.

[attachment]: ../datastorage/attachments.md
[entitytype]: index.md#entitytype
Expand Down
2 changes: 1 addition & 1 deletion docs/entities/renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sidebar_position: 5

Entity renderers are used to define rendering behavior for an entity. They only exist on the [logical and physical client][sides].

Entity rendering uses what is known as entity render states. Simply put, this is an object that holds all values that the renderer needs. Every time the entity is rendered, the render state is updated, and then the `#render` method uses that render state to render the entity. This is to avoid common issues when the entity renderer accidentally mutates the entity's fields.
Entity rendering uses what is known as entity render states. Simply put, this is an object that holds all values that the renderer needs. Every time the entity is rendered, the render state is updated, and then the `#render` method uses that render state to render the entity. This is probably intended to be adapted into a deferred rendering system at some point, where the render information is collected beforehand (which could potentially be multithreaded) and rendering then happens at a later point in time.

## Creating an Entity Renderer

Expand Down

1 comment on commit 8e8774d

@neoforged-pages-deployments
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploying with Cloudflare Pages

Name Result
Last commit: 8e8774dade8a2ff2bce050578c3e4ee775162edf
Status: ✅ Deploy successful!
Preview URL: https://0bd7414b.neoforged-docs-previews.pages.dev
PR Preview URL: https://pr-171.neoforged-docs-previews.pages.dev

Please sign in to comment.