Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MinecraftForge
GitHub Repository: MinecraftForge/MinecraftForge
Path: blob/1.21.x/patches/minecraft/net/minecraft/world/level/chunk/LevelChunk.java.patch
7479 views
1
--- a/net/minecraft/world/level/chunk/LevelChunk.java
2
+++ b/net/minecraft/world/level/chunk/LevelChunk.java
3
@@ -62,7 +_,7 @@
4
import net.minecraft.world.ticks.TickContainerAccess;
5
import org.slf4j.Logger;
6
7
-public class LevelChunk extends ChunkAccess implements DebugValueSource {
8
+public class LevelChunk extends ChunkAccess implements DebugValueSource, net.minecraftforge.common.capabilities.ICapabilityProviderImpl<LevelChunk> {
9
static final Logger LOGGER = LogUtils.getLogger();
10
private static final TickingBlockEntity NULL_TICKER = new TickingBlockEntity() {
11
@Override
12
@@ -124,6 +_,7 @@
13
this.postLoad = p_196861_;
14
this.blockTicks = p_196857_;
15
this.fluidTicks = p_196858_;
16
+ this.capProvider.initInternal();
17
}
18
19
public LevelChunk(ServerLevel p_196850_, ProtoChunk p_196851_, @Nullable LevelChunk.PostLoadProcessor p_196852_) {
20
@@ -321,7 +_,7 @@
21
if (!levelchunksection.getBlockState(j, k, l).is(block)) {
22
return null;
23
} else {
24
- if (!this.level.isClientSide() && (p_393998_ & 512) == 0) {
25
+ if (!this.level.isClientSide() && (p_393998_ & 512) == 0 && !this.level.captureBlockSnapshots) {
26
p_62866_.onPlace(this.level, p_62865_, blockstate, flag2);
27
}
28
29
@@ -376,6 +_,12 @@
30
@Nullable
31
public BlockEntity getBlockEntity(BlockPos p_62868_, LevelChunk.EntityCreationType p_62869_) {
32
BlockEntity blockentity = this.blockEntities.get(p_62868_);
33
+
34
+ if (blockentity != null && blockentity.isRemoved()) {
35
+ blockEntities.remove(p_62868_);
36
+ blockentity = null;
37
+ }
38
+
39
if (blockentity == null) {
40
CompoundTag compoundtag = this.pendingBlockEntities.remove(p_62868_);
41
if (compoundtag != null) {
42
@@ -393,9 +_,6 @@
43
this.addAndRegisterBlockEntity(blockentity);
44
}
45
}
46
- } else if (blockentity.isRemoved()) {
47
- this.blockEntities.remove(p_62868_);
48
- return null;
49
}
50
51
return blockentity;
52
@@ -410,6 +_,7 @@
53
54
this.level.onBlockEntityAdded(p_156391_);
55
this.updateBlockEntityTicker(p_156391_);
56
+ p_156391_.onLoad();
57
}
58
}
59
60
@@ -462,9 +_,14 @@
61
public CompoundTag getBlockEntityNbtForSaving(BlockPos p_62932_, HolderLookup.Provider p_329605_) {
62
BlockEntity blockentity = this.getBlockEntity(p_62932_);
63
if (blockentity != null && !blockentity.isRemoved()) {
64
+ try {
65
CompoundTag compoundtag1 = blockentity.saveWithFullMetadata(this.level.registryAccess());
66
compoundtag1.putBoolean("keepPacked", false);
67
return compoundtag1;
68
+ } catch (Exception e) {
69
+ LOGGER.error("A BlockEntity type {} has thrown an exception trying to write state. It will not persist, Report this to the mod author", blockentity.getClass().getName(), e);
70
+ return null;
71
+ }
72
} else {
73
CompoundTag compoundtag = this.pendingBlockEntities.get(p_62932_);
74
if (compoundtag != null) {
75
@@ -544,9 +_,10 @@
76
(p_405747_, p_405748_, p_405749_) -> {
77
BlockEntity blockentity = this.getBlockEntity(p_405747_, LevelChunk.EntityCreationType.IMMEDIATE);
78
if (blockentity != null && p_405749_ != null && blockentity.getType() == p_405748_) {
79
- blockentity.loadWithComponents(
80
+ var input = (
81
TagValueInput.create(problemreporter$scopedcollector.forChild(blockentity.problemPath()), this.level.registryAccess(), p_405749_)
82
);
83
+ blockentity.handleUpdateTag(input, this.level.registryAccess());
84
}
85
}
86
);
87
@@ -684,6 +_,7 @@
88
}
89
90
public void clearAllBlockEntities() {
91
+ this.blockEntities.values().forEach(BlockEntity::onChunkUnloaded);
92
this.blockEntities.values().forEach(BlockEntity::setRemoved);
93
this.blockEntities.clear();
94
this.tickersInLevel.values().forEach(p_187966_ -> p_187966_.rebind(NULL_TICKER));
95
@@ -691,6 +_,7 @@
96
}
97
98
public void registerAllBlockEntitiesAfterLevelLoad() {
99
+ this.level.addFreshBlockEntities(this.blockEntities.values());
100
this.blockEntities.values().forEach(p_405750_ -> {
101
if (this.level instanceof ServerLevel serverlevel) {
102
this.addGameEventListener(p_405750_, serverlevel);
103
@@ -743,6 +_,24 @@
104
return new LevelChunk.BoundTickingBlockEntity<>(p_156376_, p_156377_);
105
}
106
107
+ private final net.minecraftforge.common.capabilities.CapabilityProvider.AsField.LevelChunks capProvider = new net.minecraftforge.common.capabilities.CapabilityProvider.AsField.LevelChunks(this);
108
+
109
+ @org.jetbrains.annotations.NotNull
110
+ @Override
111
+ public <T> net.minecraftforge.common.util.LazyOptional<T> getCapability(@org.jetbrains.annotations.NotNull net.minecraftforge.common.capabilities.Capability<T> cap, @org.jetbrains.annotations.Nullable net.minecraft.core.Direction side) {
112
+ return capProvider.getCapability(cap, side);
113
+ }
114
+
115
+ @Override
116
+ public void invalidateCaps() {
117
+ capProvider.invalidateCaps();
118
+ }
119
+
120
+ @Override
121
+ public void reviveCaps() {
122
+ capProvider.reviveCaps();
123
+ }
124
+
125
class BoundTickingBlockEntity<T extends BlockEntity> implements TickingBlockEntity {
126
private final T blockEntity;
127
private final BlockEntityTicker<T> ticker;
128
@@ -760,6 +_,7 @@
129
if (LevelChunk.this.isTicking(blockpos)) {
130
try {
131
ProfilerFiller profilerfiller = Profiler.get();
132
+ net.minecraftforge.server.timings.TimeTracker.BLOCK_ENTITY_UPDATE.trackStart(blockEntity);
133
profilerfiller.push(this::getType);
134
BlockState blockstate = LevelChunk.this.getBlockState(blockpos);
135
if (this.blockEntity.getType().isValid(blockstate)) {
136
@@ -781,6 +_,11 @@
137
CrashReport crashreport = CrashReport.forThrowable(throwable, "Ticking block entity");
138
CrashReportCategory crashreportcategory = crashreport.addCategory("Block entity being ticked");
139
this.blockEntity.fillCrashReportCategory(crashreportcategory);
140
+ if (net.minecraftforge.common.ForgeConfig.SERVER.removeErroringBlockEntities.get()) {
141
+ LOGGER.error("{}", crashreport.getFriendlyReport(net.minecraft.ReportType.CRASH));
142
+ blockEntity.setRemoved();
143
+ LevelChunk.this.removeBlockEntity(blockEntity.getBlockPos());
144
+ } else
145
throw new ReportedException(crashreport);
146
}
147
}
148
@@ -854,6 +_,33 @@
149
public String toString() {
150
return this.ticker + " <wrapped>";
151
}
152
+ }
153
+
154
+ /**
155
+ * <strong>FOR INTERNAL USE ONLY</strong>
156
+ * <p>
157
+ * Only public for use in {@link net.minecraft.world.level.chunk.storage.ChunkSerializer}.
158
+ */
159
+ @java.lang.Deprecated
160
+ @org.jetbrains.annotations.Nullable
161
+ public final CompoundTag writeCapsToNBT(HolderLookup.Provider registryAccess) {
162
+ return capProvider.serializeInternal(registryAccess);
163
+ }
164
+
165
+ /**
166
+ * <strong>FOR INTERNAL USE ONLY</strong>
167
+ * <p>
168
+ * Only public for use in {@link net.minecraft.world.level.chunk.storage.ChunkSerializer}.
169
+ *
170
+ */
171
+ @java.lang.Deprecated
172
+ public final void readCapsFromNBT(HolderLookup.Provider registryAccess, CompoundTag tag) {
173
+ capProvider.deserializeInternal(registryAccess, tag);
174
+ }
175
+
176
+ @Override
177
+ public Level getWorldForge() {
178
+ return getLevel();
179
}
180
181
@FunctionalInterface
182
183