Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2002' into 2004
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/dev/latvian/mods/kubejs/bindings/TextWrapper.java
#	src/main/java/dev/latvian/mods/kubejs/core/RecipeHolderKJS.java
#	src/main/java/dev/latvian/mods/kubejs/recipe/ingredientaction/ConsumeAction.java
  • Loading branch information
MaxNeedsSnacks committed Feb 21, 2024
2 parents 2314f30 + bf682e2 commit 99b2c98
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 34 deletions.
2 changes: 0 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Locale;

public class KubeJS {
public static final String MOD_ID = "kubejs";
Expand Down Expand Up @@ -86,7 +85,6 @@ public static ScriptManager getClientScriptManager() {
public KubeJS() throws Throwable {
instance = this;
gameDirectory = Platform.getGameFolder().normalize().toAbsolutePath();
Locale.setDefault(Locale.US);

if (Files.notExists(KubeJSPaths.README)) {
try {
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/bindings/TextWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.contents.PlainTextContents;
import net.minecraft.network.chat.contents.ScoreContents;
import net.minecraft.network.chat.contents.SelectorContents;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

@Info("The hub for all things text components. Format text to your hearts content!")
public class TextWrapper {
Expand Down Expand Up @@ -180,6 +183,11 @@ public static MutableComponent join(MutableComponent separator, Iterable<? exten
return joined;
}

@Info("Returns an empty component")
public static MutableComponent empty() {
return Component.empty();
}

@Info("Joins all components")
public static MutableComponent join(Component... texts) {
return join(Component.empty(), Arrays.asList(texts));
Expand All @@ -190,21 +198,51 @@ public static MutableComponent string(String text) {
return Component.literal(text);
}

@Info("Returns a plain component of the input")
public static MutableComponent literal(String text) {
return Component.literal(text);
}

@Info("Returns a translatable component of the input key")
public static MutableComponent translate(String key) {
return Component.translatable(key, new Object[0]);
return Component.translatable(key);
}

@Info("Returns a translatable component of the input key, with args of the objects")
public static MutableComponent translate(String key, Object... objects) {
return Component.translatable(key, objects);
}

@Info("Returns a translatable component of the input key")
public static MutableComponent translatable(String key) {
return Component.translatable(key);
}

@Info("Returns a translatable component of the input key, with args of the objects")
public static MutableComponent translatable(String key, Object... objects) {
return Component.translatable(key, objects);
}

@Info("Returns a keybinding component of the input keybinding descriptor")
public static MutableComponent keybind(String keybind) {
return Component.keybind(keybind);
}

@Info("Returns a score component of the input objective, for the provided selector")
public static MutableComponent score(String selector, String objective) {
return MutableComponent.create(new ScoreContents(selector, objective));
}

@Info("Returns a component displaying all entities matching the input selector")
public static MutableComponent selector(String selector) {
return MutableComponent.create(new SelectorContents(selector, Optional.empty()));
}

@Info("Returns a component displaying all entities matching the input selector, with a custom separator")
public static MutableComponent selector(String selector, Component separator) {
return MutableComponent.create(new SelectorContents(selector, Optional.of(separator)));
}

@Info("Returns a component of the input, colored black")
public static MutableComponent black(Object text) {
return of(text).kjs$black();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeSerializer;
Expand Down Expand Up @@ -66,7 +67,13 @@ default boolean replaceInput(ReplacementMatch match, InputReplacement with) {
}

default boolean hasOutput(ReplacementMatch match) {
return match instanceof ItemMatch m && m.contains(kjs$getRecipe().getResultItem(UtilsJS.staticRegistryAccess));
if (match instanceof ItemMatch m) {
var result = kjs$getRecipe().getResultItem(UtilsJS.staticRegistryAccess);
//noinspection ConstantValue
return result != null && result != ItemStack.EMPTY && !result.isEmpty() && m.contains(result);
}

return false;
}

default boolean replaceOutput(ReplacementMatch match, OutputReplacement with) {
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/dev/latvian/mods/kubejs/recipe/JsonRecipeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.latvian.mods.kubejs.util.UtilsJS;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;

public class JsonRecipeJS extends RecipeJS {
@Override
Expand Down Expand Up @@ -33,14 +34,11 @@ public boolean replaceInput(ReplacementMatch match, InputReplacement with) {
}

@Override
@SuppressWarnings("ConstantValue")
public boolean hasOutput(ReplacementMatch match) {
if (CommonProperties.get().matchJsonRecipes && match instanceof ItemMatch m && getOriginalRecipe() != null) {
var r = getOriginalRecipe().getResultItem(UtilsJS.staticRegistryAccess);
//noinspection ConstantValue
if (r == null) {
throw new NullPointerException("ItemStack should never be null, but recipe " + this + " returned null as the output!");
}
return r != ItemStack.EMPTY && m.contains(r);
var result = ((Recipe<?>) this).getResultItem(UtilsJS.staticRegistryAccess);
return result != null && result != ItemStack.EMPTY && !result.isEmpty() && m.contains(result);
}

return false;
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/recipe/RecipeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.latvian.mods.kubejs.recipe.component.MissingComponentException;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentBuilderMap;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentValue;
import dev.latvian.mods.kubejs.recipe.ingredientaction.ConsumeAction;
import dev.latvian.mods.kubejs.recipe.ingredientaction.CustomIngredientAction;
import dev.latvian.mods.kubejs.recipe.ingredientaction.DamageAction;
import dev.latvian.mods.kubejs.recipe.ingredientaction.IngredientAction;
Expand Down Expand Up @@ -553,7 +554,9 @@ public ItemStack getOriginalRecipeResult() {
return ItemStack.EMPTY;
}

return getOriginalRecipe().getResultItem(UtilsJS.staticRegistryAccess);
var result = getOriginalRecipe().getResultItem(UtilsJS.staticRegistryAccess);
//noinspection ConstantValue
return result == null ? ItemStack.EMPTY : result;
}

public List<Ingredient> getOriginalRecipeIngredients() {
Expand Down Expand Up @@ -596,6 +599,10 @@ public final RecipeJS keepIngredient(IngredientActionFilter filter) {
return ingredientAction(filter, new KeepAction());
}

public final RecipeJS consumeIngredient(IngredientActionFilter filter) {
return ingredientAction(filter, new ConsumeAction());
}

public final RecipeJS modifyResult(ModifyRecipeResultCallback callback) {
modifyResult = callback;
save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.Bootstrap;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeManager;
Expand Down Expand Up @@ -110,7 +111,9 @@ private static String recipeToString(Recipe<?> recipe) {
}

try {
map.put("out", recipe.getResultItem(UtilsJS.staticRegistryAccess).kjs$toItemString());
var result = recipe.getResultItem(UtilsJS.staticRegistryAccess);
//noinspection ConstantValue
map.put("out", (result == null ? ItemStack.EMPTY : result).kjs$toItemString());
} catch (Exception ex) {
map.put("out_error", ex.toString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.latvian.mods.kubejs.recipe.ingredientaction;

import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.ItemStack;

public class ConsumeAction extends IngredientAction {
@Override
public ItemStack transform(ItemStack old, int index, CraftingContainer container) {
return ItemStack.EMPTY;
}

@Override
public String getType() {
return "consume";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public abstract class IngredientAction extends IngredientActionFilter {
FACTORY_MAP.put("damage", json -> new DamageAction(json.get("damage").getAsInt()));
FACTORY_MAP.put("replace", json -> new ReplaceAction(ItemStackJS.resultFromRecipeJson(json.get("item"))));
FACTORY_MAP.put("keep", json -> new KeepAction());
FACTORY_MAP.put("consume", json -> new ConsumeAction());
}

public static List<IngredientAction> parseList(JsonElement json) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public interface KubeJSCraftingRecipe extends CraftingRecipe {
}

var modifyResult = kjs$getModifyResult();
var result = getResultItem(registryAccess).copy();
var result = getResultItem(registryAccess);
//noinspection ConstantValue
result = (result == null || result.isEmpty()) ? ItemStack.EMPTY : result.copy();
if (modifyResult != null) {
return modifyResult.modify(new ModifyRecipeCraftingGrid(container), result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.latvian.mods.kubejs.generator.DataJsonGenerator;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.UtilsJS;
import net.minecraft.Util;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

Expand Down Expand Up @@ -117,7 +118,7 @@ public void generateAssetJsons(AssetJsonGenerator generator) {

public String getBuilderTranslationKey() {
if (translationKey.isEmpty()) {
return getTranslationKeyGroup() + '.' + id.getNamespace() + '.' + id.getPath();
return Util.makeDescriptionId(getTranslationKeyGroup(), id);
}

return translationKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class ScriptFileInfo {
private static final Pattern FILE_FIXER = Pattern.compile("[^\\w./]");
private static final Pattern PROPERTY_PATTERN = Pattern.compile("^(\\w+)\\s*[:=]?\\s*(\\w+)$");
private static final Pattern PROPERTY_PATTERN = Pattern.compile("^(\\w+)\\s*[:=]?\\s*(-?\\w+)$");

public final ScriptPackInfo pack;
public final String file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public IoSupplier<InputStream> getResource(PackType type, ResourceLocation locat

@Override
public void listResources(PackType packType, String namespace, String path, ResourceOutput visitor) {
if (!path.endsWith("/")) {
path = path + "/";
}

for (ResourceLocation r : locationToData.keySet()) {
if (!r.getPath().endsWith(".mcmeta")) {
if (r.getNamespace().equals(namespace) && r.getPath().startsWith(path)) {
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/dev/latvian/mods/kubejs/server/DataExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,21 @@ private void exportData0() throws Exception {
appendLine(logStringBuilder, calendar, line);
}

logStringBuilder.setLength(logStringBuilder.length() - 1);
addString("errors.log", logStringBuilder.toString());
if (logStringBuilder.length() > 0) {
logStringBuilder.setLength(logStringBuilder.length() - 1);
addString("errors.log", logStringBuilder.toString());
}

logStringBuilder.setLength(0);

for (var line : ConsoleJS.SERVER.warnings) {
appendLine(logStringBuilder, calendar, line);
}

logStringBuilder.setLength(logStringBuilder.length() - 1);
addString("warnings.log", logStringBuilder.toString());
if (logStringBuilder.length() > 0) {
logStringBuilder.setLength(logStringBuilder.length() - 1);
addString("warnings.log", logStringBuilder.toString());
}

var modArr = new JsonArray();

Expand Down
27 changes: 13 additions & 14 deletions src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.google.gson.internal.Streams;
Expand Down Expand Up @@ -101,27 +102,25 @@ public static Object toPrimitive(@Nullable JsonElement element) {
return null;
}

@Nullable
public static Map<?, ?> read(Path path) throws IOException {
if (Files.notExists(path)) {
public static JsonElement readJson(Path path) throws IOException {
if (!Files.isRegularFile(path)) {
return null;
}

try (var fileReader = Files.newBufferedReader(path)) {
var jsonReader = new JsonReader(fileReader);
JsonElement element;
var lenient = jsonReader.isLenient();
jsonReader.setLenient(true);
element = Streams.parse(jsonReader);

if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) {
throw new JsonSyntaxException("Did not consume the entire document.");
}

return MapJS.of(element);
return JsonParser.parseReader(fileReader);
}
}

public static String readString(Path path) throws IOException {
return toString(readJson(path));
}

@Nullable
public static Map<?, ?> read(Path path) throws IOException {
return MapJS.of(readJson(path));
}

public static void write(Path path, @Nullable JsonObject json) throws IOException {
if (json == null || json.isJsonNull()) {
Files.deleteIfExists(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public interface NBTIOWrapper {
@Nullable
static CompoundTag read(Path path) throws IOException {
if (Files.notExists(path)) {
if (!Files.isRegularFile(path)) {
return null;
}

Expand Down

0 comments on commit 99b2c98

Please sign in to comment.