Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only cache JEI citizens temporarily (#10489) #10548

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.minecolonies.core.compatibility.jei;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
Expand All @@ -14,6 +15,7 @@
import com.minecolonies.api.entity.ModEntities;
import com.minecolonies.api.equipment.ModEquipmentTypes;
import com.minecolonies.api.equipment.registry.EquipmentTypeEntry;
import com.minecolonies.api.util.Log;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.api.util.constant.TranslationConstants;
import com.minecolonies.core.colony.CitizenData;
Expand Down Expand Up @@ -52,7 +54,9 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.storage.loot.LootTable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -70,10 +74,13 @@ public abstract class JobBasedRecipeCategory<T> extends AbstractRecipeCategory<T
@NotNull private final IDrawableStatic background;
@NotNull protected final IDrawableStatic slot;
@NotNull protected final IDrawableStatic chanceSlot;
@NotNull private final EntityCitizen citizen;
@NotNull private final List<FormattedText> description;
@NotNull private final LoadingCache<T, List<InfoBlock>> infoBlocksCache;

private static final Cache<IJob<?>, EntityCitizen> citizenCache = CacheBuilder.newBuilder()
.expireAfterAccess(Duration.ofMinutes(2))
.build();

protected static final int WIDTH = 167;
protected static final int HEIGHT = 120;
protected static final int CITIZEN_X = 2;
Expand All @@ -100,8 +107,6 @@ protected JobBasedRecipeCategory(@NotNull final IJob<?> job,
this.slot = guiHelper.getSlotDrawable();
this.chanceSlot = guiHelper.createDrawable(TEXTURE, 0, 121, 18, 18);

this.citizen = createCitizenWithJob(this.job);

this.description = translateDescription(
TranslationConstants.PARTIAL_JEI_INFO +
this.job.getJobRegistryEntry().getKey().getPath());
Expand Down Expand Up @@ -199,12 +204,16 @@ public void draw(@NotNull final T recipe,
final int citizen_by = CITIZEN_Y + CITIZEN_H;
final int offsetY = 4;

final float headYaw = (float) Math.atan((citizen_cx - mouseX) / 40.0F) * 40.0F;
final float yaw = (float) Math.atan((citizen_cx - mouseX) / 40.0F) * 20.0F;
final float pitch = (float) Math.atan((citizen_cy - offsetY - mouseY) / 40.0F) * 20.0F;
Lighting.setupForFlatItems();
UiRenderMacros.drawEntity(stack.pose(), citizen_cx, citizen_by - offsetY, scale, headYaw, yaw, pitch, this.citizen);
Lighting.setupFor3DItems();
final EntityCitizen citizen = createCitizenWithJob(this.job);
if (citizen != null)
{
final float headYaw = (float) Math.atan((citizen_cx - mouseX) / 40.0F) * 40.0F;
final float yaw = (float) Math.atan((citizen_cx - mouseX) / 40.0F) * 20.0F;
final float pitch = (float) Math.atan((citizen_cy - offsetY - mouseY) / 40.0F) * 20.0F;
Lighting.setupForFlatItems();
UiRenderMacros.drawEntity(stack.pose(), citizen_cx, citizen_by - offsetY, scale, headYaw, yaw, pitch, citizen);
Lighting.setupFor3DItems();
}
}

@Override
Expand Down Expand Up @@ -283,16 +292,27 @@ public InfoBlock(final String text, final String tip, final Rect2i bounds)
public final Rect2i bounds;
}

@NotNull
@Nullable
private static EntityCitizen createCitizenWithJob(@NotNull final IJob<?> job)
{
final FakeLevel level = new SingleBlockFakeLevel(Minecraft.getInstance().level);
final EntityCitizen citizen = new EntityCitizen(ModEntities.CITIZEN, level);
citizen.setFemale(citizen.getRandom().nextBoolean());
citizen.setTextureId(citizen.getRandom().nextInt(255));
citizen.getEntityData().set(EntityCitizen.DATA_TEXTURE_SUFFIX, CitizenData.SUFFIXES.get(citizen.getRandom().nextInt(CitizenData.SUFFIXES.size())));
citizen.setModelId(job.getModel());
return citizen;
try
{
return citizenCache.get(job, () ->
{
final FakeLevel level = new SingleBlockFakeLevel(Minecraft.getInstance().level);
final EntityCitizen citizen = new EntityCitizen(ModEntities.CITIZEN, level);
citizen.setFemale(citizen.getRandom().nextBoolean());
citizen.setTextureId(citizen.getRandom().nextInt(255));
citizen.getEntityData().set(EntityCitizen.DATA_TEXTURE_SUFFIX, CitizenData.SUFFIXES.get(citizen.getRandom().nextInt(CitizenData.SUFFIXES.size())));
citizen.setModelId(job.getModel());
return citizen;
});
}
catch (final Throwable e)
{
Log.getLogger().error("Error creating citizen for {}", job.getJobRegistryEntry().getTranslationKey(), e);
return null;
}
}

@NotNull
Expand Down
Loading