Skip to content

Commit 56ca361

Browse files
authored
[1.21.4] Allow modded BE and Entity types to declare that they have NBT data only OPs can set (#1730)
1 parent 8ed5b50 commit 56ca361

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

patches/net/minecraft/world/entity/EntityType.java.patch

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
--- a/net/minecraft/world/entity/EntityType.java
22
+++ b/net/minecraft/world/entity/EntityType.java
3-
@@ -1070,6 +_,10 @@
3+
@@ -1070,6 +_,11 @@
44
private final float spawnDimensionsScale;
55
private final FeatureFlagSet requiredFeatures;
66

77
+ private final java.util.function.Predicate<EntityType<?>> trackDeltasSupplier;
88
+ private final java.util.function.ToIntFunction<EntityType<?>> trackingRangeSupplier;
99
+ private final java.util.function.ToIntFunction<EntityType<?>> updateIntervalSupplier;
10+
+ private final boolean onlyOpCanSetNbt;
1011
+
1112
private static <T extends Entity> EntityType<T> register(ResourceKey<EntityType<?>> p_368669_, EntityType.Builder<T> p_368714_) {
1213
return Registry.register(BuiltInRegistries.ENTITY_TYPE, p_368669_, p_368714_.build(p_368669_));
1314
}
14-
@@ -1106,6 +_,28 @@
15+
@@ -1106,6 +_,29 @@
1516
Optional<ResourceKey<LootTable>> p_368696_,
1617
FeatureFlagSet p_273518_
1718
) {
18-
+ this(p_273268_, p_272918_, p_273417_, p_273389_, p_273556_, p_272654_, p_273631_, p_272946_, p_338404_, p_272895_, p_273451_, p_368582_, p_368696_, p_273518_, EntityType::defaultTrackDeltasSupplier, EntityType::defaultTrackingRangeSupplier, EntityType::defaultUpdateIntervalSupplier);
19+
+ this(p_273268_, p_272918_, p_273417_, p_273389_, p_273556_, p_272654_, p_273631_, p_272946_, p_338404_, p_272895_, p_273451_, p_368582_, p_368696_, p_273518_, EntityType::defaultTrackDeltasSupplier, EntityType::defaultTrackingRangeSupplier, EntityType::defaultUpdateIntervalSupplier, false);
1920
+ }
2021
+
2122
+ public EntityType(
@@ -35,18 +36,20 @@
3536
+ FeatureFlagSet p_273518_,
3637
+ final java.util.function.Predicate<EntityType<?>> trackDeltasSupplier,
3738
+ final java.util.function.ToIntFunction<EntityType<?>> trackingRangeSupplier,
38-
+ final java.util.function.ToIntFunction<EntityType<?>> updateIntervalSupplier
39+
+ final java.util.function.ToIntFunction<EntityType<?>> updateIntervalSupplier,
40+
+ boolean onlyOpCanSetNbt
3941
+ ) {
4042
this.factory = p_273268_;
4143
this.category = p_272918_;
4244
this.canSpawnFarFromPlayer = p_272654_;
43-
@@ -1120,6 +_,9 @@
45+
@@ -1120,6 +_,10 @@
4446
this.descriptionId = p_368582_;
4547
this.lootTable = p_368696_;
4648
this.requiredFeatures = p_273518_;
4749
+ this.trackDeltasSupplier = trackDeltasSupplier;
4850
+ this.trackingRangeSupplier = trackingRangeSupplier;
4951
+ this.updateIntervalSupplier = updateIntervalSupplier;
52+
+ this.onlyOpCanSetNbt = onlyOpCanSetNbt;
5053
}
5154

5255
@Nullable
@@ -88,7 +91,11 @@
8891
return this != PLAYER
8992
&& this != LLAMA_SPIT
9093
&& this != WITHER
91-
@@ -1462,6 +_,8 @@
94+
@@ -1459,9 +_,12 @@
95+
}
96+
97+
public boolean onlyOpCanSetNbt() {
98+
+ if (onlyOpCanSetNbt) return true;
9299
return OP_ONLY_CUSTOM_DATA.contains(this);
93100
}
94101

@@ -97,18 +104,19 @@
97104
public static class Builder<T extends Entity> {
98105
private final EntityType.EntityFactory<T> factory;
99106
private final MobCategory category;
100-
@@ -1481,6 +_,10 @@
107+
@@ -1481,6 +_,11 @@
101108
);
102109
private DependantName<EntityType<?>, String> descriptionId = p_367918_ -> Util.makeDescriptionId("entity", p_367918_.location());
103110

104111
+ private java.util.function.Predicate<EntityType<?>> velocityUpdateSupplier = EntityType::defaultTrackDeltasSupplier;
105112
+ private java.util.function.ToIntFunction<EntityType<?>> trackingRangeSupplier = EntityType::defaultTrackingRangeSupplier;
106113
+ private java.util.function.ToIntFunction<EntityType<?>> updateIntervalSupplier = EntityType::defaultUpdateIntervalSupplier;
114+
+ private boolean onlyOpCanSetNbt = false;
107115
+
108116
private Builder(EntityType.EntityFactory<T> p_20696_, MobCategory p_20697_) {
109117
this.factory = p_20696_;
110118
this.category = p_20697_;
111-
@@ -1593,6 +_,21 @@
119+
@@ -1593,6 +_,26 @@
112120
return this;
113121
}
114122

@@ -126,19 +134,25 @@
126134
+ this.velocityUpdateSupplier = t->value;
127135
+ return this;
128136
+ }
137+
+
138+
+ public EntityType.Builder<T> setOnlyOpCanSetNbt(boolean onlyOpCanSetNbt) {
139+
+ this.onlyOpCanSetNbt = onlyOpCanSetNbt;
140+
+ return this;
141+
+ }
129142
+
130143
public EntityType<T> build(ResourceKey<EntityType<?>> p_368626_) {
131144
if (this.serialize) {
132145
Util.fetchChoiceType(References.ENTITY_TREE, p_368626_.location().toString());
133-
@@ -1612,7 +_,10 @@
146+
@@ -1612,7 +_,11 @@
134147
this.updateInterval,
135148
this.descriptionId.get(p_368626_),
136149
this.lootTable.get(p_368626_),
137150
- this.requiredFeatures
138151
+ this.requiredFeatures,
139-
+ velocityUpdateSupplier,
140-
+ trackingRangeSupplier,
141-
+ updateIntervalSupplier
152+
+ this.velocityUpdateSupplier,
153+
+ this.trackingRangeSupplier,
154+
+ this.updateIntervalSupplier,
155+
+ this.onlyOpCanSetNbt
142156
);
143157
}
144158
}

patches/net/minecraft/world/level/block/entity/BlockEntityType.java.patch

+38-10
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,63 @@
11
--- a/net/minecraft/world/level/block/entity/BlockEntityType.java
22
+++ b/net/minecraft/world/level/block/entity/BlockEntityType.java
3-
@@ -230,6 +_,7 @@
3+
@@ -230,8 +_,11 @@
44
public static final BlockEntityType<VaultBlockEntity> VAULT = register("vault", VaultBlockEntity::new, Blocks.VAULT);
55
private static final Set<BlockEntityType<?>> OP_ONLY_CUSTOM_DATA = Set.of(COMMAND_BLOCK, LECTERN, SIGN, HANGING_SIGN, MOB_SPAWNER, TRIAL_SPAWNER);
66
private final BlockEntityType.BlockEntitySupplier<? extends T> factory;
77
+ // Neo: This field will be modified by BlockEntityTypeAddBlocksEvent event. Please use the event to add to this field for vanilla or other mod's BlockEntityTypes.
88
private final Set<Block> validBlocks;
99
private final Holder.Reference<BlockEntityType<?>> builtInRegistryHolder = BuiltInRegistries.BLOCK_ENTITY_TYPE.createIntrusiveHolder(this);
10+
+ // Neo: Allow modded BE types to declare that they have NBT data only OPs can set
11+
+ private final boolean onlyOpCanSetNbt;
1012

11-
@@ -254,9 +_,24 @@
12-
this.validBlocks = p_155260_;
13+
@Nullable
14+
public static ResourceLocation getKey(BlockEntityType<?> p_58955_) {
15+
@@ -250,8 +_,26 @@
1316
}
1417

18+
public BlockEntityType(BlockEntityType.BlockEntitySupplier<? extends T> p_155259_, Set<Block> p_155260_) {
19+
+ this(p_155259_, p_155260_, false);
20+
+ }
21+
+
22+
+ public BlockEntityType(BlockEntityType.BlockEntitySupplier<? extends T> p_155259_, Set<Block> p_155260_, boolean onlyOpCanSetNbt) {
23+
this.factory = p_155259_;
24+
this.validBlocks = p_155260_;
25+
+ this.onlyOpCanSetNbt = onlyOpCanSetNbt;
26+
+ }
27+
+
1528
+ // Neo: Additional constructor for convenience.
1629
+ public BlockEntityType(BlockEntityType.BlockEntitySupplier<? extends T> p_155259_, Block... p_155260_) {
17-
+ this(p_155259_, Set.of(p_155260_));
30+
+ this(p_155259_, false, p_155260_);
31+
+ }
32+
+
33+
+ // Neo: Additional constructor for convenience.
34+
+ public BlockEntityType(BlockEntityType.BlockEntitySupplier<? extends T> p_155259_, boolean onlyOpCanSetNbt, Block... p_155260_) {
35+
+ this(p_155259_, Set.of(p_155260_), onlyOpCanSetNbt);
1836
+ if (p_155260_.length == 0) {
1937
+ throw new IllegalArgumentException("Block entity type instantiated without valid blocks. If this is intentional, pass Set.of() instead of an empty varag.");
2038
+ }
21-
+ }
22-
+
39+
}
40+
2341
@Nullable
24-
public T create(BlockPos p_155265_, BlockState p_155266_) {
42+
@@ -259,6 +_,13 @@
2543
return (T)this.factory.create(p_155265_, p_155266_);
26-
+ }
27-
+
44+
}
45+
2846
+ /**
2947
+ * Neo: Add getter for an immutable view of the set of valid blocks.
3048
+ */
3149
+ public Set<Block> getValidBlocks() {
3250
+ return java.util.Collections.unmodifiableSet(this.validBlocks);
51+
+ }
52+
+
53+
public boolean isValid(BlockState p_155263_) {
54+
return this.validBlocks.contains(p_155263_.getBlock());
55+
}
56+
@@ -275,6 +_,7 @@
57+
}
58+
59+
public boolean onlyOpCanSetNbt() {
60+
+ if (onlyOpCanSetNbt) return true;
61+
return OP_ONLY_CUSTOM_DATA.contains(this);
3362
}
3463

35-
public boolean isValid(BlockState p_155263_) {

0 commit comments

Comments
 (0)