Skip to content

Commit

Permalink
[1.21.3] Add RenderPlayerEvent test and docs fixup (#1304)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhyces authored Nov 10, 2024
1 parent 9129289 commit db945b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
* Fired when a {@link LivingEntity} is rendered.
* See the two subclasses to listen for before and after rendering.
*
* <p>Despite this event's use of generic type parameters, this is not a {@link net.neoforged.bus.api.GenericEvent},
* and should not be treated as such (such as using generic-specific listeners, which may cause a {@link ClassCastException}).</p>
*
* @param <T> the living entity that is being rendered
* @param <M> the model for the living entity
* @see RenderLivingEvent.Pre
Expand Down Expand Up @@ -99,11 +96,11 @@ public int getPackedLight() {
* Fired <b>before</b> an entity is rendered.
* This can be used to render additional effects or suppress rendering.
*
* <p>This event is {@linkplain ICancellableEvent cancelable}, and does not {@linkplain HasResult have a result}.
* <p>This event is {@linkplain ICancellableEvent cancelable}.
* If this event is cancelled, then the entity will not be rendered and the corresponding
* {@link RenderLivingEvent.Post} will not be fired.</p>
*
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus},
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main game event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*
* @param <T> the living entity that is being rendered
Expand All @@ -119,9 +116,9 @@ public Pre(S renderState, LivingEntityRenderer<T, S, M> renderer, float partialT
/**
* Fired <b>after</b> an entity is rendered, if the corresponding {@link RenderLivingEvent.Post} is not cancelled.
*
* <p>This event is not {@linkplain ICancellableEvent cancelable}, and does not {@linkplain HasResult have a result}.</p>
* <p>This event is not {@linkplain ICancellableEvent cancelable}.</p>
*
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus},
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main game event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*
* @param <T> the living entity that was rendered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.player.PlayerRenderer;
import net.minecraft.client.renderer.entity.state.PlayerRenderState;
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.ICancellableEvent;
import net.neoforged.fml.LogicalSide;
import net.neoforged.neoforge.common.NeoForge;
Expand All @@ -31,15 +30,20 @@ protected RenderPlayerEvent(PlayerRenderState renderState, PlayerRenderer render
super(renderState, renderer, partialTick, poseStack, multiBufferSource, packedLight);
}

@Override
public PlayerRenderer getRenderer() {
return (PlayerRenderer) super.getRenderer();
}

/**
* Fired <b>before</b> the player is rendered.
* This can be used for rendering additional effects or suppressing rendering.
*
* <p>This event is {@linkplain ICancellableEvent cancellable}, and does not {@linkplain Event.HasResult have a result}.
* <p>This event is {@linkplain ICancellableEvent cancellable}.
* If this event is cancelled, then the player will not be rendered and the corresponding
* {@link RenderPlayerEvent.Post} will not be fired.</p>
*
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus},
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main game event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
public static class Pre extends RenderPlayerEvent implements ICancellableEvent {
Expand All @@ -52,9 +56,9 @@ public Pre(PlayerRenderState renderState, PlayerRenderer renderer, float partial
/**
* Fired <b>after</b> the player is rendered, if the corresponding {@link RenderPlayerEvent.Pre} is not cancelled.
*
* <p>This event is not {@linkplain ICancellableEvent cancellable}, and does not {@linkplain Event.HasResult have a result}.</p>
* <p>This event is not {@linkplain ICancellableEvent cancellable}.</p>
*
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus},
* <p>This event is fired on the {@linkplain NeoForge#EVENT_BUS main game event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
public static class Post extends RenderPlayerEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
import java.util.Map;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.core.SectionPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.SingleThreadedRandomSource;
import net.neoforged.api.distmarker.Dist;
Expand All @@ -19,6 +24,7 @@
import net.neoforged.neoforge.client.event.ClientPlayerChangeGameTypeEvent;
import net.neoforged.neoforge.client.event.RegisterRenderBuffersEvent;
import net.neoforged.neoforge.client.event.RenderLevelStageEvent;
import net.neoforged.neoforge.client.event.RenderPlayerEvent;
import net.neoforged.neoforge.client.model.data.ModelData;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.testframework.DynamicTest;
Expand Down Expand Up @@ -103,4 +109,21 @@ static void addSectionGeometryTest(final ClientChatEvent chatEvent, final Dynami
test.requestConfirmation(player, Component.literal("Is a diamond block rendered above you?"));
}
}

@TestHolder(description = { "Tests that RenderPlayerEvent is fired correctly and functions as expected" })
static void renderPlayerEvent(final DynamicTest test) {
test.whenEnabled(listeners -> {
var item = Items.IRON_BLOCK;
var itemStack = item.getDefaultInstance();
var modelId = ModelResourceLocation.inventory(BuiltInRegistries.ITEM.getKey(item));
listeners.forge().addListener((final RenderPlayerEvent.Post event) -> {
event.getPoseStack().pushPose();
event.getPoseStack().translate(0, 2, 0);
var model = Minecraft.getInstance().getModelManager().getModel(modelId);
Minecraft.getInstance().getItemRenderer().render(itemStack, ItemDisplayContext.GROUND, false, event.getPoseStack(), event.getMultiBufferSource(), event.getPackedLight(), OverlayTexture.NO_OVERLAY, model);
event.getPoseStack().popPose();
});
test.requestConfirmation(Minecraft.getInstance().player, Component.literal("Is an iron block rendered above you in third-person?"));
});
}
}

0 comments on commit db945b7

Please sign in to comment.