Skip to content

Commit

Permalink
Remove capturing lambdas when updating swim and step height attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
pupnewfster committed Apr 4, 2024
1 parent b1206bb commit 4e2097b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
26 changes: 26 additions & 0 deletions src/api/java/mekanism/api/functions/ToFloatFunction.java
@@ -0,0 +1,26 @@
package mekanism.api.functions;

import java.util.function.Function;

/**
* Represents a function that produces a float-valued result. This is the {@code float}-producing primitive specialization for {@link Function}.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #applyAsFloat(Object)}.
*
* @param <T> the type of the input to the function
*
* @see Function
* @since 10.5.15
*/
@FunctionalInterface
public interface ToFloatFunction<T> {

/**
* Applies this function to the given argument.
*
* @param value the function argument
* @return the function result
*/
float applyAsFloat(T value);
}
8 changes: 4 additions & 4 deletions src/main/java/mekanism/client/render/lib/QuadUtils.java
Expand Up @@ -2,7 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import mekanism.api.functions.ToFloatFunction;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;

Expand Down Expand Up @@ -83,15 +83,15 @@ public static void contractUVs(Quad quad) {
}
}

private static float[] contract(Quad quad, Function<Vertex, Float> uvf, float ep) {
private static float[] contract(Quad quad, ToFloatFunction<Vertex> uvf, float ep) {
float center = 0;
float[] ret = new float[4];
for (int v = 0; v < 4; v++) {
center += uvf.apply(quad.getVertices()[v]);
center += uvf.applyAsFloat(quad.getVertices()[v]);
}
center /= 4;
for (int v = 0; v < 4; v++) {
float orig = uvf.apply(quad.getVertices()[v]);
float orig = uvf.applyAsFloat(quad.getVertices()[v]);
float shifted = orig * (1 - eps) + center * eps;
float delta = orig - shifted;
if (Math.abs(delta) < ep) { // not moving a fraction of a pixel
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/mekanism/common/base/PlayerState.java
Expand Up @@ -3,7 +3,7 @@
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import java.util.Set;
import java.util.UUID;
import mekanism.api.functions.FloatSupplier;
import mekanism.api.functions.ToFloatFunction;
import mekanism.client.sound.PlayerSound.SoundType;
import mekanism.client.sound.SoundHandler;
import mekanism.common.CommonPlayerTickHandler;
Expand Down Expand Up @@ -145,7 +145,7 @@ public Set<UUID> getActiveScubaMasks() {
// ----------------------

public void updateStepAssist(Player player) {
updateAttribute(player, NeoForgeMod.STEP_HEIGHT.value(), STEP_ASSIST_MODIFIER_UUID, "Step Assist", () -> CommonPlayerTickHandler.getStepBoost(player));
updateAttribute(player, NeoForgeMod.STEP_HEIGHT.value(), STEP_ASSIST_MODIFIER_UUID, "Step Assist", CommonPlayerTickHandler::getStepBoost);
}

// ----------------------
Expand All @@ -155,15 +155,15 @@ public void updateStepAssist(Player player) {
// ----------------------

public void updateSwimBoost(Player player) {
updateAttribute(player, NeoForgeMod.SWIM_SPEED.value(), SWIM_BOOST_MODIFIER_UUID, "Swim Boost", () -> CommonPlayerTickHandler.getSwimBoost(player));
updateAttribute(player, NeoForgeMod.SWIM_SPEED.value(), SWIM_BOOST_MODIFIER_UUID, "Swim Boost", CommonPlayerTickHandler::getSwimBoost);
}

//TODO - 1.20.4: Move these to the items?
private void updateAttribute(Player player, Attribute attribute, UUID uuid, String name, FloatSupplier additionalSupplier) {
//Note: The attributes that currently use this cannot be converted to just being attributes on the items, as they can be disabled based on the player state
private void updateAttribute(Player player, Attribute attribute, UUID uuid, String name, ToFloatFunction<Player> additionalSupplier) {
AttributeInstance attributeInstance = player.getAttribute(attribute);
if (attributeInstance != null) {
AttributeModifier existing = attributeInstance.getModifier(uuid);
float additional = additionalSupplier.getAsFloat();
float additional = additionalSupplier.applyAsFloat(player);
if (existing != null) {
if (existing.getAmount() == additional) {
//If we already have it set to the correct value just exit
Expand Down

0 comments on commit 4e2097b

Please sign in to comment.