Be a little more untrusting of values we get from outside. Fixes #175

This commit is contained in:
Sollace 2020-08-12 16:41:16 +02:00
parent 5b20402f3f
commit 3cfacb1e7b
4 changed files with 32 additions and 9 deletions

View file

@ -12,6 +12,7 @@ import com.google.common.base.Strings;
import com.minelittlepony.model.armour.ArmourLayer; import com.minelittlepony.model.armour.ArmourLayer;
import com.minelittlepony.model.armour.ArmourVariant; import com.minelittlepony.model.armour.ArmourVariant;
import com.minelittlepony.model.armour.IArmourTextureResolver; import com.minelittlepony.model.armour.IArmourTextureResolver;
import com.minelittlepony.util.ResourceUtil;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -40,10 +41,10 @@ public class DefaultArmourTextureResolver<T extends LivingEntity> implements IAr
texture = texture.substring(idx + 1); texture = texture.substring(idx + 1);
} }
String customType = type.isEmpty() ? "" : String.format("_%s", type); String customType = type.isEmpty() ? "" : "_" + type;
String res = String.format("%s:textures/models/armor/%s_layer_%s%s.png", domain, texture, layer.name().toLowerCase(), customType); String res = ResourceUtil.format("%s:textures/models/armor/%s_layer_%s%s.png", domain, texture, layer, customType);
String oldRes = String.format("%s:textures/models/armor/%s_layer_%d%s.png", domain, texture, layer == ArmourLayer.INNER ? 2 : 1, customType); String oldRes = ResourceUtil.format("%s:textures/models/armor/%s_layer_%d%s.png", domain, texture, layer.getLegacyId(), customType);
Identifier human = getArmorTexture(res, type); Identifier human = getArmorTexture(res, type);
Identifier pony = ponifyResource(human); Identifier pony = ponifyResource(human);

View file

@ -10,6 +10,7 @@ import net.minecraft.village.VillagerProfession;
import net.minecraft.village.VillagerType; import net.minecraft.village.VillagerType;
import com.minelittlepony.client.MineLittlePony; import com.minelittlepony.client.MineLittlePony;
import com.minelittlepony.util.ResourceUtil;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -62,7 +63,7 @@ public class PonyTextures<T extends LivingEntity & VillagerDataContainer> implem
return fallback; return fallback;
} }
String key = String.format("pony/%s/%s", type, profession); String key = ResourceUtil.format("pony/%s/%s", type, profession);
if (cache.containsKey(key)) { if (cache.containsKey(key)) {
return cache.get(key); // People often complain that villagers cause lag, return cache.get(key); // People often complain that villagers cause lag,

View file

@ -4,12 +4,16 @@ package com.minelittlepony.model.armour;
* The layer used to render a given armour piece. * The layer used to render a given armour piece.
*/ */
public enum ArmourLayer { public enum ArmourLayer {
/**
* Fits snugly to the player's model.
*/
INNER,
/** /**
* Hanging loose and sagging free * Hanging loose and sagging free
*/ */
OUTER OUTER,
/**
* Fits snugly to the player's model.
*/
INNER;
public int getLegacyId() {
return ordinal() + 1;
}
} }

View file

@ -0,0 +1,17 @@
package com.minelittlepony.util;
public final class ResourceUtil {
public static String format(String template, Object... args) {
for (int i = 0; i < args.length; i++) {
if (!(args[i] instanceof Number)) {
args[i] = toPathComponent(args[i]);
}
}
return String.format(template, args);
}
private static String toPathComponent(Object value) {
return value.toString().toLowerCase().replaceAll("[^a-z0-9_.-]", "_");
}
}