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

Make setting crafting components from KJS more ergonomic. #2639

Draft
wants to merge 1 commit into
base: 1.20.1
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions src/main/java/com/gregtechceu/gtceu/api/GTValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static net.minecraft.ChatFormatting.*;

Expand Down Expand Up @@ -142,6 +145,9 @@ public static int[] tiersBetween(int minInclusive, int maxInclusive) {
public static final String[] VN = new String[] { "ULV", "LV", "MV", "HV", "EV", "IV", "LuV", "ZPM", "UV", "UHV",
"UEV", "UIV", "UXV", "OpV", "MAX" };

public static final Map<String, Integer> RVN = IntStream.range(0, VN.length)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object2IntMap to avoid boxing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, what does RVN stand for?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably reverse voltage names, no?

.boxed().collect(Collectors.toMap(i -> VN[i], i -> (Integer) i));

/**
* The short names for the voltages, formatted for text
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gregtechceu.gtceu.integration.kjs.events;

import com.gregtechceu.gtceu.api.GTValues;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.UnificationEntry;
import com.gregtechceu.gtceu.data.recipe.CraftingComponent;

Expand All @@ -9,6 +10,8 @@
import net.minecraft.world.item.ItemStack;

import dev.latvian.mods.kubejs.event.StartupEventJS;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import dev.latvian.mods.rhino.Context;
import lombok.NoArgsConstructor;

import java.util.Map;
Expand All @@ -22,79 +25,80 @@ public void modify(CraftingComponent.Component component, int tier, Object value
component.appendIngredients(Map.of(tier, value));
}

public void modify(CraftingComponent.Component component, Map<Number, Object> map) {
Map<Integer, Object> newMap = map.entrySet()
.stream()
.map(entry -> Map.entry(entry.getKey().intValue(), entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
component.appendIngredients(newMap);
public void modify(CraftingComponent.Component component, Map<Object, Object> map) {
component.appendIngredients(toMap(map));
}

public void modifyItem(CraftingComponent.Component component, int tier, ItemStack item) {
component.appendIngredients(Map.of(tier, item));
}

public void modifyItem(CraftingComponent.Component component, Map<Number, ItemStack> map) {
Map<Integer, Object> newMap = map.entrySet()
.stream()
.map(entry -> Map.entry(entry.getKey().intValue(), entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
component.appendIngredients(newMap);
public void modifyItem(CraftingComponent.Component component, Map<Object, ItemStack> map) {
component.appendIngredients(toMap(map));
}

public void modifyTag(CraftingComponent.Component component, int tier, ResourceLocation tag) {
component.appendIngredients(Map.of(tier, TagKey.create(Registries.ITEM, tag)));
}

public void modifyTag(CraftingComponent.Component component, Map<Number, ResourceLocation> map) {
Map<Integer, Object> newMap = map.entrySet()
.stream()
.map(entry -> Map.entry(entry.getKey().intValue(), TagKey.create(Registries.ITEM, entry.getValue())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
component.appendIngredients(newMap);
public void modifyTag(CraftingComponent.Component component, Map<Object, ResourceLocation> map) {
component.appendIngredients(toTagMap(map));
}

public void modifyUnificationEntry(CraftingComponent.Component component, int tier, UnificationEntry item) {
component.appendIngredients(Map.of(tier, item));
}

public void modifyUnificationEntry(CraftingComponent.Component component, Map<Number, UnificationEntry> map) {
Map<Integer, Object> newMap = map.entrySet()
.stream()
.map(entry -> Map.entry(entry.getKey().intValue(), entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
component.appendIngredients(newMap);
public void modifyUnificationEntry(CraftingComponent.Component component, Map<Object, UnificationEntry> map) {
component.appendIngredients(toMap(map));
}

public CraftingComponent.Component create(Map<Number, Object> map) {
Map<Integer, Object> newMap = map.entrySet()
.stream()
.map(entry -> Map.entry(entry.getKey().intValue(), entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return new CraftingComponent.Component(newMap);
public CraftingComponent.Component create(Map<Object, Object> map) {
return new CraftingComponent.Component(toMap(map));
}

public CraftingComponent.Component createItem(Map<Number, ItemStack> map) {
Map<Integer, Object> newMap = map.entrySet()
.stream()
.map(entry -> Map.entry(entry.getKey().intValue(), entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return new CraftingComponent.Component(newMap);
public CraftingComponent.Component createItem(Map<Object, ItemStack> map) {
return new CraftingComponent.Component(toMap(map));
}

public CraftingComponent.Component createTag(Map<Number, ResourceLocation> map) {
Map<Integer, Object> newMap = map.entrySet()
.stream()
.map(entry -> Map.entry(entry.getKey().intValue(), TagKey.create(Registries.ITEM, entry.getValue())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return new CraftingComponent.Component(newMap);
public CraftingComponent.Component createTag(Map<Object, ResourceLocation> map) {
return new CraftingComponent.Component(toTagMap(map));
}

public CraftingComponent.Component createUnificationEntry(Map<Number, UnificationEntry> map) {
Map<Integer, Object> newMap = map.entrySet()
.stream()
.map(entry -> Map.entry(entry.getKey().intValue(), entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return new CraftingComponent.Component(newMap);
public CraftingComponent.Component createUnificationEntry(Map<Object, UnificationEntry> map) {
return new CraftingComponent.Component(toMap(map));
}

private static Map<Integer, Object> toMap(Map<Object, ?> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(
e -> toTier(e.getKey()), Map.Entry::getValue));
}

private static Map<Integer, Object> toTagMap(Map<Object, ResourceLocation> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(
e -> toTier(e.getKey()), e -> TagKey.create(Registries.ITEM, e.getValue())));
}

private static int toTier(Object o) {
if (o instanceof String s) {
Integer tier = GTValues.RVN.get(s);
if (tier == null) {
ConsoleJS.getCurrent((Context) null).error("Unknown tier '" + s + "'!");
throw new IllegalArgumentException("Unknown tier '" + s + "'!");
}
return tier;
} else if (o instanceof Number n) {
int tier = n.intValue();
if (tier < 0 || tier >= GTValues.TIER_COUNT) {
ConsoleJS.getCurrent((Context) null).error("Tier out of range '" + n + "'!");
throw new IllegalArgumentException("Tier out of range '" + n + "'!");
}
return tier;
} else {
ConsoleJS.getCurrent((Context) null).error("Unknown tier '" + o + "'!");
throw new IllegalArgumentException("Unknown tier '" + o + "'!");
}
}
}
Loading