Skip to content

Commit ad71e20

Browse files
committed
Add BlockEntity tick methods
1 parent 5c80421 commit ad71e20

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/main/java/org/spongepowered/common/event/tracking/TrackingUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ public static void tickTileEntity(final TrackedWorldBridge mixinWorldServer, fin
150150
return;
151151
}
152152
final net.minecraft.world.level.block.entity.BlockEntity blockEntity = tickingBlockEntity.get();
153+
if (!((org.spongepowered.api.block.entity.BlockEntity) blockEntity).isTicking()) {
154+
return;
155+
}
153156
final BlockEntityBridge mixinTileEntity = (BlockEntityBridge) tickingBlockEntity.get();
154157
final BlockPos pos = blockEntity.getBlockPos();
155158
final @Nullable LevelChunkBridge chunk = ((ActiveChunkReferantBridge) blockEntity).bridge$getActiveChunk();

src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/block/entity/BlockEntityMixin_API.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ public abstract class BlockEntityMixin_API implements BlockEntity {
6969
//@formatter:on
7070

7171
@Shadow @Final protected BlockPos worldPosition;
72+
@Shadow private net.minecraft.world.level.block.state.BlockState blockState;
73+
7274
@Nullable private LocatableBlock api$LocatableBlock;
75+
private boolean api$canTickRequested = false;
76+
private boolean api$canTick;
77+
private boolean api$isTicking = true;
7378

7479
public ServerLocation location() {
7580
return ServerLocation.of((ServerWorld) this.level, VecHelper.toVector3i(this.shadow$getBlockPos()));
@@ -143,6 +148,34 @@ public void remove() {
143148
}
144149
}
145150

151+
@Override
152+
public boolean canTick() {
153+
if (!this.api$canTickRequested) {
154+
this.api$canTick = this.blockState.getTicker(this.level, this.type) != null;
155+
this.api$canTickRequested = true;
156+
}
157+
158+
return api$canTick;
159+
}
160+
161+
@Override
162+
public boolean isTicking() {
163+
return !this.remove && this.canTick() && this.api$isTicking;
164+
}
165+
166+
@Override
167+
public void setTicking(final boolean ticking) {
168+
if (this.remove) {
169+
throw new IllegalStateException("BlockEntity is removed");
170+
}
171+
172+
if (!this.canTick()) {
173+
throw new IllegalStateException("BlockEntity cannot tick");
174+
}
175+
176+
this.api$isTicking = ticking;
177+
}
178+
146179
@Override
147180
public final BlockEntityType type() {
148181
return (BlockEntityType) this.type;

0 commit comments

Comments
 (0)