Skip to content

Commit

Permalink
Change ClibanoRecipe.java to a record
Browse files Browse the repository at this point in the history
  • Loading branch information
stal111 committed Oct 27, 2024
1 parent bc42a69 commit 3b6b3bf
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 151 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.stal111.forbidden_arcanus.common.block.entity.clibano;

import com.mojang.serialization.Codec;
import net.minecraft.util.ExtraCodecs;

import java.util.Arrays;
import java.util.List;

public record ClibanoCookingTimes(List<Integer> cookingTimes) {

public static final Codec<ClibanoCookingTimes> CODEC = ExtraCodecs.POSITIVE_INT.xmap(ClibanoCookingTimes::of, clibanoCookingTimes -> clibanoCookingTimes.cookingTimes().getFirst());

public static ClibanoCookingTimes of(int defaultTime) {
var list = Arrays.stream(ClibanoFireType.values())
.map(fireType -> (int) (defaultTime / fireType.getCookingSpeedMultiplier()))
.toList();

return new ClibanoCookingTimes(list);
}

public int get(ClibanoFireType fireType) {
return this.cookingTimes().get(fireType.ordinal());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.stal111.forbidden_arcanus.common.block.entity.clibano.logic.ClibanoSmeltLogic;
import com.stal111.forbidden_arcanus.common.block.entity.clibano.logic.DefaultSmeltLogic;
import com.stal111.forbidden_arcanus.common.block.entity.clibano.logic.DoubleSmeltLogic;
import com.stal111.forbidden_arcanus.common.block.entity.clibano.residue.ResidueChance;
import com.stal111.forbidden_arcanus.common.inventory.clibano.ClibanoMenu;
import com.stal111.forbidden_arcanus.common.item.crafting.ClibanoRecipe;
import com.stal111.forbidden_arcanus.common.item.crafting.ClibanoRecipeInput;
Expand Down Expand Up @@ -275,7 +274,7 @@ public boolean canSmelt(@Nullable RecipeHolder<ClibanoRecipe> recipe, ClibanoInp

ItemStack stack = recipe.value().getResultItem(this.level.registryAccess());

if (stack.isEmpty() || (this.soulTime == 0 ? this.nextFireType : this.fireType).ordinal() < recipe.value().getRequiredFireType().ordinal()) {
if (stack.isEmpty() || (this.soulTime == 0 ? this.nextFireType : this.fireType).ordinal() < recipe.value().requiredFireType().ordinal()) {
return false;
}

Expand Down Expand Up @@ -342,7 +341,7 @@ public void finishRecipe(RecipeHolder<ClibanoRecipe> recipe, ClibanoInputSlot in

@Override
public int getCookingTime(RecipeHolder<ClibanoRecipe> recipe) {
return recipe.value().getCookingTime(this.fireType);
return recipe.value().cookingTimes().get(this.fireType);
}

/**
Expand All @@ -356,15 +355,15 @@ private void addResidue(ClibanoRecipe recipe, RandomSource random) {
return;
}

ResidueChance chance = recipe.getResidueChance();
recipe.residueChance().ifPresent(chance -> {
if (random.nextDouble() < chance.chance()) {
this.residuesStorage.increaseType(chance.type(), 1);

if (chance != null && random.nextDouble() < chance.chance()) {
this.residuesStorage.increaseType(chance.type(), 1);

if (this.level instanceof ServerLevel serverLevel) {
PacketDistributor.sendToPlayersTrackingChunk(serverLevel, new ChunkPos(this.getBlockPos()), new SetClibanoResiduesPayload(this.residuesStorage));
if (this.level instanceof ServerLevel serverLevel) {
PacketDistributor.sendToPlayersTrackingChunk(serverLevel, new ChunkPos(this.getBlockPos()), new SetClibanoResiduesPayload(this.residuesStorage));
}
}
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void updateCookingProgress(ClibanoFireType fireType, RecipeHolder<Clib
if (recipeHolder != null) {
int oldDuration = this.cookingDuration[slot];

this.cookingDuration[slot] = recipeHolder.value().getCookingTime(fireType);
this.cookingDuration[slot] = recipeHolder.value().cookingTimes().get(fireType);
this.cookingProgress[slot] = (int) (((float) this.cookingProgress[slot] / oldDuration) * this.cookingDuration[slot]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.Holder;
import net.minecraft.network.FriendlyByteBuf;

import javax.annotation.Nonnull;

/**
* @author stal111
Expand All @@ -14,20 +11,7 @@
public record ResidueChance(Holder<ResidueType> type, double chance) {

public static final Codec<ResidueChance> CODEC = RecordCodecBuilder.create(instance -> instance.group(
ResidueType.CODEC.fieldOf("type").forGetter(info -> {
return info.type;
}),
Codec.DOUBLE.fieldOf("chance").forGetter(info -> {
return info.chance;
})
ResidueType.CODEC.fieldOf("type").forGetter(ResidueChance::type),
Codec.DOUBLE.fieldOf("chance").forGetter(ResidueChance::chance)
).apply(instance, ResidueChance::new));

public static ResidueChance fromNetwork(@Nonnull FriendlyByteBuf buffer) {
return new ResidueChance(buffer.readJsonWithCodec(ResidueType.CODEC), buffer.readDouble());
}

public void toNetwork(@Nonnull FriendlyByteBuf buffer) {
buffer.writeJsonWithCodec(ResidueType.CODEC, this.type);
buffer.writeDouble(this.chance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.google.common.cache.LoadingCache;
import com.stal111.forbidden_arcanus.ForbiddenArcanus;
import com.stal111.forbidden_arcanus.common.block.entity.clibano.ClibanoFireType;
import com.stal111.forbidden_arcanus.common.block.entity.clibano.residue.ResidueChance;
import com.stal111.forbidden_arcanus.common.item.crafting.ClibanoRecipe;
import com.stal111.forbidden_arcanus.common.item.enhancer.EnhancerDefinition;
import com.stal111.forbidden_arcanus.core.init.ModBlocks;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
Expand All @@ -23,7 +21,6 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -115,47 +112,41 @@ public void setRecipe(@NotNull IRecipeLayoutBuilder builder, @NotNull ClibanoRec
builder.addSlot(INPUT, 55, 24).addIngredients(recipe.getIngredients().get(1));
}

TagKey<Item> tagKey = recipe.getRequiredFireType().getTagKey();
TagKey<Item> tagKey = recipe.requiredFireType().getTagKey();

if (tagKey != null) {
builder.addSlot(RENDER_ONLY, 12, 60).addIngredients(Ingredient.of(tagKey));
}

RegistryAccess registryAccess = Minecraft.getInstance().level.registryAccess();
Holder<EnhancerDefinition> enhancer = recipe.getRequiredEnhancer();

if (enhancer != null) {
recipe.requiredEnhancer().ifPresent(enhancer -> {
builder.addSlot(CATALYST, 12, 24).addItemStack(enhancer.value().displayItem().value().getDefaultInstance());
}
});

RegistryAccess registryAccess = Minecraft.getInstance().level.registryAccess();

builder.addSlot(OUTPUT, 97, 35).addItemStack(recipe.getResultItem(registryAccess));
}

private IDrawableAnimated getArrow(ClibanoRecipe recipe) {
int cookTime = recipe.getCookingTime(recipe.getRequiredFireType());
if (cookTime <= 0) {
cookTime = ClibanoRecipe.DEFAULT_COOKING_TIME;
}
return this.cachedArrows.getUnchecked(cookTime);
private IDrawableAnimated getArrow(int cookingTime) {
return this.cachedArrows.getUnchecked(cookingTime);
}

@Override
public void draw(@NotNull ClibanoRecipe recipe, @NotNull IRecipeSlotsView recipeSlotsView, @NotNull GuiGraphics guiGraphics, double mouseX, double mouseY) {
this.animatedFlames.get(recipe.getRequiredFireType()).draw(guiGraphics, 48, 43);
this.animatedFlames.get(recipe.requiredFireType()).draw(guiGraphics, 48, 43);

if (!recipe.isDoubleRecipe()) {
guiGraphics.blit(TEXTURE, 54, 23, 224, 0, 18, 18);
}

IDrawableAnimated arrow = this.getArrow(recipe);
IDrawableAnimated arrow = this.getArrow(recipe.getDefaultCookingTime());
arrow.draw(guiGraphics, 74, 43);

this.drawExperience(recipe, guiGraphics, 12);
this.drawCookTime(recipe, guiGraphics, 79);
this.drawExperience(recipe.experience(), guiGraphics, 12);
this.drawCookTime(recipe.getDefaultCookingTime(), guiGraphics, 79);
}

protected void drawExperience(ClibanoRecipe recipe, GuiGraphics guiGraphics, int y) {
float experience = recipe.getExperience();
protected void drawExperience(float experience, GuiGraphics guiGraphics, int y) {
if (experience > 0) {
Component experienceString = Component.translatable("gui.jei.category.smelting.experience", experience);
Minecraft minecraft = Minecraft.getInstance();
Expand All @@ -166,10 +157,9 @@ protected void drawExperience(ClibanoRecipe recipe, GuiGraphics guiGraphics, int
}
}

protected void drawCookTime(ClibanoRecipe recipe, GuiGraphics guiGraphics, int y) {
int cookTime = recipe.getCookingTime(recipe.getRequiredFireType());
if (cookTime > 0) {
int cookTimeSeconds = cookTime / 20;
protected void drawCookTime(int cookingTime, GuiGraphics guiGraphics, int y) {
if (cookingTime > 0) {
int cookTimeSeconds = cookingTime / 20;
Component timeString = Component.translatable("gui.jei.category.smelting.time.seconds", cookTimeSeconds);
Minecraft minecraft = Minecraft.getInstance();
Font font = minecraft.font;
Expand All @@ -182,17 +172,13 @@ protected void drawCookTime(ClibanoRecipe recipe, GuiGraphics guiGraphics, int y
@Override
public void getTooltip(@NotNull ITooltipBuilder tooltip, @NotNull ClibanoRecipe recipe, @NotNull IRecipeSlotsView recipeSlotsView, double mouseX, double mouseY) {
if (mouseX >= 92 && mouseY >= 59 && mouseX <= 117 && mouseY <= 65) {
ResidueChance chance = recipe.getResidueChance();

if (chance == null) {
return;
}

tooltip.add(chance.type().value().name().copy()
.append(" ")
.append(Component.translatable("jei.forbidden_arcanus.clibanoCombustion.residue"))
.append(" (" + chance.chance() * 100 +"%)")
);
recipe.residueChance().ifPresent(chance -> {
tooltip.add(chance.type().value().name().copy()
.append(" ")
.append(Component.translatable("jei.forbidden_arcanus.clibanoCombustion.residue"))
.append(" (" + chance.chance() * 100 +"%)")
);
});
}
}
}
Loading

0 comments on commit 3b6b3bf

Please sign in to comment.