RenderPlayerEvent in 1.18.2

Started by Object-13 on

Topic category: Help with modding (Java Edition)

Last seen on 00:04, 28. Feb 2023
Joined Feb 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
RenderPlayerEvent in 1.18.2
Tue, 02/21/2023 - 01:24 (edited)

Hello!

Recently I've started toying a bit with MCreator and its tools along with some code I've written on my own, using documentation. What I'm trying to do now is making player COMPLETELY invisible in certain situation - that includes held item(-s) as well as armor. From what I could gather I should use RenderPlayerEvent and create listener for said event. The thing is I'm lost how I should actually connect it to the rest of my code and I'm curious if I can fire it manually somhow, since it seems that both .Pre (which I managed to cancel on load thus making character invisible for the rest of the session) and .Post (that is not cancelable so it's been useless for me so far) can't be triggered by @SubscribeEvent and I cannot make it work mid-session. 
Also IF (and only if, because if that doesn't work I'll need to look for some workaround anyway) 3rd-person model view can be invisible by stoping renderer, can the same be done to 1st-person model?

Any help witll be much appreciated and thanks in advance,
Object-13

Edited by Object-13 on Tue, 02/21/2023 - 01:24
Last seen on 00:04, 28. Feb 2023
Joined Feb 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Nevermind guys, I managed to…
Tue, 02/21/2023 - 15:37

Nevermind guys, I managed to figure it out. I don't know if it's good solution (I should probably make the code more secure) but for now I think it may be a good start. I'm gonna leave my class here for future references, maybe it'll let someone save taht hours of researching how it actually works.
 

@Mod.EventBusSubscriber

public class RenderPlayer {
@SubscribeEvent
public static void renderPlayerPre(RenderPlayerEvent.Pre event) {
renderPre(event);
}

public static void renderPre(RenderPlayerEvent.Pre event) {
Player player = event.getPlayer();
if (/* your own condition */) {
event.setCanceled(true);
}
}

@SubscribeEvent
public static void renderPlayerPost(RenderPlayerEvent.Post event) {
renderPost(event);
}

public static void renderPost(RenderPlayerEvent.Post event) {
Player player = event.getPlayer();
PlayerRenderer renderer = event.getRenderer();
PlayerModel<AbstractClientPlayer> model = renderer.getModel();
if (/* your own condition */) {
renderer.render((LocalPlayer) player, player.getYRot(), event.getPartialTick(), event.getPoseStack(), event.getMultiBufferSource(),
event.getPackedLight());
}
}
}