Skip to content

Commit

Permalink
Further cleanup of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Jan 11, 2024
1 parent d94fc9d commit 37f37bf
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 38 deletions.
17 changes: 1 addition & 16 deletions src/main/java/dev/tr7zw/firstperson/FirstPersonModelCore.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package dev.tr7zw.firstperson;

import dev.tr7zw.firstperson.api.ActivationHandler;
import dev.tr7zw.firstperson.api.FirstPersonAPI;
import dev.tr7zw.firstperson.modsupport.PlayerAnimatorSupport;
import dev.tr7zw.firstperson.versionless.FirstPersonBase;
import lombok.Getter;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;

public abstract class FirstPersonModelCore extends FirstPersonBase {

Expand Down Expand Up @@ -48,18 +44,7 @@ private void lateInit() {
}

logicHandler.registerDefaultHandlers();
logicHandler.clearAutoVanillaHandsList();
Item invalid = BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft", "air"));
for (String itemId : config.autoVanillaHands) {
try {
Item item = BuiltInRegistries.ITEM
.get(new ResourceLocation(itemId.split(":")[0], itemId.split(":")[1]));
if (invalid != item)
logicHandler.addAutoVanillaHandsItem(item);
} catch (Exception ex) {
LOGGER.info("Unknown item to add to the auto vanilla hold list: " + itemId);
}
}
logicHandler.reloadAutoVanillaHandsSettings();
}

public void onTick() {
Expand Down
44 changes: 41 additions & 3 deletions src/main/java/dev/tr7zw/firstperson/LogicHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import net.minecraft.client.CameraType;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.vehicle.Boat;
import net.minecraft.world.entity.vehicle.Minecart;
import net.minecraft.world.item.Item;
Expand All @@ -38,7 +41,7 @@ public boolean preventFirstperson() {
return true;
if (client.player.isFallFlying())
return true;
if (client.player.getSwimAmount(1f) != 0 && !client.player.isVisuallySwimming())
if (client.player.getSwimAmount(1f) != 0 && !isCrawlingOrSwimming(client.player))
return true;
if (client.player.isScoping())
return true;
Expand Down Expand Up @@ -78,7 +81,7 @@ public void updatePositionOffset(Entity player, Vec3 defValue) {
}
if (!abstractClientPlayerEntity_1.isLocalPlayer() || client.getCameraEntity() == abstractClientPlayerEntity_1) {
float bodyOffset;
if (client.player.isVisuallySwimming()) {
if (isCrawlingOrSwimming(client.player)) {
abstractClientPlayerEntity_1.yBodyRot = abstractClientPlayerEntity_1.yHeadRot;
if (abstractClientPlayerEntity_1.xRotO > 0) {
bodyOffset = Constants.swimUpBodyOffset;
Expand Down Expand Up @@ -106,7 +109,7 @@ public void updatePositionOffset(Entity player, Vec3 defValue) {
}
x += bodyOffset * Math.sin(Math.toRadians(realYaw));
z -= bodyOffset * Math.cos(Math.toRadians(realYaw));
if (client.player.isVisuallySwimming()) {
if (isCrawlingOrSwimming(client.player)) {
if (abstractClientPlayerEntity_1.xRotO > 0 && abstractClientPlayerEntity_1.isUnderWater()) {
y += 0.6f * Math.sin(Math.toRadians(abstractClientPlayerEntity_1.xRotO));
} else {
Expand All @@ -118,6 +121,26 @@ public void updatePositionOffset(Entity player, Vec3 defValue) {
offset = new Vec3(x, y, z);
}

/**
* Util method to quicker find where swimming is referenced
*
* @param player
* @return
*/
public boolean isSwimming(Player player) {
return player.isSwimming();
}

/**
* Util method to quicker find where the crawling/swimming animation is referenced
*
* @param player
* @return
*/
public boolean isCrawlingOrSwimming(Player player) {
return player.isVisuallySwimming();
}

public boolean showVanillaHands() {
return FirstPersonModelMod.config.vanillaHands
|| autoVanillaHandItems.contains(client.player.getMainHandItem().getItem())
Expand All @@ -131,5 +154,20 @@ public void addAutoVanillaHandsItem(Item item) {
public void clearAutoVanillaHandsList() {
this.autoVanillaHandItems.clear();
}

public void reloadAutoVanillaHandsSettings() {
clearAutoVanillaHandsList();
Item invalid = BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft", "air"));
for (String itemId : FirstPersonModelCore.config.autoVanillaHands) {
try {
Item item = BuiltInRegistries.ITEM
.get(new ResourceLocation(itemId.split(":")[0], itemId.split(":")[1]));
if (invalid != item)
addAutoVanillaHandsItem(item);
} catch (Exception ex) {
FirstPersonModelCore.LOGGER.info("Unknown item to add to the auto vanilla hold list: " + itemId);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void renderArmor(PoseStack matrices, MultiBufferSource vertexConsumers,
info.cancel();
}
if (equipmentSlot == EquipmentSlot.CHEST && FirstPersonModelCore.isRenderingPlayer
&& (livingEntity instanceof LocalPlayer player && player.isSwimming())) {
&& (livingEntity instanceof LocalPlayer player && FirstPersonModelCore.instance.getLogicHandler().isSwimming(player))) {
info.cancel();
}
if (equipmentSlot == EquipmentSlot.CHEST && FirstPersonModelCore.isRenderingPlayer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ElytraLayerMixin<T extends LivingEntity> {
public void render(PoseStack poseStack, MultiBufferSource multiBufferSource, int i, T livingEntity, float f,
float g, float h, float j, float k, float l, CallbackInfo ci) {
if (FirstPersonModelCore.isRenderingPlayer && livingEntity instanceof LocalPlayer player
&& player.isSwimming()) {
&& FirstPersonModelCore.instance.getLogicHandler().isSwimming(player)) {
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ private boolean doCorrect() {
&& Minecraft.getInstance().options.getCameraType() == CameraType.FIRST_PERSON;
}


@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import net.minecraft.world.item.Items;

/**
* Hides the normal first person hands and handels map rendering
* Hides the normal first person hands
*
*/
@Mixin(ItemInHandRenderer.class)
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/dev/tr7zw/firstperson/mixins/PlayerRenderMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,25 @@ public PlayerRenderMixin(Context ctx, PlayerModel<AbstractClientPlayer> model, f

@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/entity/player/PlayerRenderer;setModelProperties(Lnet/minecraft/client/player/AbstractClientPlayer;)V"))
private void setModelPoseRedirect(PlayerRenderer playerEntityRenderer,
AbstractClientPlayer abstractClientPlayerEntity, AbstractClientPlayer abstractClientPlayerEntity_1, float f,
AbstractClientPlayer player, AbstractClientPlayer abstractClientPlayerEntity_1, float f,
float g, PoseStack matrixStack, MultiBufferSource vertexConsumerProvider, int i) {
setModelProperties(abstractClientPlayerEntity);
setModelProperties(player);
if (FirstPersonModelCore.isRenderingPlayer) {
PlayerModel<AbstractClientPlayer> playerEntityModel_1 = this.getModel();
playerEntityModel_1.head.visible = false;
playerEntityModel_1.hat.visible = false;
PlayerModel<AbstractClientPlayer> model = this.getModel();
model.head.visible = false;
model.hat.visible = false;
// Moved to HumanoidModelMixin to be called during setupAnim
// ((ModelPartBase)(Object)playerEntityModel_1.head).setHidden();
if (FirstPersonModelCore.instance.getLogicHandler().showVanillaHands()) {
playerEntityModel_1.leftArm.visible = false;
playerEntityModel_1.leftSleeve.visible = false;
playerEntityModel_1.rightArm.visible = false;
playerEntityModel_1.rightSleeve.visible = false;
model.leftArm.visible = false;
model.leftSleeve.visible = false;
model.rightArm.visible = false;
model.rightSleeve.visible = false;
} else {

}
if (abstractClientPlayerEntity.isSwimming()) {
playerEntityModel_1.body.visible = false;
if (FirstPersonModelCore.instance.getLogicHandler().isSwimming(player)) {
model.body.visible = false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ public void render(PoseStack matrices, float tickDelta, long limitTime, boolean
if (camera.isDetached() || !FirstPersonModelCore.instance.getLogicHandler().shouldApplyThirdPerson(false))
return;
Vec3 vec3d = camera.getPosition();
double d = vec3d.x();
double e = vec3d.y();
double f = vec3d.z();
MultiBufferSource.BufferSource immediate = this.renderBuffers.bufferSource();
FirstPersonModelCore.isRenderingPlayer = true;
this.renderEntity(camera.getEntity(), d, e, f, tickDelta, matrices, (MultiBufferSource) immediate);
this.renderEntity(camera.getEntity(), vec3d.x(), vec3d.y(), vec3d.z(), tickDelta, matrices, (MultiBufferSource) immediate);
FirstPersonModelCore.isRenderingPlayer = false;
}

Expand Down

0 comments on commit 37f37bf

Please sign in to comment.