Skip to content

Commit

Permalink
More recipe issue fixes, added StaticRegistries class
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jun 11, 2024
1 parent cf02150 commit 8367c89
Show file tree
Hide file tree
Showing 42 changed files with 401 additions and 500 deletions.
107 changes: 107 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/BaseProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package dev.latvian.mods.kubejs;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

public class BaseProperties {
private final Path path;
private final String name;
protected final Properties properties;
private boolean writeProperties;

protected BaseProperties(Path path, String name) {
this.path = path;
this.name = name;
this.properties = new Properties();

try {
writeProperties = false;

if (Files.exists(path)) {
try (var reader = Files.newBufferedReader(path)) {
properties.load(reader);
}
} else {
writeProperties = true;
}

load();

if (writeProperties) {
save();
}
} catch (Exception ex) {
ex.printStackTrace();
}

KubeJS.LOGGER.info("Loaded " + path.getFileName());
}

protected void load() {
}

public void remove(String key) {
var s = properties.getProperty(key);

if (s != null) {
properties.remove(key);
writeProperties = true;
}
}

public String get(String key, String def) {
var s = properties.getProperty(key);

if (s == null) {
properties.setProperty(key, def);
writeProperties = true;
return def;
}

return s;
}

public boolean get(String key, boolean def) {
return get(key, def ? "true" : "false").equals("true");
}

public int get(String key, int def) {
return Integer.parseInt(get(key, Integer.toString(def)));
}

public double get(String key, double def) {
return Double.parseDouble(get(key, Double.toString(def)));
}

public int getColor(String key, int def) {
var s = get(key, String.format("%06X", def & 0xFFFFFF));

if (s.isEmpty() || s.equals("default")) {
return def;
}

try {
return 0xFFFFFF & Integer.decode(s.startsWith("#") ? s : ("#" + s));
} catch (Exception ex) {
ex.printStackTrace();
return def;
}
}

public float[] getColor3f(int color) {
var c = new float[3];
c[0] = ((color >> 16) & 0xFF) / 255F;
c[1] = ((color >> 8) & 0xFF) / 255F;
c[2] = ((color >> 0) & 0xFF) / 255F;
return c;
}

public void save() {
try (var writer = Files.newBufferedWriter(path)) {
properties.store(writer, name);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
94 changes: 19 additions & 75 deletions src/main/java/dev/latvian/mods/kubejs/CommonProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import dev.latvian.mods.kubejs.util.KubeJSPlugins;

import java.nio.file.Files;
import java.util.Properties;

public class CommonProperties {
public class CommonProperties extends BaseProperties {
private static CommonProperties instance;

public static CommonProperties get() {
Expand All @@ -20,9 +17,6 @@ public static void reload() {
instance = new CommonProperties();
}

private final Properties properties;
private boolean writeProperties;

public boolean hideServerScriptErrors;
public boolean serverOnly;
public boolean announceReload;
Expand All @@ -36,74 +30,24 @@ public static void reload() {
public String creativeModeTabIcon;

private CommonProperties() {
properties = new Properties();

try {
writeProperties = false;

if (Files.exists(KubeJSPaths.COMMON_PROPERTIES)) {
try (var reader = Files.newBufferedReader(KubeJSPaths.COMMON_PROPERTIES)) {
properties.load(reader);
}
} else {
writeProperties = true;
}

hideServerScriptErrors = get("hideServerScriptErrors", false);
serverOnly = get("serverOnly", false);
announceReload = get("announceReload", true);
packMode = get("packmode", "");
saveDevPropertiesInConfig = get("saveDevPropertiesInConfig", false);
allowAsyncStreams = get("allowAsyncStreams", true);
matchJsonRecipes = get("matchJsonRecipes", true);
ignoreCustomUniqueRecipeIds = get("ignoreCustomUniqueRecipeIds", false);
startupErrorGUI = get("startupErrorGUI", true);
startupErrorReportUrl = get("startupErrorReportUrl", "");
creativeModeTabIcon = get("creativeModeTabIcon", "minecraft:purple_dye");

KubeJSPlugins.forEachPlugin(this, KubeJSPlugin::loadCommonProperties);

if (writeProperties) {
save();
}
} catch (Exception ex) {
ex.printStackTrace();
}

KubeJS.LOGGER.info("Loaded common.properties");
}

public void remove(String key) {
var s = properties.getProperty(key);

if (s != null) {
properties.remove(key);
writeProperties = true;
}
}

public String get(String key, String def) {
var s = properties.getProperty(key);

if (s == null) {
properties.setProperty(key, def);
writeProperties = true;
return def;
}

return s;
}

public boolean get(String key, boolean def) {
return get(key, def ? "true" : "false").equals("true");
}

public void save() {
try (var writer = Files.newBufferedWriter(KubeJSPaths.COMMON_PROPERTIES)) {
properties.store(writer, "KubeJS Common Properties");
} catch (Exception ex) {
ex.printStackTrace();
}
super(KubeJSPaths.COMMON_PROPERTIES, "KubeJS Common Properties");
}

@Override
protected void load() {
hideServerScriptErrors = get("hideServerScriptErrors", false);
serverOnly = get("serverOnly", false);
announceReload = get("announceReload", true);
packMode = get("packmode", "");
saveDevPropertiesInConfig = get("saveDevPropertiesInConfig", false);
allowAsyncStreams = get("allowAsyncStreams", true);
matchJsonRecipes = get("matchJsonRecipes", true);
ignoreCustomUniqueRecipeIds = get("ignoreCustomUniqueRecipeIds", false);
startupErrorGUI = get("startupErrorGUI", true);
startupErrorReportUrl = get("startupErrorReportUrl", "");
creativeModeTabIcon = get("creativeModeTabIcon", "minecraft:purple_dye");

KubeJSPlugins.forEachPlugin(this, KubeJSPlugin::loadCommonProperties);
}

public void setPackMode(String s) {
Expand Down
97 changes: 19 additions & 78 deletions src/main/java/dev/latvian/mods/kubejs/DevProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import dev.latvian.mods.kubejs.util.KubeJSPlugins;
import net.neoforged.fml.loading.FMLLoader;

import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.util.Properties;

public class DevProperties {
public class DevProperties extends BaseProperties {
private static DevProperties instance;

public static DevProperties get() {
Expand All @@ -23,9 +18,6 @@ public static void reload() {
instance = new DevProperties();
}

private final Properties properties;
private boolean writeProperties;

public boolean debugInfo;
public boolean dataPackOutput = false;
public boolean logAddedRecipes = false;
Expand All @@ -41,76 +33,25 @@ public static void reload() {
public boolean alwaysCaptureErrors = false;

private DevProperties() {
properties = new Properties();

try {
var propertiesFile = KubeJSPaths.getLocalDevProperties();
writeProperties = false;

if (Files.exists(propertiesFile)) {
try (Reader reader = Files.newBufferedReader(propertiesFile)) {
properties.load(reader);
}
} else {
writeProperties = true;
}

debugInfo = get("debugInfo", !FMLLoader.isProduction());
dataPackOutput = get("dataPackOutput", false);
logAddedRecipes = get("logAddedRecipes", false);
logRemovedRecipes = get("logRemovedRecipes", false);
logModifiedRecipes = get("logModifiedRecipes", false);
logSkippedRecipes = get("logSkippedRecipes", false);
logSkippedTags = get("logSkippedTags", false);
logErroringRecipes = get("logErroringRecipes", true);
logInvalidRecipeHandlers = get("logInvalidRecipeHandlers", true);
logSkippedPlugins = get("logSkippedPlugins", true);
logGeneratedData = get("logGeneratedData", false);
strictTags = get("strictTags", false);
alwaysCaptureErrors = get("alwaysCaptureErrors", false);

KubeJSPlugins.forEachPlugin(this, KubeJSPlugin::loadDevProperties);

if (writeProperties) {
save();
}
} catch (Exception ex) {
ex.printStackTrace();
}

KubeJS.LOGGER.info("Loaded dev.properties");
super(KubeJSPaths.getLocalDevProperties(), "KubeJS Dev Properties");
}

public void remove(String key) {
var s = properties.getProperty(key);

if (s != null) {
properties.remove(key);
writeProperties = true;
}
}

public String get(String key, String def) {
var s = properties.getProperty(key);

if (s == null) {
properties.setProperty(key, def);
writeProperties = true;
return def;
}

return s;
}

public boolean get(String key, boolean def) {
return get(key, def ? "true" : "false").equals("true");
}

public void save() {
try (Writer writer = Files.newBufferedWriter(KubeJSPaths.getLocalDevProperties())) {
properties.store(writer, "KubeJS Dev Properties");
} catch (Exception ex) {
ex.printStackTrace();
}
@Override
protected void load() {
debugInfo = get("debugInfo", !FMLLoader.isProduction());
dataPackOutput = get("dataPackOutput", false);
logAddedRecipes = get("logAddedRecipes", false);
logRemovedRecipes = get("logRemovedRecipes", false);
logModifiedRecipes = get("logModifiedRecipes", false);
logSkippedRecipes = get("logSkippedRecipes", false);
logSkippedTags = get("logSkippedTags", false);
logErroringRecipes = get("logErroringRecipes", true);
logInvalidRecipeHandlers = get("logInvalidRecipeHandlers", true);
logSkippedPlugins = get("logSkippedPlugins", true);
logGeneratedData = get("logGeneratedData", false);
strictTags = get("strictTags", false);
alwaysCaptureErrors = get("alwaysCaptureErrors", false);

KubeJSPlugins.forEachPlugin(this, KubeJSPlugin::loadDevProperties);
}
}
1 change: 1 addition & 0 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static Path dir(Path dir) {
Path COMMON_PROPERTIES = CONFIG.resolve("common.properties");
Path CLIENT_PROPERTIES = CONFIG.resolve("client.properties");
Path CONFIG_DEV_PROPERTIES = CONFIG.resolve("dev.properties");
Path LOGGING_PROPERTIES = CONFIG.resolve("logging.properties");
Path PACKICON = CONFIG.resolve("packicon.png");
Path README = DIRECTORY.resolve("README.txt");
Path LOCAL = dir(GAMEDIR.resolve("local").resolve("kubejs"));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.latvian.mods.kubejs;

import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.util.NBTUtils;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.rhino.Context;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.NbtOps;
import net.minecraft.util.valueproviders.ClampedInt;
import net.minecraft.util.valueproviders.ClampedNormalInt;
import net.minecraft.util.valueproviders.ConstantInt;
Expand Down Expand Up @@ -54,7 +54,7 @@ static IntProvider intProviderOf(Context cx, Object o) {
}
}

var decoded = IntProvider.CODEC.parse(NbtOps.INSTANCE, NBTUtils.toTagCompound(cx, m)).result();
var decoded = IntProvider.CODEC.parse(((KubeJSContext) cx).getNbtOps(), NBTUtils.toTagCompound(cx, m)).result();
if (decoded.isPresent()) {
return decoded.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class DamageSourceWrapper {
public static DamageSource of(Context cx, Object from) {
return switch (from) {
case DamageSource source -> source;
case Player player -> ((KubeJSContext) cx).getDamageSources().playerAttack(player);
case LivingEntity livingEntity -> ((KubeJSContext) cx).getDamageSources().mobAttack(livingEntity);
case null, default -> ((KubeJSContext) cx).getDamageSources().source(ResourceKey.create(Registries.DAMAGE_TYPE, ID.mc(from)));
case Player player -> ((KubeJSContext) cx).getRegistries().damageSources().get().playerAttack(player);
case LivingEntity livingEntity -> ((KubeJSContext) cx).getRegistries().damageSources().get().mobAttack(livingEntity);
case null, default -> ((KubeJSContext) cx).getRegistries().damageSources().get().source(ResourceKey.create(Registries.DAMAGE_TYPE, ID.mc(from)));
};
}
}
Loading

0 comments on commit 8367c89

Please sign in to comment.