SynchedEntityData .java

net.minecraft.network.syncher.SynchedEntityData (class): members: getNonDefaultValues, MAX_ID_VALUE, assignValues, ID_REGISTRY, DataValue, itemsById, set.

net.minecraft.network.syncherclass248 linescommon7.6 KB

API Surface (18)

<T> EntityDataAccessor<T> defineId(final Class<? extends SyncedDataHolder> clazz, final EntityDataSerializer<T> type)
<T> T get(final EntityDataAccessor<T> accessor)
<T> void set(final EntityDataAccessor<T> accessor, final T value)
<T> void set(final EntityDataAccessor<T> accessor, final T value, final boolean forceDirty)
boolean isDirty()
void assignValues(final List<SynchedEntityData.DataValue<?>> items)
<T> SynchedEntityData.Builder define(final EntityDataAccessor<T> accessor, final T value)
SynchedEntityData build()
EntityDataAccessor<T> getAccessor()
void setValue(final T value)
T getValue()
boolean isDirty()
void setDirty(final boolean dirty)
boolean isSetToDefault()
SynchedEntityData.DataValue<T> value()
<T> SynchedEntityData.DataValue<T> create(final EntityDataAccessor<T> accessor, final T value)
void write(final RegistryFriendlyByteBuf output)
SynchedEntityData.DataValue<?> read(final RegistryFriendlyByteBuf input, final int id)

Fields & Constants (6)

LOGGER
MAX_ID_VALUE
ID_REGISTRY
entity
itemsById
isDirty

String literals (9)

"Data value id is too big with "
"! (Max is 254)"
"! (Max is "
"Duplicate id value for "
"Unregistered serializer "
" for "
"Entity "
" has not defined synched data value "
"Unknown serializer type "

Full Source

SynchedEntityData.java248 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network.syncher;
2
3
import com.mojang.logging.LogUtils;
4
import io.netty.handler.codec.DecoderException;
5
import io.netty.handler.codec.EncoderException;
6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.Locale;
9
import java.util.Objects;
10
import net.minecraft.network.RegistryFriendlyByteBuf;
11
import net.minecraft.util.ClassTreeIdRegistry;
12
import org.apache.commons.lang3.ObjectUtils;
13
import org.jspecify.annotations.Nullable;
14
import org.slf4j.Logger;
15
16
public class SynchedEntityData {
17
	private static final Logger LOGGER = LogUtils.getLogger();
18
	private static final int MAX_ID_VALUE = 254;
19
	private static final ClassTreeIdRegistry ID_REGISTRY = new ClassTreeIdRegistry();
20
	private final SyncedDataHolder entity;
21
	private final SynchedEntityData.DataItem<?>[] itemsById;
22
	private boolean isDirty;
23
24
	private SynchedEntityData(final SyncedDataHolder entity, final SynchedEntityData.DataItem<?>[] itemsById) {
25
		this.entity = entity;
26
		this.itemsById = itemsById;
27
	}
28
29
	public static <T> EntityDataAccessor<T> defineId(final Class<? extends SyncedDataHolder> clazz, final EntityDataSerializer<T> type) {
30
		if (LOGGER.isDebugEnabled()) {
31
			try {
32
				Class<?> aClass = Class.forName(Thread.currentThread().getStackTrace()[2].getClassName());
33
				if (!aClass.equals(clazz)) {
34
					LOGGER.debug("defineId called for: {} from {}", clazz, aClass, new RuntimeException());
35
				}
36
			} catch (ClassNotFoundException var3) {
37
			}
38
		}
39
40
		int id = ID_REGISTRY.define(clazz);
41
		if (id > 254) {
42
			throw new IllegalArgumentException("Data value id is too big with " + id + "! (Max is 254)");
43
		} else {
44
			return type.createAccessor(id);
45
		}
46
	}
47
48
	private <T> SynchedEntityData.DataItem<T> getItem(final EntityDataAccessor<T> accessor) {
49
		return (SynchedEntityData.DataItem<T>)this.itemsById[accessor.id()];
50
	}
51
52
	public <T> T get(final EntityDataAccessor<T> accessor) {
53
		return this.getItem(accessor).getValue();
54
	}
55
56
	public <T> void set(final EntityDataAccessor<T> accessor, final T value) {
57
		this.set(accessor, value, false);
58
	}
59
60
	public <T> void set(final EntityDataAccessor<T> accessor, final T value, final boolean forceDirty) {
61
		SynchedEntityData.DataItem<T> dataItem = this.getItem(accessor);
62
		if (forceDirty || ObjectUtils.notEqual(value, dataItem.getValue())) {
63
			dataItem.setValue(value);
64
			this.entity.onSyncedDataUpdated(accessor);
65
			dataItem.setDirty(true);
66
			this.isDirty = true;
67
		}
68
	}
69
70
	public boolean isDirty() {
71
		return this.isDirty;
72
	}
73
74
	public @Nullable List<SynchedEntityData.DataValue<?>> packDirty() {
75
		if (!this.isDirty) {
76
			return null;
77
		}
78
79
		this.isDirty = false;
80
		List<SynchedEntityData.DataValue<?>> result = new ArrayList<>();
81
82
		for (SynchedEntityData.DataItem<?> dataItem : this.itemsById) {
83
			if (dataItem.isDirty()) {
84
				dataItem.setDirty(false);
85
				result.add(dataItem.value());
86
			}
87
		}
88
89
		return result;
90
	}
91
92
	public @Nullable List<SynchedEntityData.DataValue<?>> getNonDefaultValues() {
93
		List<SynchedEntityData.DataValue<?>> result = null;
94
95
		for (SynchedEntityData.DataItem<?> dataItem : this.itemsById) {
96
			if (!dataItem.isSetToDefault()) {
97
				if (result == null) {
98
					result = new ArrayList<>();
99
				}
100
101
				result.add(dataItem.value());
102
			}
103
		}
104
105
		return result;
106
	}
107
108
	public void assignValues(final List<SynchedEntityData.DataValue<?>> items) {
109
		for (SynchedEntityData.DataValue<?> item : items) {
110
			SynchedEntityData.DataItem<?> dataItem = this.itemsById[item.id];
111
			this.assignValue(dataItem, item);
112
			this.entity.onSyncedDataUpdated(dataItem.getAccessor());
113
		}
114
115
		this.entity.onSyncedDataUpdated(items);
116
	}
117
118
	private <T> void assignValue(final SynchedEntityData.DataItem<T> dataItem, final SynchedEntityData.DataValue<?> item) {
119
		if (!Objects.equals(item.serializer(), dataItem.accessor.serializer())) {
120
			throw new IllegalStateException(
121
				String.format(
122
					Locale.ROOT,
123
					"Invalid entity data item type for field %d on entity %s: old=%s(%s), new=%s(%s)",
124
					dataItem.accessor.id(),
125
					this.entity,
126
					dataItem.value,
127
					dataItem.value.getClass(),
128
					item.value,
129
					item.value.getClass()
130
				)
131
			);
132
		}
133
134
		dataItem.setValue((T)item.value);
135
	}
136
137
	public static class Builder {
138
		private final SyncedDataHolder entity;
139
		private final SynchedEntityData.@Nullable DataItem<?>[] itemsById;
140
141
		public Builder(final SyncedDataHolder entity) {
142
			this.entity = entity;
143
			this.itemsById = new SynchedEntityData.DataItem[SynchedEntityData.ID_REGISTRY.getCount(entity.getClass())];
144
		}
145
146
		public <T> SynchedEntityData.Builder define(final EntityDataAccessor<T> accessor, final T value) {
147
			int id = accessor.id();
148
			if (id >= this.itemsById.length) {
149
				throw new IllegalArgumentException("Data value id is too big with " + id + "! (Max is " + (this.itemsById.length - 1) + ")");
150
			}
151
152
			if (this.itemsById[id] != null) {
153
				throw new IllegalArgumentException("Duplicate id value for " + id + "!");
154
			}
155
156
			if (EntityDataSerializers.getSerializedId(accessor.serializer()) < 0) {
157
				throw new IllegalArgumentException("Unregistered serializer " + accessor.serializer() + " for " + id + "!");
158
			}
159
160
			this.itemsById[accessor.id()] = new SynchedEntityData.DataItem<>(accessor, value);
161
			return this;
162
		}
163
164
		public SynchedEntityData build() {
165
			for (int i = 0; i < this.itemsById.length; i++) {
166
				if (this.itemsById[i] == null) {
167
					throw new IllegalStateException("Entity " + this.entity.getClass() + " has not defined synched data value " + i);
168
				}
169
			}
170
171
			return new SynchedEntityData(this.entity, this.itemsById);
172
		}
173
	}
174
175
	public static class DataItem<T> {
176
		private final EntityDataAccessor<T> accessor;
177
		private T value;
178
		private final T initialValue;
179
		private boolean dirty;
180
181
		public DataItem(final EntityDataAccessor<T> accessor, final T initialValue) {
182
			this.accessor = accessor;
183
			this.initialValue = initialValue;
184
			this.value = initialValue;
185
		}
186
187
		public EntityDataAccessor<T> getAccessor() {
188
			return this.accessor;
189
		}
190
191
		public void setValue(final T value) {
192
			this.value = value;
193
		}
194
195
		public T getValue() {
196
			return this.value;
197
		}
198
199
		public boolean isDirty() {
200
			return this.dirty;
201
		}
202
203
		public void setDirty(final boolean dirty) {
204
			this.dirty = dirty;
205
		}
206
207
		public boolean isSetToDefault() {
208
			return this.initialValue.equals(this.value);
209
		}
210
211
		public SynchedEntityData.DataValue<T> value() {
212
			return SynchedEntityData.DataValue.create(this.accessor, this.value);
213
		}
214
	}
215
216
	public record DataValue<T>(int id, EntityDataSerializer<T> serializer, T value) {
217
		public static <T> SynchedEntityData.DataValue<T> create(final EntityDataAccessor<T> accessor, final T value) {
218
			EntityDataSerializer<T> serializer = accessor.serializer();
219
			return new SynchedEntityData.DataValue<>(accessor.id(), serializer, serializer.copy(value));
220
		}
221
222
		public void write(final RegistryFriendlyByteBuf output) {
223
			int serializerId = EntityDataSerializers.getSerializedId(this.serializer);
224
			if (serializerId < 0) {
225
				throw new EncoderException("Unknown serializer type " + this.serializer);
226
			}
227
228
			output.writeByte(this.id);
229
			output.writeVarInt(serializerId);
230
			this.serializer.codec().encode(output, this.value);
231
		}
232
233
		public static SynchedEntityData.DataValue<?> read(final RegistryFriendlyByteBuf input, final int id) {
234
			int type = input.readVarInt();
235
			EntityDataSerializer<?> serializer = EntityDataSerializers.getSerializer(type);
236
			if (serializer == null) {
237
				throw new DecoderException("Unknown serializer type " + type);
238
			} else {
239
				return read(input, id, serializer);
240
			}
241
		}
242
243
		private static <T> SynchedEntityData.DataValue<T> read(final RegistryFriendlyByteBuf input, final int id, final EntityDataSerializer<T> serializer) {
244
			return new SynchedEntityData.DataValue<>(id, serializer, serializer.codec().decode(input));
245
		}
246
	}
247
}
248

Decompiled Minecraft 26.3 source of SynchedEntityData (net/minecraft/network/syncher/SynchedEntityData.java) with Mojang official mappings, stored verbatim on the site.