9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 18:09:27 +00:00

when的值应该是jsonelement

This commit is contained in:
XiaoMoMi
2025-07-14 00:44:46 +08:00
parent 5244524d4b
commit b1a79157b3
6 changed files with 65 additions and 53 deletions

View File

@@ -1,5 +1,7 @@
package net.momirealms.craftengine.core.item;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import net.momirealms.craftengine.core.attribute.AttributeModifier;
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
import net.momirealms.craftengine.core.item.behavior.ItemBehaviors;
@@ -768,35 +770,44 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
) {
if (model.property() instanceof LegacyModelPredicate predicate) {
String predicateId = predicate.legacyPredicateId(materialId);
for (Map.Entry<Either<String, List<String>>, ItemModel> entry : model.whenMap().entrySet()) {
List<String> cases = entry.getKey().fallbackOrMapPrimary(List::of);
for (String caseValue : cases) {
Number legacyValue = predicate.toLegacyValue(caseValue);
if (predicate instanceof TrimMaterialSelectProperty) {
if (legacyValue.floatValue() > 1f) {
continue;
for (Map.Entry<Either<JsonElement, List<JsonElement>>, ItemModel> entry : model.whenMap().entrySet()) {
List<JsonElement> cases = entry.getKey().fallbackOrMapPrimary(List::of);
for (JsonElement caseValue : cases) {
if (caseValue instanceof JsonPrimitive primitive) {
Number legacyValue;
if (primitive.isBoolean()) {
legacyValue = predicate.toLegacyValue(primitive.getAsBoolean());
} else if (primitive.isString()) {
legacyValue = predicate.toLegacyValue(primitive.getAsString());
} else {
legacyValue = predicate.toLegacyValue(primitive.getAsNumber());
}
}
Map<String, Object> merged = mergePredicates(
parentPredicates,
predicateId,
legacyValue
);
// Additional check for crossbow
if (predicate instanceof ChargeTypeSelectProperty && materialId.equals(ItemKeys.CROSSBOW)) {
merged = mergePredicates(
if (predicate instanceof TrimMaterialSelectProperty) {
if (legacyValue.floatValue() > 1f) {
continue;
}
}
Map<String, Object> merged = mergePredicates(
parentPredicates,
predicateId,
legacyValue
);
// Additional check for crossbow
if (predicate instanceof ChargeTypeSelectProperty && materialId.equals(ItemKeys.CROSSBOW)) {
merged = mergePredicates(
merged,
"charged",
1
);
}
processModelRecursively(
entry.getValue(),
merged,
"charged",
1
resultList,
materialId,
customModelData
);
}
processModelRecursively(
entry.getValue(),
merged,
resultList,
materialId,
customModelData
);
}
}
// Additional check for crossbow

View File

@@ -9,6 +9,7 @@ import net.momirealms.craftengine.core.pack.model.select.SelectProperties;
import net.momirealms.craftengine.core.pack.model.select.SelectProperty;
import net.momirealms.craftengine.core.pack.revision.Revision;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.GsonHelper;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MinecraftVersion;
import net.momirealms.craftengine.core.util.MiscUtils;
@@ -25,11 +26,11 @@ public class SelectItemModel implements ItemModel {
public static final Factory FACTORY = new Factory();
public static final Reader READER = new Reader();
private final SelectProperty property;
private final Map<Either<String, List<String>>, ItemModel> whenMap;
private final Map<Either<JsonElement, List<JsonElement>>, ItemModel> whenMap;
private final ItemModel fallBack;
public SelectItemModel(@NotNull SelectProperty property,
@NotNull Map<Either<String, List<String>>, ItemModel> whenMap,
@NotNull Map<Either<JsonElement, List<JsonElement>>, ItemModel> whenMap,
@Nullable ItemModel fallBack) {
this.property = property;
this.whenMap = whenMap;
@@ -40,7 +41,7 @@ public class SelectItemModel implements ItemModel {
return this.property;
}
public Map<Either<String, List<String>>, ItemModel> whenMap() {
public Map<Either<JsonElement, List<JsonElement>>, ItemModel> whenMap() {
return this.whenMap;
}
@@ -55,17 +56,17 @@ public class SelectItemModel implements ItemModel {
this.property.accept(json);
JsonArray array = new JsonArray();
json.add("cases", array);
for (Map.Entry<Either<String, List<String>>, ItemModel> entry : this.whenMap.entrySet()) {
for (Map.Entry<Either<JsonElement, List<JsonElement>>, ItemModel> entry : this.whenMap.entrySet()) {
JsonObject item = new JsonObject();
ItemModel itemModel = entry.getValue();
item.add("model", itemModel.apply(version));
Either<String, List<String>> either = entry.getKey();
Either<JsonElement, List<JsonElement>> either = entry.getKey();
if (either.primary().isPresent()) {
item.addProperty("when", either.primary().get());
item.add("when", either.primary().get());
} else {
List<String> list = either.fallback().get();
List<JsonElement> list = either.fallback().get();
JsonArray whens = new JsonArray();
for (String e : list) {
for (JsonElement e : list) {
whens.add(e);
}
item.add("when", whens);
@@ -118,21 +119,21 @@ public class SelectItemModel implements ItemModel {
if (casesObj instanceof List<?> list) {
List<Map<String, Object>> cases = (List<Map<String, Object>>) list;
if (!cases.isEmpty()) {
Map<Either<String, List<String>>, ItemModel> whenMap = new HashMap<>();
Map<Either<JsonElement, List<JsonElement>>, ItemModel> whenMap = new HashMap<>();
for (Map<String, Object> c : cases) {
Object when = c.get("when");
if (when == null) {
throw new LocalizedResourceConfigException("warning.config.item.model.select.case.missing_when");
}
Either<String, List<String>> either;
Either<JsonElement, List<JsonElement>> either;
if (when instanceof List<?> whenList) {
List<String> whens = new ArrayList<>(whenList.size());
List<JsonElement> whens = new ArrayList<>(whenList.size());
for (Object o : whenList) {
whens.add(o.toString());
whens.add(GsonHelper.get().toJsonTree(o));
}
either = Either.ofFallback(whens);
} else {
either = Either.ofPrimary(when.toString());
either = Either.ofPrimary(GsonHelper.get().toJsonTree(when));
}
Object model = c.get("model");
if (model == null) {
@@ -158,22 +159,22 @@ public class SelectItemModel implements ItemModel {
if (cases == null) {
throw new IllegalArgumentException("cases is expected to be a JsonArray");
}
Map<Either<String, List<String>>, ItemModel> whenMap = new HashMap<>(cases.size());
Map<Either<JsonElement, List<JsonElement>>, ItemModel> whenMap = new HashMap<>(cases.size());
for (JsonElement e : cases) {
if (e instanceof JsonObject caseObj) {
ItemModel model = ItemModels.fromJson(caseObj.getAsJsonObject("model"));
JsonElement whenObj = caseObj.get("when");
Either<String, List<String>> either;
Either<JsonElement, List<JsonElement>> either;
if (whenObj instanceof JsonArray array) {
List<String> whens = new ArrayList<>(array.size());
List<JsonElement> whens = new ArrayList<>(array.size());
for (JsonElement o : array) {
whens.add(o.getAsString());
whens.add(o);
}
either = Either.ofFallback(whens);
} else if (whenObj instanceof JsonPrimitive primitive) {
either = Either.ofPrimary(primitive.getAsString());
} else if (whenObj != null) {
either = Either.ofPrimary(whenObj);
} else {
throw new IllegalArgumentException("when is expected to be either JsonPrimitive or JsonArray");
throw new IllegalArgumentException("'when' should not be null");
}
whenMap.put(either, model);
} else {

View File

@@ -7,7 +7,7 @@ import net.momirealms.craftengine.core.util.Key;
import java.util.Map;
public class CrossBowPullingRangeDispatchProperty implements RangeDispatchProperty, LegacyModelPredicate<Float> {
public class CrossBowPullingRangeDispatchProperty implements RangeDispatchProperty, LegacyModelPredicate<Number> {
public static final Factory FACTORY = new Factory();
public static final Reader READER = new Reader();
public static final CrossBowPullingRangeDispatchProperty INSTANCE = new CrossBowPullingRangeDispatchProperty();
@@ -29,7 +29,7 @@ public class CrossBowPullingRangeDispatchProperty implements RangeDispatchProper
}
@Override
public Number toLegacyValue(Float value) {
public Number toLegacyValue(Number value) {
return value;
}

View File

@@ -7,7 +7,7 @@ import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
public class CustomModelDataRangeDispatchProperty implements RangeDispatchProperty, LegacyModelPredicate<Float> {
public class CustomModelDataRangeDispatchProperty implements RangeDispatchProperty, LegacyModelPredicate<Number> {
public static final Factory FACTORY = new Factory();
public static final Reader READER = new Reader();
private final int index;
@@ -33,7 +33,7 @@ public class CustomModelDataRangeDispatchProperty implements RangeDispatchProper
}
@Override
public Number toLegacyValue(Float value) {
public Number toLegacyValue(Number value) {
return value.intValue();
}

View File

@@ -7,7 +7,7 @@ import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
public class DamageRangeDispatchProperty implements RangeDispatchProperty, LegacyModelPredicate<Float> {
public class DamageRangeDispatchProperty implements RangeDispatchProperty, LegacyModelPredicate<Number> {
public static final Factory FACTORY = new Factory();
public static final Reader READER = new Reader();
private final boolean normalize;
@@ -36,7 +36,7 @@ public class DamageRangeDispatchProperty implements RangeDispatchProperty, Legac
}
@Override
public Number toLegacyValue(Float value) {
public Number toLegacyValue(Number value) {
if (this.normalize) return value;
throw new RuntimeException("Enable 'normalize' option if you want to use 'damage' on 1.21.3 and below");
}

View File

@@ -8,7 +8,7 @@ import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.Map;
public class UseDurationRangeDispatchProperty implements RangeDispatchProperty, LegacyModelPredicate<Float> {
public class UseDurationRangeDispatchProperty implements RangeDispatchProperty, LegacyModelPredicate<Number> {
public static final Factory FACTORY = new Factory();
public static final Reader READER = new Reader();
private final boolean remaining;
@@ -37,7 +37,7 @@ public class UseDurationRangeDispatchProperty implements RangeDispatchProperty,
}
@Override
public Number toLegacyValue(Float value) {
public Number toLegacyValue(Number value) {
return value;
}