Use a fast map for storing gear offsets and unravel lambdas

This commit is contained in:
Sollace 2022-12-21 23:32:08 +01:00
parent 24ed233210
commit 7fe4aa0d36

View file

@ -1,5 +1,7 @@
package com.minelittlepony.client.render.entity.feature; package com.minelittlepony.client.render.entity.feature;
import it.unimi.dsi.fastutil.objects.Object2FloatLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2FloatMap;
import net.minecraft.client.render.*; import net.minecraft.client.render.*;
import net.minecraft.client.render.entity.model.EntityModel; import net.minecraft.client.render.entity.model.EntityModel;
import net.minecraft.client.util.math.MatrixStack; import net.minecraft.client.util.math.MatrixStack;
@ -34,13 +36,13 @@ public class GearFeature<T extends LivingEntity, M extends EntityModel<T> & IPon
MOD_GEARS.stream().map(e -> new Entry(e.get(), Wearable.NONE)) MOD_GEARS.stream().map(e -> new Entry(e.get(), Wearable.NONE))
).collect(Collectors.toList()); ).collect(Collectors.toList());
private final LoadingCache<UUID, List<Entry>> randomisedGearCache = CacheBuilder.newBuilder() private final LoadingCache<Long, List<Entry>> randomisedGearCache = CacheBuilder.newBuilder()
.expireAfterAccess(3, TimeUnit.MINUTES) .expireAfterAccess(3, TimeUnit.MINUTES)
.build(CacheLoader.from(uuid -> { .build(CacheLoader.from(id -> {
List<Entry> randomizedOrder = new ArrayList<>(); List<Entry> randomizedOrder = new ArrayList<>();
List<Entry> pool = new ArrayList<>(gears); List<Entry> pool = new ArrayList<>(gears);
Random rng = Random.create(uuid.getLeastSignificantBits()); Random rng = Random.create(id);
while (!pool.isEmpty()) { while (!pool.isEmpty()) {
randomizedOrder.add(pool.remove(rng.nextInt(pool.size() + 1) % pool.size())); randomizedOrder.add(pool.remove(rng.nextInt(pool.size() + 1) % pool.size()));
@ -59,30 +61,26 @@ public class GearFeature<T extends LivingEntity, M extends EntityModel<T> & IPon
} }
final M model = getModelWrapper().body(); final M model = getModelWrapper().body();
final Object2FloatMap<BodyPart> renderStackingOffsets = new Object2FloatLinkedOpenHashMap<>();
final Map<BodyPart, Float> renderStackingOffsets = new HashMap<>(); for (var entry : randomisedGearCache.getUnchecked(entity.getUuid().getLeastSignificantBits())) {
randomisedGearCache.getUnchecked(entity.getUuid()).forEach(entry -> {
if (getContext().shouldRender(model, entity, entry.wearable, entry.gear)) { if (getContext().shouldRender(model, entity, entry.wearable, entry.gear)) {
stack.push(); stack.push();
BodyPart part = entry.gear.getGearLocation(); BodyPart part = entry.gear.getGearLocation();
entry.gear.transform(model, stack); entry.gear.transform(model, stack);
if (entry.gear instanceof IStackable) { if (entry.gear instanceof IStackable s) {
renderStackingOffsets.compute(part, (k, v) -> { float v = renderStackingOffsets.getFloat(part);
float offset = ((IStackable)entry.gear).getStackingHeight(); if (v != 0) {
if (v != null) {
stack.translate(0, -v, 0); stack.translate(0, -v, 0);
offset += v;
} }
return offset; renderStackingOffsets.put(part, v + s.getStackingHeight());
});
} }
renderGear(model, entity, entry.gear, stack, renderContext, lightUv, limbDistance, limbAngle, tickDelta); renderGear(model, entity, entry.gear, stack, renderContext, lightUv, limbDistance, limbAngle, tickDelta);
stack.pop(); stack.pop();
} }
}); }
} }
private void renderGear(M model, T entity, IGear gear, MatrixStack stack, VertexConsumerProvider renderContext, int lightUv, float limbDistance, float limbAngle, float tickDelta) { private void renderGear(M model, T entity, IGear gear, MatrixStack stack, VertexConsumerProvider renderContext, int lightUv, float limbDistance, float limbAngle, float tickDelta) {