9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-23 17:09:19 +00:00

让事情变得更加美好

This commit is contained in:
XiaoMoMi
2025-04-30 00:28:56 +08:00
parent f875e1dee2
commit 2e871eeef3
69 changed files with 220 additions and 363 deletions

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
import net.momirealms.craftengine.core.loot.LootTable;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.shared.block.BlockBehavior;
@@ -54,13 +55,12 @@ public abstract class CustomBlock {
String nbtString = entry.getKey();
CompoundTag tag = BlockNbtParser.deserialize(this, nbtString);
if (tag == null) {
CraftEngine.instance().logger().warn("Illegal block state: " + nbtString);
continue;
throw new LocalizedResourceConfigException("warning.config.block.state.property.invalid_format", nbtString);
}
VariantState variantState = entry.getValue();
int vanillaStateRegistryId = appearances.getOrDefault(variantState.appearance(), -1);
// This should never happen
if (vanillaStateRegistryId == -1) {
CraftEngine.instance().logger().warn("Could not find appearance " + variantState.appearance() + " for block " + id);
vanillaStateRegistryId = appearances.values().iterator().next();
}
// Late init states

View File

@@ -7,6 +7,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import net.momirealms.craftengine.shared.block.BlockBehavior;
import net.momirealms.craftengine.shared.block.EmptyBlockBehavior;
@@ -25,11 +26,8 @@ public class BlockBehaviors {
public static BlockBehavior fromMap(CustomBlock block, @Nullable Map<String, Object> map) {
if (map == null || map.isEmpty()) return EmptyBlockBehavior.INSTANCE;
Object type = map.get("type");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.block.behavior.missing_type");
}
Key key = Key.withDefaultNamespace(type.toString(), "craftengine");
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.block.behavior.missing_type");
Key key = Key.withDefaultNamespace(type, "craftengine");
BlockBehaviorFactory factory = BuiltInRegistries.BLOCK_BEHAVIOR_FACTORY.getValue(key);
if (factory == null) {
throw new LocalizedResourceConfigException("warning.config.block.behavior.invalid_type", type.toString());

View File

@@ -5,10 +5,7 @@ import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Direction;
import net.momirealms.craftengine.core.util.HorizontalDirection;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceKey;
import net.momirealms.craftengine.core.util.*;
import java.util.Map;
@@ -35,11 +32,8 @@ public class Properties {
}
public static Property<?> fromMap(String name, Map<String, Object> map) {
Object type = map.get("type");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.block.state.property.missing_type", name);
}
Key key = Key.withDefaultNamespace(type.toString(), "craftengine");
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.block.state.property.missing_type");
Key key = Key.withDefaultNamespace(type, "craftengine");
PropertyFactory factory = BuiltInRegistries.PROPERTY_FACTORY.getValue(key);
if (factory == null) {
throw new LocalizedResourceConfigException("warning.config.block.state.property.invalid_type", key.toString(), name);

View File

@@ -24,7 +24,7 @@ public class HitBoxTypes {
}
public static HitBox fromMap(Map<String, Object> arguments) {
Key type = Optional.ofNullable((String) arguments.get("type")).map(Key::of).orElse(HitBoxTypes.INTERACTION);
Key type = Optional.ofNullable(arguments.get("type")).map(String::valueOf).map(Key::of).orElse(HitBoxTypes.INTERACTION);
HitBoxFactory factory = BuiltInRegistries.HITBOX_FACTORY.getValue(type);
if (factory == null) {
throw new LocalizedResourceConfigException("warning.config.furniture.hitbox.invalid_type", type.toString());

View File

@@ -7,7 +7,9 @@ import net.momirealms.craftengine.core.util.CharacterUtils;
import net.momirealms.craftengine.core.util.FormatUtils;
import net.momirealms.craftengine.core.util.Key;
public class BitmapImage implements FontProvider {
import java.util.function.Supplier;
public class BitmapImage implements Supplier<JsonObject> {
private final Key id;
private final Key font;
private final int height;

View File

@@ -1,8 +0,0 @@
package net.momirealms.craftengine.core.font;
import com.google.gson.JsonObject;
import java.util.function.Supplier;
public interface FontProvider extends Supplier<JsonObject> {
}

View File

@@ -7,6 +7,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.nio.file.Path;
@@ -23,11 +24,8 @@ public class ItemBehaviors {
public static ItemBehavior fromMap(Pack pack, Path path, Key id, Map<String, Object> map) {
if (map == null || map.isEmpty()) return EmptyItemBehavior.INSTANCE;
Object type = map.get("type");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.item.behavior.missing_type");
}
Key key = Key.withDefaultNamespace(type.toString(), "craftengine");
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.item.behavior.missing_type");
Key key = Key.withDefaultNamespace(type, "craftengine");
ItemBehaviorFactory factory = BuiltInRegistries.ITEM_BEHAVIOR_FACTORY.getValue(key);
if (factory == null) {
throw new LocalizedResourceConfigException("warning.config.item.behavior.invalid_type", type.toString());

View File

@@ -7,6 +7,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.util.EnumUtils;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.*;
@@ -34,10 +35,7 @@ public abstract class AbstractRecipeFactory<T> implements RecipeFactory<T> {
}
protected Object getIngredientOrThrow(Map<String, Object> arguments) {
Object ingredient = arguments.get("ingredient");
if (ingredient == null) {
ingredient = arguments.get("ingredients");
}
Object ingredient = ResourceConfigUtils.get(arguments, "ingredient", "ingredients");
if (ingredient == null) {
throw new LocalizedResourceConfigException("warning.config.recipe.missing_ingredient");
}

View File

@@ -18,10 +18,7 @@ public interface RecipeFactory<T> {
if (resultMap == null) {
throw new LocalizedResourceConfigException("warning.config.recipe.missing_result");
}
String id = (String) resultMap.get("id");
if (id == null) {
throw new LocalizedResourceConfigException("warning.config.recipe.result.missing_id");
}
String id = ResourceConfigUtils.requireNonEmptyStringOrThrow(resultMap.get("id"), "warning.config.recipe.result.missing_id");
int count = ResourceConfigUtils.getAsInt(resultMap.getOrDefault("count", 1), "count");
return new CustomRecipeResult(
CraftEngine.instance().itemManager().getBuildableItem(Key.of(id)).orElseThrow(

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.Map;
@@ -40,10 +41,7 @@ public class RecipeTypes {
@SuppressWarnings("unchecked")
public static <T> Recipe<T> fromMap(Key id, Map<String, Object> map) {
String type = (String) map.get("type");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.recipe.missing_type");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.recipe.missing_type");
Key key = Key.withDefaultNamespace(type, "minecraft");
RecipeFactory<T> factory = (RecipeFactory<T>) BuiltInRegistries.RECIPE_FACTORY.getValue(key);
if (factory == null) {

View File

@@ -12,6 +12,7 @@ import net.momirealms.craftengine.core.loot.number.NumberProvider;
import net.momirealms.craftengine.core.loot.number.NumberProviders;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.context.ContextHolder;
import net.momirealms.craftengine.core.world.World;
import org.jetbrains.annotations.Nullable;
@@ -40,10 +41,7 @@ public class LootTable<T> {
@SuppressWarnings("unchecked")
public static <T> LootTable<T> fromMap(Map<String, Object> map) {
if (map == null || map.isEmpty()) return null;
Object pools = map.get("pools");
if (pools == null) {
throw new LocalizedResourceConfigException("warning.config.loot_table.missing_pools");
}
Object pools = ResourceConfigUtils.requireNonNullOrThrow(map.get("pools"), "warning.config.loot_table.missing_pools");
if (!(pools instanceof List<?> list) || list.isEmpty()) {
throw new LocalizedResourceConfigException("warning.config.loot_table.invalid_pools_type", pools.getClass().getSimpleName());
}

View File

@@ -6,8 +6,8 @@ import net.momirealms.craftengine.core.loot.condition.LootConditions;
import net.momirealms.craftengine.core.loot.number.NumberProvider;
import net.momirealms.craftengine.core.loot.number.NumberProviders;
import net.momirealms.craftengine.core.loot.parameter.LootParameters;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Collections;
import java.util.List;
@@ -44,10 +44,7 @@ public class ExpLootEntryContainer<T> extends AbstractLootEntryContainer<T> {
@SuppressWarnings("unchecked")
@Override
public LootEntryContainer<A> create(Map<String, Object> arguments) {
Object value = arguments.get("count");
if (value == null) {
throw new LocalizedResourceConfigException("warning.config.loot_table.entry.exp.missing_count");
}
Object value = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("count"), "warning.config.loot_table.entry.exp.missing_count");
List<LootCondition> conditions = Optional.ofNullable(arguments.get("conditions"))
.map(it -> LootConditions.fromMapList((List<Map<String, Object>>) it))
.orElse(Collections.emptyList());

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.ArrayList;
@@ -40,10 +41,7 @@ public class LootEntryContainers {
@SuppressWarnings("unchecked")
public static <T> LootEntryContainer<T> fromMap(Map<String, Object> map) {
String type = (String) map.get("type");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.loot_table.entry.missing_type");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.loot_table.entry.missing_type");
Key key = Key.withDefaultNamespace(type, "craftengine");
LootEntryContainerFactory<T> factory = (LootEntryContainerFactory<T>) BuiltInRegistries.LOOT_ENTRY_CONTAINER_FACTORY.getValue(key);
if (factory == null) {

View File

@@ -8,8 +8,8 @@ import net.momirealms.craftengine.core.loot.function.LootFunction;
import net.momirealms.craftengine.core.loot.function.LootFunctions;
import net.momirealms.craftengine.core.loot.parameter.LootParameters;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.*;
import java.util.function.Consumer;
@@ -43,13 +43,10 @@ public class SingleItemLootEntryContainer<T> extends AbstractSingleLootEntryCont
@SuppressWarnings("unchecked")
@Override
public LootEntryContainer<A> create(Map<String, Object> arguments) {
Object itemObj = arguments.get("item");
if (itemObj == null) {
throw new LocalizedResourceConfigException("warning.config.loot_table.entry.item.missing_item");
}
Key item = Key.from(itemObj.toString());
int weight = (int) arguments.getOrDefault("weight", 1);
int quality = (int) arguments.getOrDefault("quality", 0);
String itemObj = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("item"), "warning.config.loot_table.entry.item.missing_item");
Key item = Key.from(itemObj);
int weight = ResourceConfigUtils.getAsInt(arguments.getOrDefault("weight", 1), "weight");
int quality = ResourceConfigUtils.getAsInt(arguments.getOrDefault("quality", 0), "quality");
List<LootCondition> conditions = Optional.ofNullable(arguments.get("conditions"))
.map(it -> LootConditions.fromMapList((List<Map<String, Object>>) it))
.orElse(Collections.emptyList());

View File

@@ -49,10 +49,7 @@ public class ApplyBonusCountFunction<T> extends AbstractLootConditionalFunction<
@SuppressWarnings("unchecked")
@Override
public LootFunction<T> create(Map<String, Object> arguments) {
String enchantment = (String) arguments.get("enchantment");
if (enchantment == null || enchantment.isEmpty()) {
throw new LocalizedResourceConfigException("warning.config.loot_table.function.apply_bonus.missing_enchantment");
}
String enchantment = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("enchantment"), "warning.config.loot_table.function.apply_bonus.missing_enchantment");
Map<String, Object> formulaMap = MiscUtils.castToMap(arguments.get("formula"), true);
if (formulaMap == null) {
throw new LocalizedResourceConfigException("warning.config.loot_table.function.apply_bonus.missing_formula");

View File

@@ -7,8 +7,8 @@ import net.momirealms.craftengine.core.loot.condition.LootConditions;
import net.momirealms.craftengine.core.loot.number.NumberProvider;
import net.momirealms.craftengine.core.loot.number.NumberProviders;
import net.momirealms.craftengine.core.loot.parameter.LootParameters;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Collections;
import java.util.List;
@@ -40,10 +40,7 @@ public class DropExpFunction<T> extends AbstractLootConditionalFunction<T> {
@SuppressWarnings("unchecked")
@Override
public LootFunction<T> create(Map<String, Object> arguments) {
Object value = arguments.get("count");
if (value == null) {
throw new LocalizedResourceConfigException("warning.config.loot_table.function.drop_exp.missing_count");
}
Object value = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("count"), "warning.config.loot_table.function.drop_exp.missing_count");
List<LootCondition> conditions = Optional.ofNullable(arguments.get("conditions"))
.map(it -> LootConditions.fromMapList((List<Map<String, Object>>) it))
.orElse(Collections.emptyList());

View File

@@ -8,6 +8,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.ArrayList;
@@ -68,10 +69,7 @@ public class LootFunctions {
@SuppressWarnings("unchecked")
public static <T> LootFunction<T> fromMap(Map<String, Object> map) {
String type = (String) map.get("type");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.loot_table.function.missing_type");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.loot_table.function.missing_type");
Key key = Key.withDefaultNamespace(type, "craftengine");
LootFunctionFactory<T> factory = (LootFunctionFactory<T>) BuiltInRegistries.LOOT_FUNCTION_FACTORY.getValue(key);
if (factory == null) {

View File

@@ -6,8 +6,8 @@ import net.momirealms.craftengine.core.loot.condition.LootCondition;
import net.momirealms.craftengine.core.loot.condition.LootConditions;
import net.momirealms.craftengine.core.loot.number.NumberProvider;
import net.momirealms.craftengine.core.loot.number.NumberProviders;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Collections;
import java.util.List;
@@ -42,10 +42,7 @@ public class SetCountFunction<T> extends AbstractLootConditionalFunction<T> {
@SuppressWarnings("unchecked")
@Override
public LootFunction<A> create(Map<String, Object> arguments) {
Object value = arguments.get("count");
if (value == null) {
throw new LocalizedResourceConfigException("warning.config.loot_table.function.set_count.missing_count");
}
Object value = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("count"), "warning.config.loot_table.function.set_count.missing_count");
boolean add = (boolean) arguments.getOrDefault("add", false);
List<LootCondition> conditions = Optional.ofNullable(arguments.get("conditions"))
.map(it -> LootConditions.fromMapList((List<Map<String, Object>>) it))

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.ArrayList;
@@ -37,10 +38,7 @@ public class NumberProviders {
}
public static NumberProvider fromMap(Map<String, Object> map) {
String type = (String) map.get("type");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.loot_table.number.missing_type");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.loot_table.number.missing_type");
Key key = Key.withDefaultNamespace(type, "craftengine");
NumberProviderFactory factory = BuiltInRegistries.NUMBER_PROVIDER_FACTORY.getValue(key);
if (factory == null) {

View File

@@ -1,6 +1,6 @@
package net.momirealms.craftengine.core.pack.conflict.matcher;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
@@ -44,7 +44,7 @@ public class AllOfPathMatcher implements PathMatcher {
Map<String, Object> terms = MiscUtils.castToMap(termsObj, false);
return new AllOfPathMatcher(PathMatchers.fromMapList(List.of(terms)));
} else {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.all_of.missing_terms");
throw new LocalizedException("warning.config.conflict_matcher.all_of.missing_terms");
}
}
}

View File

@@ -1,6 +1,6 @@
package net.momirealms.craftengine.core.pack.conflict.matcher;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
@@ -44,7 +44,7 @@ public class AnyOfPathMatcher implements PathMatcher {
Map<String, Object> terms = MiscUtils.castToMap(termsObj, false);
return new AnyOfPathMatcher(PathMatchers.fromMapList(List.of(terms)));
} else {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.any_of.missing_terms");
throw new LocalizedException("warning.config.conflict_matcher.any_of.missing_terms");
}
}
}

View File

@@ -1,7 +1,8 @@
package net.momirealms.craftengine.core.pack.conflict.matcher;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.nio.file.Path;
import java.util.Map;
@@ -29,11 +30,8 @@ public class ExactPathMatcher implements PathMatcher {
@Override
public PathMatcher create(Map<String, Object> arguments) {
Object path = arguments.get("path");
if (path == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.exact.missing_path");
}
return new ExactPathMatcher(path.toString());
String path = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("path"), () -> new LocalizedException("warning.config.conflict_matcher.exact.missing_path"));
return new ExactPathMatcher(path);
}
}
}

View File

@@ -1,7 +1,8 @@
package net.momirealms.craftengine.core.pack.conflict.matcher;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.nio.file.Path;
import java.util.Map;
@@ -29,11 +30,8 @@ public class FilenameMatcher implements PathMatcher {
@Override
public PathMatcher create(Map<String, Object> arguments) {
Object name = arguments.get("name");
if (name == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.filename.missing_name");
}
return new FilenameMatcher(name.toString());
String name = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("name"), () -> new LocalizedException("warning.config.conflict_matcher.filename.missing_name"));
return new FilenameMatcher(name);
}
}
}

View File

@@ -1,8 +1,9 @@
package net.momirealms.craftengine.core.pack.conflict.matcher;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.nio.file.Path;
import java.util.Map;
@@ -29,10 +30,7 @@ public class InvertedPathMatcher implements PathMatcher {
@Override
public PathMatcher create(Map<String, Object> arguments) {
Object inverted = arguments.get("term");
if (inverted == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.inverted.missing_term");
}
Object inverted = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("term"), () -> new LocalizedException("warning.config.conflict_matcher.inverted.missing_term"));
Map<String, Object> term = MiscUtils.castToMap(inverted, false);
return new InvertedPathMatcher(PathMatchers.fromMap(term));
}

View File

@@ -1,7 +1,8 @@
package net.momirealms.craftengine.core.pack.conflict.matcher;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.nio.file.Path;
import java.util.Map;
@@ -31,11 +32,8 @@ public class ParentPathPrefixMatcher implements PathMatcher {
@Override
public PathMatcher create(Map<String, Object> arguments) {
Object prefix = arguments.get("prefix");
if (prefix == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.parent_prefix.missing_prefix");
}
return new ParentPathPrefixMatcher(prefix.toString());
String prefix = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("prefix"), () -> new LocalizedException("warning.config.conflict_matcher.parent_prefix.missing_prefix"));
return new ParentPathPrefixMatcher(prefix);
}
}
}

View File

@@ -1,7 +1,8 @@
package net.momirealms.craftengine.core.pack.conflict.matcher;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.nio.file.Path;
import java.util.Map;
@@ -31,11 +32,8 @@ public class ParentPathSuffixMatcher implements PathMatcher {
@Override
public PathMatcher create(Map<String, Object> arguments) {
Object suffix = arguments.get("suffix");
if (suffix == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.parent_suffix.missing_suffix");
}
return new ParentPathSuffixMatcher(suffix.toString());
String suffix = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("suffix"), () -> new LocalizedException("warning.config.conflict_matcher.parent_suffix.missing_suffix"));
return new ParentPathSuffixMatcher(suffix);
}
}
}

View File

@@ -1,7 +1,8 @@
package net.momirealms.craftengine.core.pack.conflict.matcher;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.nio.file.Path;
import java.util.Map;
@@ -29,11 +30,8 @@ public class PathContainsMatcher implements PathMatcher {
@Override
public PathMatcher create(Map<String, Object> arguments) {
Object path = arguments.get("path");
if (path == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.contains.missing_path");
}
return new PathContainsMatcher(path.toString());
String path = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("path"), () -> new LocalizedException("warning.config.conflict_matcher.contains.missing_path"));
return new PathContainsMatcher(path);
}
}
}

View File

@@ -1,11 +1,12 @@
package net.momirealms.craftengine.core.pack.conflict.matcher;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.ArrayList;
@@ -49,14 +50,11 @@ public class PathMatchers {
}
public static PathMatcher fromMap(Map<String, Object> map) {
String type = (String) map.getOrDefault("type", "empty");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.missing_type");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), () -> new LocalizedException("warning.config.conflict_matcher.missing_type"));
Key key = Key.withDefaultNamespace(type, "craftengine");
PathMatcherFactory factory = BuiltInRegistries.PATH_MATCHER_FACTORY.getValue(key);
if (factory == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.invalid_type", type);
throw new LocalizedException("warning.config.conflict_matcher.invalid_type", type);
}
return factory.create(map);
}

View File

@@ -1,23 +1,29 @@
package net.momirealms.craftengine.core.pack.conflict.matcher;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.nio.file.Path;
import java.util.Map;
import java.util.regex.Pattern;
public class PathPatternMatcher implements PathMatcher {
public static final Factory FACTORY = new Factory();
private final String pattern;
private final Pattern pattern;
public PathPatternMatcher(String pattern) {
this.pattern = Pattern.compile(pattern);
}
public PathPatternMatcher(Pattern pattern) {
this.pattern = pattern;
}
@Override
public boolean test(Path path) {
String pathStr = path.toString().replace("\\", "/");
return pathStr.matches(pattern);
return this.pattern.matcher(pathStr).matches();
}
@Override
@@ -25,15 +31,16 @@ public class PathPatternMatcher implements PathMatcher {
return PathMatchers.PATTERN;
}
public Pattern pattern() {
return pattern;
}
public static class Factory implements PathMatcherFactory {
@Override
public PathMatcher create(Map<String, Object> arguments) {
Object pattern = arguments.get("pattern");
if (pattern == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_matcher.pattern.missing_pattern");
}
return new PathPatternMatcher(pattern.toString());
String pattern = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("pattern"), () -> new LocalizedException("warning.config.conflict_matcher.pattern.missing_pattern"));
return new PathPatternMatcher(pattern);
}
}
}

View File

@@ -207,7 +207,7 @@ public class MergePackMcMetaResolution implements Resolution {
public static class Factory implements ResolutionFactory {
@Override
public Resolution create(Map<String, Object> arguments) {
String description = (String) arguments.getOrDefault("description", "<gray>CraftEngine ResourcePack");
String description = arguments.getOrDefault("description", "<gray>CraftEngine ResourcePack</gray>").toString();
return new MergePackMcMetaResolution(description);
}
}

View File

@@ -1,11 +1,12 @@
package net.momirealms.craftengine.core.pack.conflict.resolution;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.Map;
@@ -31,14 +32,11 @@ public class Resolutions {
}
public static Resolution fromMap(Map<String, Object> map) {
String type = (String) map.getOrDefault("type", "empty");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_resolution.missing_type");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), () -> new LocalizedException("warning.config.conflict_resolution.missing_type"));
Key key = Key.withDefaultNamespace(type, "craftengine");
ResolutionFactory factory = BuiltInRegistries.RESOLUTION_FACTORY.getValue(key);
if (factory == null) {
throw new LocalizedResourceConfigException("warning.config.conflict_resolution.invalid_type", type);
throw new LocalizedException("warning.config.conflict_resolution.invalid_type", type);
}
return factory.create(map);
}

View File

@@ -292,10 +292,7 @@ public class AlistHost implements ResourcePackHost {
@Override
public ResourcePackHost create(Map<String, Object> arguments) {
boolean useEnv = (boolean) arguments.getOrDefault("use-environment-variables", false);
String apiUrl = Optional.ofNullable(arguments.get("api-url")).map(String::valueOf).orElse(null);
if (apiUrl == null || apiUrl.isEmpty()) {
throw new LocalizedException("warning.config.host.alist.missing_api_url");
}
String apiUrl = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("api-url"), () -> new LocalizedException("warning.config.host.alist.missing_api_url"));
String userName = useEnv ? System.getenv("CE_ALIST_USERNAME") : Optional.ofNullable(arguments.get("username")).map(String::valueOf).orElse(null);
if (userName == null || userName.isEmpty()) {
throw new LocalizedException("warning.config.host.alist.missing_username");
@@ -307,10 +304,7 @@ public class AlistHost implements ResourcePackHost {
String filePassword = useEnv ? System.getenv("CE_ALIST_FILE_PASSWORD") : arguments.getOrDefault("file-password", "").toString();
String otpCode = Optional.ofNullable(arguments.get("otp-code")).map(String::valueOf).orElse(null);
Duration jwtTokenExpiration = Duration.ofHours((int) arguments.getOrDefault("jwt-token-expiration", 48));
String uploadPath = Optional.ofNullable(arguments.get("upload-path")).map(String::valueOf).orElse(null);
if (uploadPath == null || uploadPath.isEmpty()) {
throw new LocalizedException("warning.config.host.alist.missing_upload_path");
}
String uploadPath = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("upload-path"), () -> new LocalizedException("warning.config.host.alist.missing_upload_path"));
boolean disableUpload = (boolean) arguments.getOrDefault("disable-upload", false);
ProxySelector proxy = getProxySelector(MiscUtils.castToMap(arguments.get("proxy"), true));
return new AlistHost(apiUrl, userName, password, filePassword, otpCode, jwtTokenExpiration, uploadPath, disableUpload, proxy);

View File

@@ -8,10 +8,7 @@ import net.momirealms.craftengine.core.pack.host.ResourcePackHostFactory;
import net.momirealms.craftengine.core.pack.host.ResourcePackHosts;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.util.GsonHelper;
import net.momirealms.craftengine.core.util.HashUtils;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.*;
import java.io.IOException;
import java.io.InputStream;
@@ -279,10 +276,7 @@ public class DropboxHost implements ResourcePackHost {
if (refreshToken == null || refreshToken.isEmpty()) {
throw new LocalizedException("warning.config.host.dropbox.missing_refresh_token");
}
String uploadPath = Optional.ofNullable(arguments.get("upload-path")).map(String::valueOf).orElse(null);
if (uploadPath == null || uploadPath.isEmpty()) {
throw new LocalizedException("warning.config.host.dropbox.missing_upload_path");
}
String uploadPath = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("upload-path"), () -> new LocalizedException("warning.config.host.dropbox.missing_upload_path"));
ProxySelector proxy = getProxySelector(MiscUtils.castToMap(arguments.get("proxy"), true));
return new DropboxHost(appKey, appSecret, refreshToken, "/" + uploadPath, proxy);
}

View File

@@ -206,18 +206,12 @@ public class S3Host implements ResourcePackHost {
Map<String, Object> proxySetting = MiscUtils.castToMap(arguments.get("proxy"), true);
if (proxySetting != null) {
String host = Optional.ofNullable(proxySetting.get("host")).map(String::valueOf).orElse(null);
if (host == null || host.isEmpty()) {
throw new LocalizedException("warning.config.host.proxy.missing_host");
}
String host = ResourceConfigUtils.requireNonEmptyStringOrThrow(proxySetting.get("host"), () -> new LocalizedException("warning.config.host.proxy.missing_host"));
int port = ResourceConfigUtils.getAsInt(proxySetting.get("port"), "port");
if (port <= 0 || port > 65535) {
throw new LocalizedException("warning.config.host.proxy.missing_port");
}
String scheme = Optional.ofNullable(proxySetting.get("scheme")).map(String::valueOf).orElse(null);
if (scheme == null || scheme.isEmpty()) {
throw new LocalizedException("warning.config.host.proxy.missing_scheme");
}
String scheme = ResourceConfigUtils.requireNonEmptyStringOrThrow(proxySetting.get("scheme"), () -> new LocalizedException("warning.config.host.proxy.missing_scheme"));
String username = Optional.ofNullable(proxySetting.get("username")).map(String::valueOf).orElse(null);
String password = Optional.ofNullable(proxySetting.get("password")).map(String::valueOf).orElse(null);
ProxyConfiguration.Builder builder = ProxyConfiguration.builder().host(host).port(port).scheme(scheme);

View File

@@ -14,7 +14,6 @@ import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@@ -62,12 +61,9 @@ public class SelfHost implements ResourcePackHost {
@Override
public ResourcePackHost create(Map<String, Object> arguments) {
SelfHostHttpServer selfHostHttpServer = SelfHostHttpServer.instance();
String ip = Optional.ofNullable(arguments.get("ip")).map(String::valueOf).orElse(null);
if (ip == null) {
throw new LocalizedException("warning.config.host.self.missing_ip");
}
String ip = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("ip"), () -> new LocalizedException("warning.config.host.self.missing_ip"));
int port = ResourceConfigUtils.getAsInt(arguments.getOrDefault("port", 8163), "port");
if (port < 0 || port > 65535) {
if (port <= 0 || port > 65535) {
throw new LocalizedException("warning.config.host.self.invalid_port", String.valueOf(port));
}
boolean oneTimeToken = (boolean) arguments.getOrDefault("one-time-token", true);

View File

@@ -9,6 +9,7 @@ import net.momirealms.craftengine.core.pack.model.tint.Tints;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
@@ -69,11 +70,7 @@ public class BaseItemModel implements ItemModel {
@SuppressWarnings("unchecked")
@Override
public ItemModel create(Map<String, Object> arguments) {
Object path = arguments.get("path");
if (path == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.base.missing_path");
}
String modelPath = path.toString();
String modelPath = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("path"), "warning.config.item.model.base.missing_path");
if (!ResourceLocation.isValid(modelPath)) {
throw new LocalizedResourceConfigException("warning.config.item.model.base.invalid_path", modelPath);
}

View File

@@ -36,7 +36,7 @@ public class ItemModels {
}
public static ItemModel fromMap(Map<String, Object> map) {
String type = (String) map.getOrDefault("type", "minecraft:model");
String type = map.getOrDefault("type", "minecraft:model").toString();
Key key = Key.withDefaultNamespace(type, "minecraft");
ItemModelFactory factory = BuiltInRegistries.ITEM_MODEL_FACTORY.getValue(key);
if (factory == null) {

View File

@@ -97,7 +97,6 @@ public class SelectItemModel implements ItemModel {
public ItemModel create(Map<String, Object> arguments) {
SelectProperty property = SelectProperties.fromMap(arguments);
Map<String, Object> fallback = MiscUtils.castToMap(arguments.get("fallback"), true);
Object casesObj = arguments.get("cases");
if (casesObj instanceof List<?> list) {
List<Map<String, Object>> cases = (List<Map<String, Object>>) list;

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.Map;
@@ -46,10 +47,7 @@ public class ConditionProperties {
}
public static ConditionProperty fromMap(Map<String, Object> map) {
String type = (String) map.get("property");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.condition.missing_property");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("property"), "warning.config.item.model.condition.missing_property");
Key key = Key.withDefaultNamespace(type, "minecraft");
ConditionPropertyFactory factory = BuiltInRegistries.CONDITION_PROPERTY_FACTORY.getValue(key);
if (factory == null) {

View File

@@ -1,8 +1,8 @@
package net.momirealms.craftengine.core.pack.model.condition;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
@@ -35,12 +35,8 @@ public class HasComponentConditionProperty implements ConditionProperty {
@Override
public ConditionProperty create(Map<String, Object> arguments) {
boolean ignoreDefault = (boolean) arguments.getOrDefault("ignore-default", false);
Object componentObj = arguments.get("component");
if (componentObj == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.condition.has_component.missing_component");
}
String component = componentObj.toString();
return new HasComponentConditionProperty(component, ignoreDefault);
String componentObj = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("component"), "warning.config.item.model.condition.has_component.missing_component");
return new HasComponentConditionProperty(componentObj, ignoreDefault);
}
}
}

View File

@@ -1,8 +1,8 @@
package net.momirealms.craftengine.core.pack.model.condition;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
@@ -29,12 +29,8 @@ public class KeyBindDownConditionProperty implements ConditionProperty {
@Override
public ConditionProperty create(Map<String, Object> arguments) {
Object keybindObj = arguments.get("keybind");
if (keybindObj == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.condition.keybind.missing");
}
String keybind = keybindObj.toString();
return new KeyBindDownConditionProperty(keybind);
String keybindObj = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("keybind"), "warning.config.item.model.condition.keybind.missing");
return new KeyBindDownConditionProperty(keybindObj);
}
}
}

View File

@@ -1,8 +1,8 @@
package net.momirealms.craftengine.core.pack.model.rangedisptach;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
@@ -29,12 +29,8 @@ public class CompassRangeDispatchProperty implements RangeDispatchProperty {
@Override
public RangeDispatchProperty create(Map<String, Object> arguments) {
Object targetObj = arguments.get("target");
if (targetObj == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.range_dispatch.compass.missing_target");
}
String target = targetObj.toString();
return new CompassRangeDispatchProperty(target);
String targetObj = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("target"), "warning.config.item.model.range_dispatch.compass.missing_target");
return new CompassRangeDispatchProperty(targetObj);
}
}
}

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.Map;
@@ -42,10 +43,7 @@ public class RangeDispatchProperties {
}
public static RangeDispatchProperty fromMap(Map<String, Object> map) {
String type = (String) map.get("property");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.range_dispatch.missing_property");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("property"), "warning.config.item.model.range_dispatch.missing_property");
Key key = Key.withDefaultNamespace(type, "minecraft");
RangeDispatchPropertyFactory factory = BuiltInRegistries.RANGE_DISPATCH_PROPERTY_FACTORY.getValue(key);
if (factory == null) {

View File

@@ -1,8 +1,8 @@
package net.momirealms.craftengine.core.pack.model.rangedisptach;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
@@ -34,13 +34,9 @@ public class TimeRangeDispatchProperty implements RangeDispatchProperty {
@Override
public RangeDispatchProperty create(Map<String, Object> arguments) {
Object sourceObj = arguments.get("source");
if (sourceObj == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.range_dispatch.time.missing_source");
}
String source = sourceObj.toString();
String sourceObj = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("source"), "warning.config.item.model.range_dispatch.time.missing_source");
boolean wobble = (boolean) arguments.getOrDefault("wobble", true);
return new TimeRangeDispatchProperty(source, wobble);
return new TimeRangeDispatchProperty(sourceObj, wobble);
}
}
}

View File

@@ -1,8 +1,8 @@
package net.momirealms.craftengine.core.pack.model.select;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
@@ -29,12 +29,8 @@ public class BlockStateSelectProperty implements SelectProperty {
@Override
public SelectProperty create(Map<String, Object> arguments) {
Object property = arguments.get("block-state-property");
if (property == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.select.block_state.missing_property");
}
String blockStateProperty = property.toString();
return new BlockStateSelectProperty(blockStateProperty);
String property = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("block-state-property"), "warning.config.item.model.select.block_state.missing_property");
return new BlockStateSelectProperty(property);
}
}
}

View File

@@ -2,6 +2,7 @@ package net.momirealms.craftengine.core.pack.model.select;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -42,11 +43,7 @@ public class LocalTimeSelectProperty implements SelectProperty {
@Override
public SelectProperty create(Map<String, Object> arguments) {
Object patternObj = arguments.get("pattern");
if (patternObj == null) {
throw new IllegalArgumentException("warning.config.item.model.select.local_time.missing_pattern", new NullPointerException("pattern should not be null"));
}
String pattern = patternObj.toString();
String pattern = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("pattern"), "warning.config.item.model.select.local_time.missing_pattern");
String locale = (String) arguments.get("locale");
String timeZone = (String) arguments.get("time-zone");
return new LocalTimeSelectProperty(pattern, locale, timeZone);

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.Map;
@@ -40,10 +41,7 @@ public class SelectProperties {
}
public static SelectProperty fromMap(Map<String, Object> map) {
String type = (String) map.get("property");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.select.missing_property");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("property"), "warning.config.item.model.select.missing_property");
Key key = Key.withDefaultNamespace(type, "minecraft");
SelectPropertyFactory factory = BuiltInRegistries.SELECT_PROPERTY_FACTORY.getValue(key);
if (factory == null) {

View File

@@ -1,8 +1,8 @@
package net.momirealms.craftengine.core.pack.model.special;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
@@ -31,11 +31,8 @@ public class BannerSpecialModel implements SpecialModel {
@Override
public SpecialModel create(Map<String, Object> arguments) {
Object color = arguments.get("color");
if (color == null) {
throw new LocalizedResourceConfigException("");
}
return new BannerSpecialModel(color.toString());
String color = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("color"), "warning.config.item.model.special.banner.missing_color");
return new BannerSpecialModel(color);
}
}
}

View File

@@ -2,9 +2,9 @@ package net.momirealms.craftengine.core.pack.model.special;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
import java.util.Objects;
public class BedSpecialModel implements SpecialModel {
public static final Factory FACTORY = new Factory();
@@ -31,7 +31,7 @@ public class BedSpecialModel implements SpecialModel {
@Override
public SpecialModel create(Map<String, Object> arguments) {
String color = Objects.requireNonNull(arguments.get("texture"), "texture").toString();
String color = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("texture"), "warning.config.item.model.special.bed.missing_texture");
return new BedSpecialModel(color);
}
}

View File

@@ -36,14 +36,11 @@ public class ChestSpecialModel implements SpecialModel {
@Override
public SpecialModel create(Map<String, Object> arguments) {
float openness = ResourceConfigUtils.getAsFloat(arguments.getOrDefault("openness", 0), "openness");
Object texture = arguments.get("texture");
if (texture == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.special.chest.missing_texture");
}
String texture = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("texture"), "warning.config.item.model.special.chest.missing_texture");
if (openness > 1 || openness < 0) {
throw new LocalizedResourceConfigException("warning.config.item.model.special.chest.invalid_openness", String.valueOf(openness));
}
return new ChestSpecialModel(texture.toString(), openness);
return new ChestSpecialModel(texture, openness);
}
}
}

View File

@@ -1,7 +1,6 @@
package net.momirealms.craftengine.core.pack.model.special;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
@@ -38,16 +37,10 @@ public class HeadSpecialModel implements SpecialModel {
@Override
public SpecialModel create(Map<String, Object> arguments) {
Object kind = arguments.get("kind");
if (kind == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.special.head.missing_kind");
}
Object texture = arguments.get("texture");
if (texture == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.special.head.missing_texture");
}
String kind = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("kind"), "warning.config.item.model.special.head.missing_kind");
String texture = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("texture"), "warning.config.item.model.special.head.missing_texture");
int animation = ResourceConfigUtils.getAsInt(arguments.getOrDefault("animation", 0), "animation");
return new HeadSpecialModel(kind.toString(), texture.toString(), animation);
return new HeadSpecialModel(kind, texture, animation);
}
}
}

View File

@@ -41,15 +41,12 @@ public class ShulkerBoxSpecialModel implements SpecialModel {
@Override
public SpecialModel create(Map<String, Object> arguments) {
float openness = ResourceConfigUtils.getAsFloat(arguments.getOrDefault("openness", 0), "openness");
Object texture = arguments.get("texture");
if (texture == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.special.shulker_box.missing_texture");
}
String texture = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("texture"), "warning.config.item.model.special.shulker_box.missing_texture");
Direction orientation = Direction.valueOf(arguments.getOrDefault("orientation", "down").toString().toUpperCase(Locale.ENGLISH));
if (openness > 1 || openness < 0) {
throw new LocalizedResourceConfigException("warning.config.item.model.special.shulker_box.invalid_openness", String.valueOf(openness));
}
return new ShulkerBoxSpecialModel(texture.toString(), openness, orientation);
return new ShulkerBoxSpecialModel(texture, openness, orientation);
}
}
}

View File

@@ -1,12 +1,10 @@
package net.momirealms.craftengine.core.pack.model.special;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
import java.util.Objects;
public class SignSpecialModel implements SpecialModel {
public static final Factory FACTORY = new Factory();
@@ -39,8 +37,8 @@ public class SignSpecialModel implements SpecialModel {
@Override
public SpecialModel create(Map<String, Object> arguments) {
Key type = Key.of(arguments.get("type").toString());
String woodType = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("wood-type"), "warning.config.item.model.special.sign.missing_wood_type").toString();
String texture = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("texture"), "warning.config.item.model.special.sign.missing_texture").toString();
String woodType = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("wood-type"), "warning.config.item.model.special.sign.missing_wood_type");
String texture = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("texture"), "warning.config.item.model.special.sign.missing_texture");
return new SignSpecialModel(type, woodType, texture);
}
}

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.Map;
@@ -43,10 +44,7 @@ public class SpecialModels {
}
public static SpecialModel fromMap(Map<String, Object> map) {
String type = (String) map.get("type");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.special.missing_type");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.item.model.special.missing_type");
Key key = Key.withDefaultNamespace(type, "minecraft");
SpecialModelFactory factory = BuiltInRegistries.SPECIAL_MODEL_FACTORY.getValue(key);
if (factory == null) {

View File

@@ -1,8 +1,8 @@
package net.momirealms.craftengine.core.pack.model.tint;
import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import org.incendo.cloud.type.Either;
import java.util.List;
@@ -33,10 +33,7 @@ public class ConstantTint implements Tint {
@Override
public Tint create(Map<String, Object> arguments) {
Object value = arguments.get("value");
if (value == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.tint.constant.missing_value");
}
Object value = ResourceConfigUtils.requireNonNullOrThrow("warning.config.item.model.special.missing_type", "warning.config.item.model.tint.constant.missing_value");
return new ConstantTint(parseTintValue(value));
}
}

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import java.util.Map;
@@ -38,10 +39,7 @@ public class Tints {
}
public static Tint fromMap(Map<String, Object> map) {
String type = (String) map.get("type");
if (type == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.tint.missing_type");
}
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.item.model.tint.missing_type");
Key key = Key.withDefaultNamespace(type, "minecraft");
TintFactory factory = BuiltInRegistries.TINT_FACTORY.getValue(key);
if (factory == null) {

View File

@@ -75,16 +75,13 @@ public abstract class AbstractSoundManager implements SoundManager {
@Override
public void parseSection(Pack pack, Path path, Key id, Map<String, Object> section) {
if (AbstractSoundManager.this.songs.containsKey(id)) {
throw new LocalizedResourceConfigException("warning.config.jukebox_song.duplicate", path, id);
}
Object sound = section.get("sound");
if (sound == null) {
throw new LocalizedResourceConfigException("warning.config.jukebox_song.missing_sound", path, id);
throw new LocalizedResourceConfigException("warning.config.jukebox_song.duplicate");
}
String sound = ResourceConfigUtils.requireNonEmptyStringOrThrow(section.get("sound"), "warning.config.jukebox_song.missing_sound");
Component description = AdventureHelper.miniMessage().deserialize(section.getOrDefault("description", "").toString());
float length = ResourceConfigUtils.getAsFloat(section.get("length"), "length");
int comparatorOutput = ResourceConfigUtils.getAsInt(section.getOrDefault("comparator-output", 15), "comparator-output");
JukeboxSong song = new JukeboxSong(Key.of(sound.toString()), description, length, comparatorOutput, ResourceConfigUtils.getAsFloat(section.getOrDefault("range", 32f), "range"));
JukeboxSong song = new JukeboxSong(Key.of(sound), description, length, comparatorOutput, ResourceConfigUtils.getAsFloat(section.getOrDefault("range", 32f), "range"));
AbstractSoundManager.this.songs.put(id, song);
}
}
@@ -105,14 +102,11 @@ public abstract class AbstractSoundManager implements SoundManager {
@Override
public void parseSection(Pack pack, Path path, Key id, Map<String, Object> section) {
if (AbstractSoundManager.this.byId.containsKey(id)) {
throw new LocalizedResourceConfigException("warning.config.sound.duplicate", path, id);
throw new LocalizedResourceConfigException("warning.config.sound.duplicate");
}
boolean replace = (boolean) section.getOrDefault("replace", false);
String subtitle = (String) section.get("subtitle");
List<?> soundList = (List<?>) section.get("sounds");
if (soundList == null) {
throw new LocalizedResourceConfigException("warning.config.sound.missing_sounds", path, id);
}
List<?> soundList = (List<?>) ResourceConfigUtils.requireNonNullOrThrow(section.get("sounds"), "warning.config.sound.missing_sounds");
List<Sound> sounds = new ArrayList<>();
for (Object sound : soundList) {
if (sound instanceof String soundPath) {

View File

@@ -1,7 +1,11 @@
package net.momirealms.craftengine.core.util;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import java.util.Map;
import java.util.function.Supplier;
public final class ResourceConfigUtils {
private ResourceConfigUtils() {}
@@ -12,6 +16,36 @@ public final class ResourceConfigUtils {
return obj;
}
public static <T> T requireNonNullOrThrow(T obj, Supplier<LocalizedException> exceptionSupplier) {
if (obj == null)
throw exceptionSupplier.get();
return obj;
}
public static String requireNonEmptyStringOrThrow(Object obj, String node) {
Object o = requireNonNullOrThrow(obj, node);
String s = o.toString();
if (s.isEmpty()) throw new LocalizedResourceConfigException(node);
return s;
}
public static String requireNonEmptyStringOrThrow(Object obj, Supplier<LocalizedException> exceptionSupplier) {
Object o = requireNonNullOrThrow(obj, exceptionSupplier);
String s = o.toString();
if (s.isEmpty()) throw exceptionSupplier.get();
return s;
}
public static Object get(Map<String, Object> arguments, String... keys) {
for (String key : keys) {
Object value = arguments.get(key);
if (value != null) {
return value;
}
}
return null;
}
public static int getAsInt(Object o, String option) {
switch (o) {
case null -> {