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

Add support for recipe conditions to have icons #918

Open
wants to merge 1 commit into
base: 1.21.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -52,10 +52,10 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.ComposterBlock;

public class MachineCategory extends ViewerCategory<RecipeHolder<MachineRecipe>> {
Expand Down Expand Up @@ -258,15 +258,36 @@ public void buildWidgets(RecipeHolder<MachineRecipe> recipeHolder, WidgetList wi
// Conditions
boolean conditionsRequired = recipe.conditions.size() > 0;
if (steelHatchRequired || upgradeEuRequired > 0 || conditionsRequired) {
ItemLike displayedItem;
List<ItemStack> displayedItems = new ArrayList<>();
if (steelHatchRequired) {
displayedItem = BuiltInRegistries.ITEM.get(MI.id("steel_item_input_hatch"));
} else if (conditionsRequired) {
displayedItem = MIItem.WRENCH;
} else {
displayedItem = MIItem.BASIC_UPGRADE;
displayedItems.add(BuiltInRegistries.ITEM.get(MI.id("steel_item_input_hatch")).getDefaultInstance());
}
if (upgradeEuRequired > 0) {
displayedItems.add(MIItem.BASIC_UPGRADE.stack());
}
for (var condition : recipe.conditions) {
ItemStack displayedItem = condition.icon();
if (!displayedItem.isEmpty()) {
displayedItems.add(displayedItem);
}
}
if (displayedItems.isEmpty()) {
displayedItems.add(MIItem.WRENCH.stack());
}
widgets.item(width / 2f - 3, 3.75, 10.8, 10.8, displayedItem);

double x = width / 2f - 3;
double y = 3.75;
double wh = 10.8;
widgets.drawable(graphics -> {
int itemIndex = (int) ((System.currentTimeMillis() / 1500L) % displayedItems.size());
ItemStack displayedItem = displayedItems.get(itemIndex).copyWithCount(1);

graphics.pose().pushPose();
graphics.pose().translate(x, y, 0);
graphics.pose().scale((float) wh / 16, (float) wh / 16, 1);
graphics.renderFakeItem(displayedItem, 0, 0);
graphics.pose().popPose();
});
}
// Tooltips
List<Component> tooltips = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@
import dev.latvian.mods.kubejs.event.KubeEvent;
import java.util.function.BiPredicate;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;

public class CustomConditionEventJS implements KubeEvent {
public void register(String id, BiPredicate<MachineProcessCondition.Context, MachineRecipe> predicate, ItemStack icon, Component... description) {
CustomProcessCondition.register(id, predicate, icon, description);
}

public void register(String id, BiPredicate<MachineProcessCondition.Context, MachineRecipe> predicate, Component... description) {
CustomProcessCondition.register(id, predicate, description);
register(id, predicate, ItemStack.EMPTY, description);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;

public record AdjacentBlockProcessCondition(Block block, RelativePosition relativePosition) implements MachineProcessCondition {
Expand Down Expand Up @@ -76,6 +77,11 @@ public void appendDescription(List<Component> list) {
list.add(text.text(block.getName()));
}

@Override
public ItemStack icon() {
return block.asItem().getDefaultInstance();
}

@Override
public MapCodec<? extends MachineProcessCondition> codec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.biome.Biome;

public record BiomeProcessCondition(ResourceKey<Biome> biome) implements MachineProcessCondition {
Expand All @@ -56,6 +58,11 @@ public void appendDescription(List<Component> list) {
list.add(MIText.RequiresBiome.text(biomeComponent));
}

@Override
public ItemStack icon() {
return Items.OAK_SAPLING.getDefaultInstance();
}

@Override
public MapCodec<? extends MachineProcessCondition> codec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.item.ItemStack;

public class CustomProcessCondition implements MachineProcessCondition {
static final Map<String, Definition> definitions = new HashMap<>();
Expand All @@ -49,8 +50,10 @@ public static void onReload() {
KubeJSProxy.instance.fireCustomConditionEvent();
}

public static void register(String id, BiPredicate<MachineProcessCondition.Context, MachineRecipe> predicate, Component... description) {
public static void register(String id, BiPredicate<MachineProcessCondition.Context, MachineRecipe> predicate, ItemStack icon,
Component... description) {
Objects.requireNonNull(predicate);
Objects.requireNonNull(icon);

if (definitions.containsKey(id)) {
throw new IllegalArgumentException("Duplicate custom process condition definition: " + id);
Expand All @@ -60,28 +63,32 @@ public static void register(String id, BiPredicate<MachineProcessCondition.Conte
throw new IllegalArgumentException("Custom process condition must have a description");
}

definitions.put(id, new Definition(predicate, List.of(description)));
definitions.put(id, new Definition(predicate, icon, List.of(description)));
}

private record Definition(
BiPredicate<MachineProcessCondition.Context, MachineRecipe> predicate,
List<Component> description) {
ItemStack icon, List<Component> description) {
}

private static MapCodec<CustomProcessCondition> makeCodec(boolean syncToClient) {
return RecordCodecBuilder.mapCodec(
g -> g
.group(
Codec.STRING.fieldOf("custom_id").forGetter(c -> c.id),
ItemStack.OPTIONAL_CODEC.fieldOf("icon").forGetter(c -> c.icon),
ComponentSerialization.CODEC.listOf().optionalFieldOf("description")
.forGetter(c -> syncToClient ? Optional.of(c.description) : Optional.empty()))
.apply(g, (id, desc) -> desc.map(d -> new CustomProcessCondition(id, d)).orElseGet(() -> new CustomProcessCondition(id))));
.apply(g, (id, icon, desc) -> desc.map(d -> new CustomProcessCondition(id, icon, d))
.orElseGet(() -> new CustomProcessCondition(id))));
}

static final MapCodec<CustomProcessCondition> CODEC = Codec.STRING.fieldOf("custom_id").xmap(CustomProcessCondition::new, c -> c.id);
static final StreamCodec<RegistryFriendlyByteBuf, CustomProcessCondition> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.STRING_UTF8,
c -> c.id,
ItemStack.OPTIONAL_STREAM_CODEC,
c -> c.icon,
ComponentSerialization.TRUSTED_STREAM_CODEC.apply(ByteBufCodecs.list()),
c -> c.description,
CustomProcessCondition::new);
Expand All @@ -92,15 +99,18 @@ public CustomProcessCondition(String id) {
throw new IllegalArgumentException("Unknown custom process condition definition: " + id);
}
this.id = id;
this.icon = definition.icon;
this.description = definition.description;
}

public CustomProcessCondition(String id, List<Component> description) {
public CustomProcessCondition(String id, ItemStack icon, List<Component> description) {
this.id = id;
this.icon = icon;
this.description = description;
}

private final String id;
private final ItemStack icon;
private final List<Component> description;

@Override
Expand All @@ -113,6 +123,11 @@ public void appendDescription(List<Component> list) {
list.addAll(description);
}

@Override
public ItemStack icon() {
return icon;
}

@Override
public MapCodec<? extends MachineProcessCondition> codec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package aztech.modern_industrialization.machines.recipe.condition;

import aztech.modern_industrialization.MIItem;
import aztech.modern_industrialization.MIText;
import aztech.modern_industrialization.machines.recipe.MachineRecipe;
import com.mojang.serialization.MapCodec;
Expand All @@ -32,6 +33,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public record DimensionProcessCondition(ResourceKey<Level> dimension) implements MachineProcessCondition {
Expand All @@ -56,6 +58,11 @@ public void appendDescription(List<Component> list) {
list.add(MIText.RequiresDimension.text(dimComponent));
}

@Override
public ItemStack icon() {
return MIItem.SINGULARITY.asItem().getDefaultInstance();
}

@Override
public MapCodec<? extends MachineProcessCondition> codec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;

public interface MachineProcessCondition {
Codec<MachineProcessCondition> CODEC = ResourceLocation.CODEC
Expand All @@ -60,6 +61,10 @@ public interface MachineProcessCondition {

void appendDescription(List<Component> list);

default ItemStack icon() {
return ItemStack.EMPTY;
}

MapCodec<? extends MachineProcessCondition> codec();

StreamCodec<? super RegistryFriendlyByteBuf, ? extends MachineProcessCondition> streamCodec();
Expand Down
Loading