Skip to content

Commit

Permalink
reduce potential mod conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeF53 committed May 28, 2024
1 parent b96fcde commit 743cfc7
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 93 deletions.
1 change: 0 additions & 1 deletion src/main/java/net/F53/HorseBuff/ClientInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.lwjgl.glfw.GLFW;

public class ClientInit implements ClientModInitializer {

public static KeyBinding horsePlayerInventory = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"text.HorseBuff.keybinding.horsePlayerInventory",
InputUtil.Type.KEYSYM,
Expand Down
66 changes: 37 additions & 29 deletions src/main/java/net/F53/HorseBuff/mixin/Client/HorseRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,76 @@
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.passive.AbstractHorseEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArgs;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;

import java.awt.*;

import static net.F53.HorseBuff.utils.RenderUtils.*;
import static net.F53.HorseBuff.utils.RenderUtils.getOpacity;
import static net.F53.HorseBuff.utils.RenderUtils.isJeb;

@Mixin(value = LivingEntityRenderer.class, priority = 960)
public abstract class HorseRenderer<T extends LivingEntity, M extends EntityModel<T>> {
@Unique
private boolean hb$isHorse;

private boolean isHorse;
@Unique
private float hb$opacity;

private float opacity;

private float r;
private float g;
private float b;
@Unique
private float hb$r;
@Unique
private float hb$g;
@Unique
private float hb$b;

@Inject(method = "render(Lnet/minecraft/entity/LivingEntity;FFLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;I)V",
at = @At("HEAD"))
at = @At("HEAD"))
void fetchOpacityAndJeb(T livingEntity, float yaw, float tickDelta, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int light, CallbackInfo ci) {
isHorse = false;
opacity = 1;
r = 1;
g = 1;
b = 1;
hb$isHorse = false;
hb$opacity = 1;
hb$r = 1;
hb$g = 1;
hb$b = 1;
if (livingEntity instanceof AbstractHorseEntity) {
isHorse = true;
hb$isHorse = true;
if (ModConfig.getInstance().pitchFade.enabled && livingEntity.hasPassenger(MinecraftClient.getInstance().player)) {
ClientPlayerEntity player = MinecraftClient.getInstance().player;
assert player != null;
opacity = getOpacity(player);
hb$opacity = getOpacity(player);
}
if (isJeb(livingEntity)) {
float hueOffset = (livingEntity.getUuid().hashCode()%5000)/5000f + (System.currentTimeMillis()%5000)/5000f;
float hueOffset = (livingEntity.getUuid().hashCode() % 5000) / 5000f + (System.currentTimeMillis() % 5000) / 5000f;
Color color = new Color(Color.HSBtoRGB(hueOffset, 0.8f, 1));
r = color.getRed()/255f;
g = color.getGreen()/255f;
b = color.getBlue()/255f;
hb$r = color.getRed() / 255f;
hb$g = color.getGreen() / 255f;
hb$b = color.getBlue() / 255f;
}
}
}

@WrapOperation(method = "render(Lnet/minecraft/entity/LivingEntity;FFLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;I)V",
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/entity/LivingEntityRenderer;getRenderLayer(Lnet/minecraft/entity/LivingEntity;ZZZ)Lnet/minecraft/client/render/RenderLayer;"))
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/entity/LivingEntityRenderer;getRenderLayer(Lnet/minecraft/entity/LivingEntity;ZZZ)Lnet/minecraft/client/render/RenderLayer;"))
RenderLayer makeRenderLayerTranslucent(LivingEntityRenderer<T, ? extends EntityModel<T>> instance, T entity, boolean showBody, boolean translucent, boolean showOutline, Operation<RenderLayer> original) {
if (showBody && entity instanceof AbstractHorseEntity && opacity < 1) {
if (showBody && entity instanceof AbstractHorseEntity && hb$opacity < 1) {
return RenderLayer.getItemEntityTranslucentCull(instance.getTexture(entity));
} else {
return original.call(instance, entity, showBody, translucent, showOutline);
}
}

@ModifyArgs(method = "render(Lnet/minecraft/entity/LivingEntity;FFLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;I)V",
at = @At(value = "INVOKE", target = "net/minecraft/client/render/entity/model/EntityModel.render (Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumer;IIFFFF)V"))
void setOpacityAndChromaForRender(Args args){
if (isHorse) {
args.set(4, r);
args.set(5, g);
args.set(6, b);
args.set(7, Math.min(args.get(7), opacity));
at = @At(value = "INVOKE", target = "net/minecraft/client/render/entity/model/EntityModel.render (Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumer;IIFFFF)V"))
void setOpacityAndChromaForRender(Args args) {
if (hb$isHorse) {
args.set(4, hb$r);
args.set(5, hb$g);
args.set(6, hb$b);
args.set(7, Math.min(args.get(7), hb$opacity));
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/net/F53/HorseBuff/mixin/Client/Swim.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public class Swim {
private void fakeSwim(PlayerEntity controllingPlayer, Vec3d movementInput, CallbackInfo ci) {
if (!((Object)this instanceof AbstractHorseEntity)) {return;}
AbstractHorseEntity horseInstance = (AbstractHorseEntity) (Object) this;
if (!shouldSwim(horseInstance)) {return;}
if (!hb$shouldSwim(horseInstance)) {return;}

if (horseInstance.getFluidHeight(FluidTags.WATER) > horseInstance.getSwimHeight()) {
horseInstance.addVelocity(0, 0.08, 0);
}
}

@Unique
private boolean shouldSwim(AbstractHorseEntity horseInstance) {
private boolean hb$shouldSwim(AbstractHorseEntity horseInstance) {
if (horseInstance instanceof HorseEntity ||
horseInstance instanceof DonkeyEntity ||
horseInstance instanceof MuleEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

@Mixin(AbstractHorseEntity.class)
public abstract class AllowPortalUse extends LivingEntity {
@Shadow public abstract @Nullable LivingEntity getControllingPassenger();
@Shadow
public abstract @Nullable LivingEntity getControllingPassenger();

// constructor to make compiler happy
protected AllowPortalUse(EntityType<? extends LivingEntity> entityType, World world) {
Expand All @@ -22,7 +24,7 @@ protected AllowPortalUse(EntityType<? extends LivingEntity> entityType, World wo

@Override
public boolean canUsePortals() {
if (!this.portalPatchApplies())
if (!this.hb$portalPatchApplies())
return super.canUsePortals();

return true;
Expand All @@ -31,25 +33,26 @@ public boolean canUsePortals() {
@Override
public void setInNetherPortal(BlockPos pos) {
// make player inherit horse portal position when mounted
if (this.portalPatchApplies())
if (this.hb$portalPatchApplies())
this.getControllingPassenger().setInNetherPortal(pos);

super.setInNetherPortal(pos);
}

@Override
public void resetPortalCooldown() {
if (this.portalPatchApplies()) {
if (this.hb$portalPatchApplies()) {
// inherit portal cooldown of controlling passenger
this.setPortalCooldown(this.getControllingPassenger().getDefaultPortalCooldown());
return;
}
super.resetPortalCooldown();
}

private boolean portalPatchApplies() {
@Unique
private boolean hb$portalPatchApplies() {
return (ModConfig.getInstance().portalPatch
&& this.hasControllingPassenger()
&& this.getControllingPassenger() instanceof PlayerEntity);
&& this.hasControllingPassenger()
&& this.getControllingPassenger() instanceof PlayerEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class OnCollideEnd {
public Entity bringRider(Entity vehicle, ServerWorld destination, Operation<Entity> original) {
// guard clause to make vanilla handle natural teleportations
if (!(ModConfig.getInstance().portalPatch
&& vehicle instanceof AbstractHorseEntity
&& vehicle.hasControllingPassenger()))
&& vehicle instanceof AbstractHorseEntity
&& vehicle.hasControllingPassenger()))
return original.call(vehicle, destination);

// Teleport then safely rejoin player and vehicle once the game is ready
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/net/F53/HorseBuff/mixin/PortalHorse/TickNether.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package net.F53.HorseBuff.mixin.PortalHorse;

import com.llamalad7.mixinextras.sugar.Local;

import net.F53.HorseBuff.config.ModConfig;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.passive.AbstractHorseEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandOutput;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Nameable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.entity.EntityLike;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import static net.F53.HorseBuff.utils.TeleportHandler.tpAndRemount;

@Mixin(Entity.class)
public abstract class TickNether implements Nameable, EntityLike, CommandOutput {
@Shadow protected abstract void fall(double heightDifference, boolean onGround, BlockState state, BlockPos landedPosition);
public abstract class TickNether {
@Shadow
protected abstract void fall(double heightDifference, boolean onGround, BlockState state, BlockPos landedPosition);

@Redirect(method = "tickPortal", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;isNetherAllowed()Z"))
private boolean tickNether(MinecraftServer minecraftServer, @Local(ordinal = 1) ServerWorld destination) {
Expand All @@ -30,13 +29,13 @@ private boolean tickNether(MinecraftServer minecraftServer, @Local(ordinal = 1)
return false;

// ensure horse
if (!((Object) this instanceof AbstractHorseEntity vehicle))
if (!(hb$thiz() instanceof AbstractHorseEntity vehicle))
return true;

// ensure Patch is enabled and player is controlling
if (!(ModConfig.getInstance().portalPatch
&& vehicle.hasControllingPassenger()
&& vehicle.getControllingPassenger() instanceof PlayerEntity player))
&& vehicle.hasControllingPassenger()
&& vehicle.getControllingPassenger() instanceof PlayerEntity player))
return true;

// manually tick player portal time (it cant go up naturally due to being mounted)
Expand All @@ -51,4 +50,9 @@ private boolean tickNether(MinecraftServer minecraftServer, @Local(ordinal = 1)
// prevent vanilla handling teleportation
return false;
}

@Unique
private Entity hb$thiz() {
return ((Entity) (Object) this);
}
}
8 changes: 7 additions & 1 deletion src/main/java/net/F53/HorseBuff/mixin/Server/AllowRaft.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
import net.minecraft.entity.passive.AbstractHorseEntity;
import net.minecraft.entity.vehicle.BoatEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(BoatEntity.class)
public abstract class AllowRaft {
@ModifyReturnValue(method = "isSmallerThanBoat", at = @At("RETURN"))
private boolean allowRaft(boolean original, @Local(argsOnly = true)Entity entity){
if (entity instanceof AbstractHorseEntity && ((BoatEntity)(Object)this).getVariant() == BoatEntity.Type.BAMBOO)
if (entity instanceof AbstractHorseEntity && hb$thiz().getVariant() == BoatEntity.Type.BAMBOO)
return true;
return original;
}

@Unique
private BoatEntity hb$thiz() {
return ((BoatEntity)(Object)this);
}
}
70 changes: 35 additions & 35 deletions src/main/java/net/F53/HorseBuff/mixin/Server/MountedModifiers.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,51 @@
// - remove BreakSpeed debuff from not being grounded (when enabled)
@Mixin(value = PlayerEntity.class, priority = 960)
public abstract class MountedModifiers extends LivingEntity {
protected MountedModifiers(EntityType<? extends LivingEntity> entityType, World world) {
super(entityType, world);
}
protected MountedModifiers(EntityType<? extends LivingEntity> entityType, World world) {
super(entityType, world);
}

@Unique
EntityAttributeModifier mountedStepHeight = new EntityAttributeModifier("HorseBuff-MountedStepHeight", 0.1, EntityAttributeModifier.Operation.ADD_VALUE);
@Unique
EntityAttributeModifier mountedBreakSpeed = new EntityAttributeModifier("HorseBuff-MountedBreakSpeed", 5, EntityAttributeModifier.Operation.ADD_MULTIPLIED_BASE);
@Unique
EntityAttributeModifier mountedStepHeight = new EntityAttributeModifier("HorseBuff-MountedStepHeight", 0.1, EntityAttributeModifier.Operation.ADD_VALUE);
@Unique
EntityAttributeModifier mountedBreakSpeed = new EntityAttributeModifier("HorseBuff-MountedBreakSpeed", 5, EntityAttributeModifier.Operation.ADD_MULTIPLIED_BASE);

@Unique
public boolean startRiding(Entity entity, boolean force) {
boolean result = super.startRiding(entity, force);
if (!(super.getWorld() instanceof ServerWorld && entity instanceof AbstractHorseEntity horse))
return result;
@Override
public boolean startRiding(Entity entity, boolean force) {
boolean result = super.startRiding(entity, force);
if (!(super.getWorld() instanceof ServerWorld && entity instanceof AbstractHorseEntity horse))
return result;

if (ModConfig.getInstance().stepHeight) {
EntityAttributeInstance stepHeight = horse.getAttributeInstance(EntityAttributes.GENERIC_STEP_HEIGHT);
if (stepHeight != null) stepHeight.addTemporaryModifier(mountedStepHeight);
}
if (ModConfig.getInstance().stepHeight) {
EntityAttributeInstance stepHeight = horse.getAttributeInstance(EntityAttributes.GENERIC_STEP_HEIGHT);
if (stepHeight != null) stepHeight.addTemporaryModifier(mountedStepHeight);
}

if (ModConfig.getInstance().breakSpeed) {
EntityAttributeInstance breakSpeed = getAttributeInstance(EntityAttributes.PLAYER_BLOCK_BREAK_SPEED);
if (breakSpeed != null) breakSpeed.addTemporaryModifier(mountedBreakSpeed);
}
return result;
}
if (ModConfig.getInstance().breakSpeed) {
EntityAttributeInstance breakSpeed = getAttributeInstance(EntityAttributes.PLAYER_BLOCK_BREAK_SPEED);
if (breakSpeed != null) breakSpeed.addTemporaryModifier(mountedBreakSpeed);
}
return result;
}

@Unique
public boolean startRiding(Entity entity) {
return this.startRiding(entity, false);
}
@Override
public boolean startRiding(Entity entity) {
return this.startRiding(entity, false);
}

@Unique
public void stopRiding() {
if (!(super.getWorld() instanceof ServerWorld && getVehicle() instanceof AbstractHorseEntity horse)) {
@Override
public void stopRiding() {
if (!(super.getWorld() instanceof ServerWorld && getVehicle() instanceof AbstractHorseEntity horse)) {
super.stopRiding();
return;
}

EntityAttributeInstance stepHeight = horse.getAttributeInstance(EntityAttributes.GENERIC_STEP_HEIGHT);
if (stepHeight != null) stepHeight.removeModifier(mountedStepHeight);
EntityAttributeInstance stepHeight = horse.getAttributeInstance(EntityAttributes.GENERIC_STEP_HEIGHT);
if (stepHeight != null) stepHeight.removeModifier(mountedStepHeight);

EntityAttributeInstance breakSpeed = getAttributeInstance(EntityAttributes.PLAYER_BLOCK_BREAK_SPEED);
if (breakSpeed != null) breakSpeed.removeModifier(mountedBreakSpeed);
EntityAttributeInstance breakSpeed = getAttributeInstance(EntityAttributes.PLAYER_BLOCK_BREAK_SPEED);
if (breakSpeed != null) breakSpeed.removeModifier(mountedBreakSpeed);

super.stopRiding();
}
super.stopRiding();
}
}
9 changes: 8 additions & 1 deletion src/main/java/net/F53/HorseBuff/mixin/Server/NoBuck.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import net.F53.HorseBuff.config.ModConfig;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.passive.AbstractHorseEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(value = AbstractHorseEntity.class, priority = 960)
public class NoBuck {
@ModifyReturnValue(method = "isAngry", at = @At("RETURN"))
private boolean isAngry(boolean original) {
AbstractHorseEntity instance = ((AbstractHorseEntity)(Object)this);
AbstractHorseEntity instance = hb$thiz();
if (ModConfig.getInstance().noBuck
&& !instance.jumping
&& instance.isTame()
Expand All @@ -19,4 +21,9 @@ private boolean isAngry(boolean original) {
}
return original;
}

@Unique
private AbstractHorseEntity hb$thiz() {
return ((AbstractHorseEntity)(Object)this);
}
}
Loading

0 comments on commit 743cfc7

Please sign in to comment.