Skip to content

Commit

Permalink
[1.20.5] Refactor tick events into distinct subclasses (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadows-of-Fire authored Apr 27, 2024
1 parent e6c1c91 commit 53df031
Show file tree
Hide file tree
Showing 25 changed files with 519 additions and 242 deletions.
24 changes: 12 additions & 12 deletions patches/net/minecraft/client/Minecraft.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@
this.profiler.pop();
if (!this.noRender) {
+ this.realPartialTick = this.pause ? this.pausePartialTick : this.timer.partialTick; // Cache this since pause is volatile
+ net.neoforged.neoforge.event.EventHooks.onRenderTickStart(this.realPartialTick);
+ net.neoforged.neoforge.client.ClientHooks.fireRenderFramePre(this.realPartialTick);
this.profiler.popPush("gameRenderer");
this.gameRenderer.render(this.pause ? this.pausePartialTick : this.timer.partialTick, i, p_91384_);
this.profiler.pop();
+ net.neoforged.neoforge.event.EventHooks.onRenderTickEnd(this.realPartialTick);
+ net.neoforged.neoforge.client.ClientHooks.fireRenderFramePost(this.realPartialTick);
}

if (this.fpsPieResults != null) {
Expand Down Expand Up @@ -326,28 +326,28 @@
if (!itemstack.isEmpty()) {
InteractionResult interactionresult2 = this.gameMode.useItem(this.player, interactionhand);
if (interactionresult2.consumesAction()) {
@@ -1808,6 +_,8 @@
this.rightClickDelay--;
}
@@ -1800,6 +_,8 @@

+ net.neoforged.neoforge.event.EventHooks.onPreClientTick();
public void tick() {
this.clientTickCount++;
+ net.neoforged.neoforge.client.ClientHooks.fireClientTickPre();
+
this.profiler.push("gui");
this.chatListener.tick();
this.gui.tick(this.pause);
if (this.level != null && !this.pause) {
this.level.tickRateManager().tick();
}
@@ -1892,6 +_,7 @@

this.tutorial.tick();

+ net.neoforged.neoforge.event.EventHooks.onPreLevelTick(this.level, () -> true);
+ net.neoforged.neoforge.event.EventHooks.fireLevelTickPre(this.level, () -> true);
try {
this.level.tick(() -> true);
} catch (Throwable throwable) {
@@ -1905,6 +_,7 @@

throw new ReportedException(crashreport);
}
+ net.neoforged.neoforge.event.EventHooks.onPostLevelTick(this.level, () -> true);
+ net.neoforged.neoforge.event.EventHooks.fireLevelTickPost(this.level, () -> true);
}

this.profiler.popPush("animateTick");
Expand All @@ -356,7 +356,7 @@
this.keyboardHandler.tick();
this.profiler.pop();
+
+ net.neoforged.neoforge.event.EventHooks.onPostClientTick();
+ net.neoforged.neoforge.client.ClientHooks.fireClientTickPost();
}

private boolean isMultiplayerServer() {
Expand Down
10 changes: 7 additions & 3 deletions patches/net/minecraft/client/multiplayer/ClientLevel.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@
}

public void queueLightUpdate(Runnable p_194172_) {
@@ -282,6 +_,7 @@
@@ -282,7 +_,10 @@
p_104640_.setOldPosAndRot();
p_104640_.tickCount++;
this.getProfiler().push(() -> BuiltInRegistries.ENTITY_TYPE.getKey(p_104640_.getType()).toString());
+ if (p_104640_.canUpdate())
p_104640_.tick();
- p_104640_.tick();
+ // Neo: Permit cancellation of Entity#tick via EntityTickEvent.Pre
+ if (!net.neoforged.neoforge.event.EventHooks.fireEntityTickPre(p_104640_).isCanceled()) {
+ p_104640_.tick();
+ }
this.getProfiler().pop();

for (Entity entity : p_104640_.getPassengers()) {
@@ -330,8 +_,10 @@
}

Expand Down
11 changes: 5 additions & 6 deletions patches/net/minecraft/server/MinecraftServer.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@
this.onServerExit();
}
}
@@ -876,12 +_,14 @@

@@ -877,11 +_,13 @@
public void tickServer(BooleanSupplier p_129871_) {
long i = Util.getNanos();
+ net.neoforged.neoforge.event.EventHooks.onPreServerTick(p_129871_, this);
this.tickCount++;
+ net.neoforged.neoforge.event.EventHooks.fireServerTickPre(p_129871_, this);
this.tickRateManager.tick();
this.tickChildren(p_129871_);
if (i - this.lastServerStatus >= STATUS_EXPIRE_TIME_NANOS) {
Expand All @@ -105,7 +104,7 @@
this.smoothedTickTimeMillis = this.smoothedTickTimeMillis * 0.8F + (float)j / (float)TimeUtil.NANOSECONDS_PER_MILLISECOND * 0.19999999F;
this.logTickMethodTime(i);
this.profiler.pop();
+ net.neoforged.neoforge.event.EventHooks.onPostServerTick(p_129871_, this);
+ net.neoforged.neoforge.event.EventHooks.fireServerTickPost(p_129871_, this);
}

private void logTickMethodTime(long p_321837_) {
Expand Down Expand Up @@ -150,15 +149,15 @@
}

this.profiler.push("tick");
+ net.neoforged.neoforge.event.EventHooks.onPreLevelTick(serverlevel, p_129954_);
+ net.neoforged.neoforge.event.EventHooks.fireLevelTickPre(serverlevel, p_129954_);

try {
serverlevel.tick(p_129954_);
@@ -989,16 +_,18 @@
serverlevel.fillReportDetails(crashreport);
throw new ReportedException(crashreport);
}
+ net.neoforged.neoforge.event.EventHooks.onPostLevelTick(serverlevel, p_129954_);
+ net.neoforged.neoforge.event.EventHooks.fireLevelTickPost(serverlevel, p_129954_);

this.profiler.pop();
this.profiler.pop();
Expand Down
18 changes: 11 additions & 7 deletions patches/net/minecraft/server/level/ServerLevel.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@
}
}

@@ -774,6 +_,7 @@
ProfilerFiller profilerfiller = this.getProfiler();
profilerfiller.push(() -> BuiltInRegistries.ENTITY_TYPE.getKey(p_8664_.getType()).toString());
profilerfiller.incrementCounter("tickPassenger");
+ if (p_8664_.canUpdate())
p_8664_.rideTick();
profilerfiller.pop();
@@ -757,7 +_,10 @@
p_8648_.tickCount++;
this.getProfiler().push(() -> BuiltInRegistries.ENTITY_TYPE.getKey(p_8648_.getType()).toString());
profilerfiller.incrementCounter("tickNonPassenger");
- p_8648_.tick();
+ // Neo: Permit cancellation of Entity#tick via EntityTickEvent.Pre
+ if (!net.neoforged.neoforge.event.EventHooks.fireEntityTickPre(p_8648_).isCanceled()) {
+ p_8648_.tick();
+ }
this.getProfiler().pop();

for (Entity entity : p_8648_.getPassengers()) {
@@ -806,6 +_,7 @@
} else {
this.entityManager.autoSave();
Expand Down
33 changes: 9 additions & 24 deletions patches/net/minecraft/world/entity/Entity.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,6 @@
}

public void moveRelative(float p_19921_, Vec3 p_19922_) {
@@ -1645,6 +_,8 @@
p_20241_.putBoolean("HasVisualFire", this.hasVisualFire);
}

+ p_20241_.putBoolean("CanUpdate", canUpdate);
+
if (!this.tags.isEmpty()) {
ListTag listtag = new ListTag();

@@ -1655,6 +_,10 @@
p_20241_.put("Tags", listtag);
}
Expand All @@ -218,12 +209,11 @@
this.addAdditionalSaveData(p_20241_);
if (this.isVehicle()) {
ListTag listtag1 = new ListTag();
@@ -1735,6 +_,9 @@
@@ -1735,6 +_,8 @@
this.setGlowingTag(p_20259_.getBoolean("Glowing"));
this.setTicksFrozen(p_20259_.getInt("TicksFrozen"));
this.hasVisualFire = p_20259_.getBoolean("HasVisualFire");
+ if (p_20259_.contains("NeoForgeData", 10)) persistentData = p_20259_.getCompound("NeoForgeData");
+ if (p_20259_.contains("CanUpdate", 99)) this.canUpdate(p_20259_.getBoolean("CanUpdate"));
+ if (p_20259_.contains(ATTACHMENTS_NBT_KEY, net.minecraft.nbt.Tag.TAG_COMPOUND)) deserializeAttachments(registryAccess(), p_20259_.getCompound(ATTACHMENTS_NBT_KEY));
if (p_20259_.contains("Tags", 9)) {
this.tags.clear();
Expand All @@ -237,14 +227,18 @@
this.level().addFreshEntity(itementity);
return itementity;
}
@@ -1865,6 +_,7 @@
@@ -1865,7 +_,10 @@

public void rideTick() {
this.setDeltaMovement(Vec3.ZERO);
+ if (canUpdate())
this.tick();
- this.tick();
+ // Neo: Permit cancellation of Entity#tick via EntityTickEvent.Pre
+ if (!net.neoforged.neoforge.event.EventHooks.fireEntityTickPre(this).isCanceled()) {
+ this.tick();
+ }
if (this.isPassenger()) {
this.getVehicle().positionRider(this);
}
@@ -1923,6 +_,7 @@
}
}
Expand Down Expand Up @@ -495,22 +489,13 @@
}

public void checkDespawn() {
@@ -3424,6 +_,126 @@
@@ -3424,6 +_,117 @@
public boolean mayInteract(Level p_146843_, BlockPos p_146844_) {
return true;
}
+
+ /* ================================== Forge Start =====================================*/
+
+ private boolean canUpdate = true;
+ @Override
+ public void canUpdate(boolean value) {
+ this.canUpdate = value;
+ }
+ @Override
+ public boolean canUpdate() {
+ return this.canUpdate;
+ }
+ @Nullable
+ private java.util.Collection<ItemEntity> captureDrops = null;
+ @Override
Expand Down
8 changes: 0 additions & 8 deletions patches/net/minecraft/world/entity/LivingEntity.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,6 @@
d2 = 0.0;
}

@@ -2334,6 +_,7 @@

@Override
public void tick() {
+ if (net.neoforged.neoforge.common.CommonHooks.onLivingTick(this)) return;
super.tick();
this.updatingUsingItem();
this.updateSwimAmount();
@@ -2483,6 +_,7 @@
};
ItemStack itemstack1 = this.getItemBySlot(equipmentslot);
Expand Down
4 changes: 2 additions & 2 deletions patches/net/minecraft/world/entity/player/Player.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

@Override
public void tick() {
+ net.neoforged.neoforge.event.EventHooks.onPlayerPreTick(this);
+ net.neoforged.neoforge.event.EventHooks.firePlayerTickPre(this);
this.noPhysics = this.isSpectator();
if (this.isSpectator()) {
this.setOnGround(false);
Expand All @@ -52,7 +52,7 @@
this.turtleHelmetTick();
this.cooldowns.tick();
this.updatePlayerPose();
+ net.neoforged.neoforge.event.EventHooks.onPlayerPostTick(this);
+ net.neoforged.neoforge.event.EventHooks.firePlayerTickPost(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.fml.ModLoader;
import net.neoforged.neoforge.common.NeoForgeMod;
import net.neoforged.neoforge.event.TickEvent;
import net.neoforged.neoforge.event.level.ChunkEvent;
import net.neoforged.neoforge.event.tick.LevelTickEvent;
import net.neoforged.neoforge.fluids.capability.wrappers.FluidBucketWrapper;
import net.neoforged.neoforge.items.VanillaHopperItemHandler;
import net.neoforged.neoforge.items.wrapper.CombinedInvWrapper;
Expand Down Expand Up @@ -152,20 +152,20 @@ else if (entity instanceof LivingEntity livingEntity)
}

public static void invalidateCapsOnChunkLoad(ChunkEvent.Load event) {
if (!event.getLevel().isClientSide()) {
((ServerLevel) event.getLevel()).invalidateCapabilities(event.getChunk().getPos());
if (event.getLevel() instanceof ServerLevel sl) {
sl.invalidateCapabilities(event.getChunk().getPos());
}
}

public static void invalidateCapsOnChunkUnload(ChunkEvent.Unload event) {
if (!event.getLevel().isClientSide()) {
((ServerLevel) event.getLevel()).invalidateCapabilities(event.getChunk().getPos());
if (event.getLevel() instanceof ServerLevel sl) {
sl.invalidateCapabilities(event.getChunk().getPos());
}
}

public static void cleanCapabilityListenerReferencesOnTick(TickEvent.LevelTickEvent event) {
if (event.phase == TickEvent.Phase.END && event.side.isServer()) {
((ServerLevel) event.level).cleanCapabilityListenerReferences();
public static void cleanCapabilityListenerReferencesOnTick(LevelTickEvent.Post event) {
if (event.getLevel() instanceof ServerLevel sl) {
sl.cleanCapabilityListenerReferences();
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/net/neoforged/neoforge/client/ClientHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
import net.neoforged.neoforge.client.event.ClientPauseUpdatedEvent;
import net.neoforged.neoforge.client.event.ClientPlayerChangeGameTypeEvent;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.client.event.ComputeFovModifierEvent;
import net.neoforged.neoforge.client.event.CustomizeGuiOverlayEvent;
import net.neoforged.neoforge.client.event.EntityRenderersEvent;
Expand All @@ -149,6 +150,7 @@
import net.neoforged.neoforge.client.event.RegisterSpriteSourceTypesEvent;
import net.neoforged.neoforge.client.event.RenderArmEvent;
import net.neoforged.neoforge.client.event.RenderBlockScreenEffectEvent;
import net.neoforged.neoforge.client.event.RenderFrameEvent;
import net.neoforged.neoforge.client.event.RenderHandEvent;
import net.neoforged.neoforge.client.event.RenderHighlightEvent;
import net.neoforged.neoforge.client.event.RenderLevelStageEvent;
Expand Down Expand Up @@ -1003,4 +1005,40 @@ public static void initClientHooks(Minecraft mc, ReloadableResourceManager resou
ItemDecoratorHandler.init();
PresetEditorManager.init();
}

/**
* Fires {@link RenderFrameEvent.Pre}. Called just before {@link GameRenderer#render(float, long, boolean)} in {@link Minecraft#runTick(boolean)}.
* <p>
* Fired before the profiler section for "gameRenderer" is started.
*
* @param partialTick The current partial tick
*/
public static void fireRenderFramePre(float partialTick) {
NeoForge.EVENT_BUS.post(new RenderFrameEvent.Pre(partialTick));
}

/**
* Fires {@link RenderFrameEvent.Post}. Called just after {@link GameRenderer#render(float, long, boolean)} in {@link Minecraft#runTick(boolean)}.
* <p>
* Fired after the profiler section for "gameRenderer" is ended.
*
* @param partialTick The current partial tick
*/
public static void fireRenderFramePost(float partialRick) {
NeoForge.EVENT_BUS.post(new RenderFrameEvent.Post(partialRick));
}

/**
* Fires {@link ClientTickEvent.Pre}. Called from the head of {@link Minecraft#tick()}.
*/
public static void fireClientTickPre() {
NeoForge.EVENT_BUS.post(new ClientTickEvent.Pre());
}

/**
* Fires {@link ClientTickEvent.Post}. Called from the tail of {@link Minecraft#tick()}.
*/
public static void fireClientTickPost() {
NeoForge.EVENT_BUS.post(new ClientTickEvent.Post());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.client.event;

import net.neoforged.bus.api.Event;

/**
* Base class of the two client tick events.
* <p>
* For the event that fires once per frame (instead of per tick), see {@link RenderFrameEvent}.
*
* @see ClientTickEvent.Pre
* @see ClientTickEvent.Post
*/
public abstract class ClientTickEvent extends Event {
/**
* {@link ClientTickEvent.Pre} is fired once per client tick, before the client performs work for the current tick.
* <p>
* This event only fires on the physical client.
*/
public static class Pre extends ClientTickEvent {
public Pre() {}
}

/**
* {@link ClientTickEvent.Post} is fired once per client tick, after the client performs work for the current tick.
* <p>
* This event only fires on the physical client.
*/
public static class Post extends ClientTickEvent {
public Post() {}
}
}
Loading

0 comments on commit 53df031

Please sign in to comment.