Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Jan 26, 2024

Verified

This commit was signed with the committer’s verified signature.
tr7zw tr7zw
1 parent 555eb42 commit 6d4b876
Showing 14 changed files with 69 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@

public interface ModelPartBase {

public void setHidden();
void setHidden();

public void showAgain();
void showAgain();

public boolean isHidden();
boolean isHidden();

}
Original file line number Diff line number Diff line change
@@ -60,8 +60,9 @@ public void onTick() {
lateInit();
}
if (keyBinding.isDown()) {
if (isHeld)
if (isHeld) {
return;
}
isHeld = true;
setEnabled(!isEnabled());
} else {
58 changes: 27 additions & 31 deletions src/main/java/dev/tr7zw/firstperson/LogicHandler.java
Original file line number Diff line number Diff line change
@@ -34,30 +34,26 @@ public class LogicHandler {
private Set<Item> autoVanillaHandItems = new HashSet<>();

void registerDefaultHandlers() {
FirstPersonAPI.registerPlayerHandler(new ActivationHandler() {

@Override
public boolean preventFirstperson() {
if (client.player.isAutoSpinAttack())
return true;
if (client.player.isFallFlying())
return true;
if (client.player.getSwimAmount(1f) != 0 && !isCrawlingOrSwimming(client.player))
return true;
// spotless:off
//#if MC >= 11700
if (client.player.isScoping())
return true;
//#endif
//spotless:on
return false;
FirstPersonAPI.registerPlayerHandler((ActivationHandler) () -> {
if (client.player.isAutoSpinAttack() || client.player.isFallFlying()
|| (client.player.getSwimAmount(1f) != 0 && !isCrawlingOrSwimming(client.player))) {
return true;
}
// spotless:off
//#if MC >= 11700
if (client.player.isScoping()) {
return true;
}
//#endif
//spotless:on
return false;
});
}

public boolean shouldApplyThirdPerson(boolean thirdPerson) {
if (!fpm.isEnabled() || thirdPerson)
if (!fpm.isEnabled() || thirdPerson) {
return false;
}
for (ActivationHandler handler : FirstPersonAPI.getActivationHandlers()) {
if (handler.preventFirstperson()) {
return false;
@@ -77,14 +73,13 @@ public void updatePositionOffset(Entity entity, Vec3 defValue) {
double z = 0;
AbstractClientPlayer player;
double realYaw;
if (entity == client.player && client.options.getCameraType() == CameraType.FIRST_PERSON
&& fpm.isRenderingPlayer()) {
player = (AbstractClientPlayer) entity;
realYaw = Mth.rotLerp(client.getFrameTime(), player.yBodyRotO, player.yBodyRot);
} else {
if ((entity != client.player) || (client.options.getCameraType() != CameraType.FIRST_PERSON)
|| !fpm.isRenderingPlayer()) {
offset = defValue;
return;
}
player = (AbstractClientPlayer) entity;
realYaw = Mth.rotLerp(client.getFrameTime(), player.yBodyRotO, player.yBodyRot);
if (!player.isLocalPlayer() || client.getCameraEntity() == player) {
float bodyOffset;
if (isCrawlingOrSwimming(client.player)) {
@@ -94,9 +89,9 @@ public void updatePositionOffset(Entity entity, Vec3 defValue) {
} else {
bodyOffset = Constants.SWIM_DOWN_BODY_OFFSET;
}
// some mods seem to break the isCrouching method
} else if (player.isCrouching() || player.getPose() == Pose.CROUCHING) {
bodyOffset = Constants.SNEAK_BODY_OFFSET + (fpm.getConfig().sneakXOffset / 100f);
// some mods seem to break the isCrouching method
} else if (player.isCrouching() || player.getPose() == Pose.CROUCHING) {
bodyOffset = Constants.SNEAK_BODY_OFFSET + fpm.getConfig().sneakXOffset / 100f;
} else if (player.isPassenger()) {
if (player.getVehicle() instanceof Boat || player.getVehicle() instanceof Minecart) {
realYaw = Mth.rotLerp(client.getFrameTime(), player.yBodyRotO, player.yBodyRot);
@@ -106,9 +101,9 @@ public void updatePositionOffset(Entity entity, Vec3 defValue) {
realYaw = Mth.rotLerp(client.getFrameTime(), player.getVehicle().yRotO,
NMSHelper.getYRot(player.getVehicle()));
}
bodyOffset = Constants.IN_VEHICLE_BODY_OFFSET + (fpm.getConfig().sitXOffset / 100f);
bodyOffset = Constants.IN_VEHICLE_BODY_OFFSET + fpm.getConfig().sitXOffset / 100f;
} else {
bodyOffset = 0.25f + (fpm.getConfig().xOffset / 100f);
bodyOffset = 0.25f + fpm.getConfig().xOffset / 100f;
}
x += bodyOffset * Math.sin(Math.toRadians(realYaw));
z -= bodyOffset * Math.cos(Math.toRadians(realYaw));
@@ -151,11 +146,11 @@ public boolean showVanillaHands() {
}

public void addAutoVanillaHandsItem(Item item) {
this.autoVanillaHandItems.add(item);
autoVanillaHandItems.add(item);
}

public void clearAutoVanillaHandsList() {
this.autoVanillaHandItems.clear();
autoVanillaHandItems.clear();
}

public void reloadAutoVanillaHandsSettings() {
@@ -164,8 +159,9 @@ public void reloadAutoVanillaHandsSettings() {
for (String itemId : fpm.getConfig().autoVanillaHands) {
try {
Item item = NMSHelper.getItem(new ResourceLocation(itemId.split(":")[0], itemId.split(":")[1]));
if (invalid != item)
if (invalid != item) {
addAutoVanillaHandsItem(item);
}
} catch (Exception ex) {
FirstPersonBase.LOGGER.info("Unknown item to add to the auto vanilla hold list: {}", itemId);
}
Original file line number Diff line number Diff line change
@@ -4,6 +4,6 @@

public interface PlayerModelAccess {

public ModelPart getCloak();
ModelPart getCloak();

}
Original file line number Diff line number Diff line change
@@ -2,5 +2,5 @@

public interface ActivationHandler {

public boolean preventFirstperson();
boolean preventFirstperson();
}
6 changes: 4 additions & 2 deletions src/main/java/dev/tr7zw/firstperson/api/FirstPersonAPI.java
Original file line number Diff line number Diff line change
@@ -50,10 +50,12 @@ public static boolean isRenderingPlayer() {
* @param handler
*/
public static void registerPlayerHandler(Object handler) {
if (handler instanceof PlayerOffsetHandler offset)
if (handler instanceof PlayerOffsetHandler offset) {
playerOffsetHandlers.add(offset);
if (handler instanceof ActivationHandler activation)
}
if (handler instanceof ActivationHandler activation) {
activationHandlers.add(activation);
}
}

/**
Original file line number Diff line number Diff line change
@@ -17,6 +17,6 @@ public interface PlayerOffsetHandler {
* @param current
* @return
*/
public Vec3 applyOffset(AbstractClientPlayer entity, float delta, Vec3 original, Vec3 current);
Vec3 applyOffset(AbstractClientPlayer entity, float delta, Vec3 original, Vec3 current);

}
Original file line number Diff line number Diff line change
@@ -39,8 +39,8 @@ private void renderArmor(PoseStack matrices, MultiBufferSource vertexConsumers,
info.cancel();
}
if (equipmentSlot == EquipmentSlot.CHEST && FirstPersonModelCore.instance.isRenderingPlayer()
&& (livingEntity instanceof LocalPlayer player
&& FirstPersonModelCore.instance.getLogicHandler().isSwimming(player))) {
&& livingEntity instanceof LocalPlayer player
&& FirstPersonModelCore.instance.getLogicHandler().isSwimming(player)) {
info.cancel();
}
if (equipmentSlot == EquipmentSlot.CHEST && FirstPersonModelCore.instance.isRenderingPlayer()
Original file line number Diff line number Diff line change
@@ -30,29 +30,29 @@ private boolean doCorrect() {

@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Options;getCameraType()Lnet/minecraft/client/CameraType;"))
private CameraType redirect(Options gameOptions) {
return (doCorrect()) ? CameraType.THIRD_PERSON_BACK : gameOptions.getCameraType();
return doCorrect() ? CameraType.THIRD_PERSON_BACK : gameOptions.getCameraType();
}

@Inject(method = "render", at = @At("HEAD"))
private void calcOffset(FishingHook fishingBobberEntity, float f, float g, PoseStack matrixStack,
MultiBufferSource vertexConsumerProvider, int i, CallbackInfo info) {
if (fishingBobberEntity.getOwner() instanceof Player) {
this.offsetvec3d = FirstPersonModelCore.instance.getLogicHandler().getOffset();// getPositionOffset((Player)
// fishingBobberEntity.getOwner(),
// matrixStack);
offsetvec3d = FirstPersonModelCore.instance.getLogicHandler().getOffset();// getPositionOffset((Player)
// fishingBobberEntity.getOwner(),
// matrixStack);
} else {
this.offsetvec3d = new Vec3(0, 0, 0);
offsetvec3d = new Vec3(0, 0, 0);
}
}

@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getX()D"))
private double offsetX(Player playerEntity) {
return playerEntity.getX() + this.offsetvec3d.x();
return playerEntity.getX() + offsetvec3d.x();
}

@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getZ()D"))
private double offsetZ(Player playerEntity) {
return playerEntity.getZ() + this.offsetvec3d.z();
return playerEntity.getZ() + offsetvec3d.z();
}

@Redirect(method = "render", at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/player/Player;xo:D"))
Original file line number Diff line number Diff line change
@@ -40,17 +40,18 @@ public void renderFirstPersonItem(AbstractClientPlayer player, float tickDelta,
|| player.getMainHandItem().getItem() == Items.FILLED_MAP
// spotless:off
//#if MC >= 11700
|| player.isScoping())
//#else
//$$|| false)
|| player.isScoping()) {
//#else
//$$|| false) {
//#endif
//spotless:on
return;
}
boolean bl = hand == InteractionHand.MAIN_HAND;
HumanoidArm arm = bl ? player.getMainArm() : player.getMainArm().getOpposite();
matrices.pushPose();
if (item.isEmpty() && !bl && !player.isInvisible()) {
this.renderPlayerArm(matrices, vertexConsumers, light, equipProgress, swingProgress, arm);
renderPlayerArm(matrices, vertexConsumers, light, equipProgress, swingProgress, arm);
}
matrices.popPose();
}
Original file line number Diff line number Diff line change
@@ -25,8 +25,9 @@ public abstract class LivingEntityRendererMixin {
@Inject(method = "render", at = @At("HEAD"), cancellable = true)
public void renderHead(LivingEntity livingEntity, float f, float g, PoseStack matrixStack,
MultiBufferSource vertexConsumerProvider, int i, CallbackInfo info) {
if (livingEntity instanceof Shulker)
if (livingEntity instanceof Shulker) {
return;// No need to mess with
}
// only run when the player is rendering, and it's not a "Humanoid" model(it
// otherwise gets handeled in HumanoidModelMixin)
if (FirstPersonModelCore.instance.isRenderingPlayer() && !(getModel() instanceof HumanoidModel)) {
Original file line number Diff line number Diff line change
@@ -22,8 +22,9 @@ public class ModelPartMixin implements ModelPartBase {

@Override
public void setHidden() {
if (!moved)
if (!moved) {
zCopy = z;
}
z = 5000;
moved = true;
visible = false;
@@ -50,7 +51,7 @@ public void setPivot(float x, float y, float z, CallbackInfo info) {
public void copyTransform(ModelPart modelPart, CallbackInfo info) {
if (moved) {
zCopy = z;
this.z = 5000;
z = 5000;
}
}

Original file line number Diff line number Diff line change
@@ -56,13 +56,13 @@ protected PlayerRenderMixin(Context ctx, PlayerModel<AbstractClientPlayer> model
@Inject(method = "render", at = @At(value = "RETURN"))
public void render(AbstractClientPlayer player, float f, float g, PoseStack matrixStack,
MultiBufferSource vertexConsumerProvider, int i, CallbackInfo info) {
((ModelPartBase) (Object) this.getModel().head).showAgain();
((ModelPartBase) (Object) this.getModel().hat).showAgain();
((ModelPartBase) (Object) this.getModel().leftArm).showAgain();
((ModelPartBase) (Object) this.getModel().rightArm).showAgain();
((ModelPartBase) (Object) this.getModel().leftSleeve).showAgain();
((ModelPartBase) (Object) this.getModel().rightSleeve).showAgain();
((ModelPartBase) (Object) this.getModel().body).showAgain();
((ModelPartBase) (Object) getModel().head).showAgain();
((ModelPartBase) (Object) getModel().hat).showAgain();
((ModelPartBase) (Object) getModel().leftArm).showAgain();
((ModelPartBase) (Object) getModel().rightArm).showAgain();
((ModelPartBase) (Object) getModel().leftSleeve).showAgain();
((ModelPartBase) (Object) getModel().rightSleeve).showAgain();
((ModelPartBase) (Object) getModel().body).showAgain();
((ModelPartBase) (Object) ((PlayerModelAccess) model).getCloak()).showAgain();
FirstPersonModelCore.instance.setRenderingPlayer(false);
}
Original file line number Diff line number Diff line change
@@ -38,12 +38,13 @@ public class WorldRendererMixin {
@Inject(method = "renderLevel", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/LevelRenderer;checkPoseStack(Lcom/mojang/blaze3d/vertex/PoseStack;)V", ordinal = 0))
public void render(PoseStack matrices, float tickDelta, long limitTime, boolean renderBlockOutline, Camera camera,
GameRenderer gameRenderer, LightTexture lightmapTextureManager, Matrix4f matrix4f, CallbackInfo info) {
if (camera.isDetached() || !FirstPersonModelCore.instance.getLogicHandler().shouldApplyThirdPerson(false))
if (camera.isDetached() || !FirstPersonModelCore.instance.getLogicHandler().shouldApplyThirdPerson(false)) {
return;
}
Vec3 vec3d = camera.getPosition();
MultiBufferSource.BufferSource immediate = this.renderBuffers.bufferSource();
MultiBufferSource.BufferSource immediate = renderBuffers.bufferSource();
FirstPersonModelCore.instance.setRenderingPlayer(true);
this.renderEntity(camera.getEntity(), vec3d.x(), vec3d.y(), vec3d.z(), tickDelta, matrices, immediate);
renderEntity(camera.getEntity(), vec3d.x(), vec3d.y(), vec3d.z(), tickDelta, matrices, immediate);
FirstPersonModelCore.instance.setRenderingPlayer(false);
}

0 comments on commit 6d4b876

Please sign in to comment.