handle invalid identifiers, fixes #980
This commit is contained in:
1 parent
8dc1556790
commit
d07fe18bb5
6 files changed
+47
-49
No files matched your search
@@ -154,7 +154,7 @@ public class Auxiliary {
|
||||
return ListValue.wrap(Registry.SOUND_EVENT.getIds().stream().map(ValueConversions::of));
|
||||
}
|
||||
String rawString = lv.get(0).getString();
|
||||
Identifier soundName = new Identifier(rawString);
|
||||
Identifier soundName = InputValidator.identifierOf(rawString);
|
||||
Vector3Argument locator = Vector3Argument.findIn(lv, 1);
|
||||
if (Registry.SOUND_EVENT.get(soundName) == null)
|
||||
throw new ThrowStatement(rawString, Throwables.UNKNOWN_SOUND);
|
||||
@@ -950,15 +950,8 @@ public class Auxiliary {
|
||||
if (player == null) return Value.NULL;
|
||||
Identifier category;
|
||||
Identifier statName;
|
||||
try
|
||||
{
|
||||
category = new Identifier(lv.get(1).getString());
|
||||
statName = new Identifier(lv.get(2).getString());
|
||||
}
|
||||
catch (InvalidIdentifierException e)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
category = InputValidator.identifierOf(lv.get(1).getString());
|
||||
statName = InputValidator.identifierOf(lv.get(2).getString());
|
||||
StatType<?> type = Registry.STAT_TYPE.get(category);
|
||||
if (type == null) return Value.NULL;
|
||||
Stat<?> stat = getStat(type, statName);
|
||||
@@ -1011,12 +1004,12 @@ public class Auxiliary {
|
||||
if (lv.size() == 0)
|
||||
return ListValue.wrap(storage.getIds().map(i -> new StringValue(nameFromRegistryId(i))).collect(Collectors.toList()));
|
||||
String key = lv.get(0).getString();
|
||||
CompoundTag old_nbt = storage.get(new Identifier(key));
|
||||
CompoundTag old_nbt = storage.get(InputValidator.identifierOf(key));
|
||||
if (lv.size() == 2) {
|
||||
Value nbt = lv.get(1);
|
||||
NBTSerializableValue new_nbt = (nbt instanceof NBTSerializableValue) ? (NBTSerializableValue) nbt
|
||||
: NBTSerializableValue.parseString(nbt.getString(), true);
|
||||
storage.set(new Identifier(key), new_nbt.getCompoundTag());
|
||||
storage.set(InputValidator.identifierOf(key), new_nbt.getCompoundTag());
|
||||
}
|
||||
return NBTSerializableValue.of(old_nbt);
|
||||
});
|
||||
|
||||
@@ -7,6 +7,7 @@ import carpet.script.Expression;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.exception.ThrowStatement;
|
||||
import carpet.script.exception.Throwables;
|
||||
import carpet.script.utils.InputValidator;
|
||||
import carpet.script.value.BooleanValue;
|
||||
import carpet.script.value.ListValue;
|
||||
import carpet.script.value.NBTSerializableValue;
|
||||
@@ -66,7 +67,7 @@ public class Inventories {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
TagManager tagManager = cc.s.getMinecraftServer().getTagManager();
|
||||
String tag = lv.get(0).getString();
|
||||
net.minecraft.tag.Tag<Item> itemTag = tagManager.getItems().getTag(new Identifier(tag));
|
||||
net.minecraft.tag.Tag<Item> itemTag = tagManager.getItems().getTag(InputValidator.identifierOf(tag));
|
||||
if (itemTag == null) return Value.NULL;
|
||||
return ListValue.wrap(itemTag.values().stream().map(b -> ValueConversions.of(Registry.ITEM.getId(b))).collect(Collectors.toList()));
|
||||
});
|
||||
@@ -81,7 +82,7 @@ public class Inventories {
|
||||
if (lv.size() == 1)
|
||||
return ListValue.wrap(tagManager.getItems().getTags().entrySet().stream().filter(e -> e.getValue().contains(item)).map(e -> ValueConversions.of(e.getKey())).collect(Collectors.toList()));
|
||||
String tag = lv.get(1).getString();
|
||||
net.minecraft.tag.Tag<Item> itemTag = tagManager.getItems().getTag(new Identifier(tag));
|
||||
net.minecraft.tag.Tag<Item> itemTag = tagManager.getItems().getTag(InputValidator.identifierOf(tag));
|
||||
if (itemTag == null) return Value.NULL;
|
||||
return BooleanValue.of(item.isIn(itemTag));
|
||||
});
|
||||
@@ -95,17 +96,10 @@ public class Inventories {
|
||||
if (lv.size() > 1)
|
||||
{
|
||||
String recipeType = lv.get(1).getString();
|
||||
try
|
||||
{
|
||||
type = Registry.RECIPE_TYPE.get(new Identifier(recipeType));
|
||||
}
|
||||
catch (InvalidIdentifierException ignored)
|
||||
{
|
||||
throw new InternalExpressionException("Unknown crafting category: " + recipeType);
|
||||
}
|
||||
type = Registry.RECIPE_TYPE.get(InputValidator.identifierOf(recipeType));
|
||||
}
|
||||
List<Recipe<?>> recipes;
|
||||
recipes = ((RecipeManagerInterface) cc.s.getMinecraftServer().getRecipeManager()).getAllMatching(type, new Identifier(recipeName));
|
||||
recipes = ((RecipeManagerInterface) cc.s.getMinecraftServer().getRecipeManager()).getAllMatching(type, InputValidator.identifierOf(recipeName));
|
||||
if (recipes.isEmpty())
|
||||
return Value.NULL;
|
||||
List<Value> recipesOutput = new ArrayList<>();
|
||||
@@ -175,7 +169,7 @@ public class Inventories {
|
||||
{
|
||||
String itemStr = v.getString();
|
||||
Item item;
|
||||
Identifier id = new Identifier(itemStr);
|
||||
Identifier id = InputValidator.identifierOf(itemStr);
|
||||
item = Registry.ITEM.getOrEmpty(id).orElseThrow(() -> new ThrowStatement(itemStr, Throwables.UNKNOWN_ITEM));
|
||||
if (!item.hasRecipeRemainder()) return Value.NULL;
|
||||
return new StringValue(NBTSerializableValue.nameFromRegistryId(Registry.ITEM.getId(item.getRecipeRemainder())));
|
||||
|
||||
@@ -9,6 +9,7 @@ import carpet.script.LazyValue;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.exception.ThrowStatement;
|
||||
import carpet.script.exception.Throwables;
|
||||
import carpet.script.utils.InputValidator;
|
||||
import carpet.script.value.BooleanValue;
|
||||
import carpet.script.value.EntityValue;
|
||||
import carpet.script.value.ListValue;
|
||||
@@ -407,14 +408,7 @@ public class Scoreboards {
|
||||
|
||||
String id = lv.get(0).getString();
|
||||
Identifier identifier;
|
||||
try
|
||||
{
|
||||
identifier = new Identifier(id);
|
||||
}
|
||||
catch (InvalidIdentifierException invalidIdentifierException)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
identifier = InputValidator.identifierOf(id);
|
||||
|
||||
if(lv.size() == 1)
|
||||
{
|
||||
|
||||
@@ -22,6 +22,7 @@ import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.exception.ThrowStatement;
|
||||
import carpet.script.exception.Throwables;
|
||||
import carpet.script.utils.BiomeInfo;
|
||||
import carpet.script.utils.InputValidator;
|
||||
import carpet.script.utils.WorldTools;
|
||||
import carpet.script.value.BlockValue;
|
||||
import carpet.script.value.BooleanValue;
|
||||
@@ -322,7 +323,7 @@ public class WorldAccess {
|
||||
String poiType = lv.get(locator.offset+1).getString().toLowerCase(Locale.ROOT);
|
||||
if (!"any".equals(poiType))
|
||||
{
|
||||
PointOfInterestType type = Registry.POINT_OF_INTEREST_TYPE.getOrEmpty(new Identifier(poiType))
|
||||
PointOfInterestType type = Registry.POINT_OF_INTEREST_TYPE.getOrEmpty(InputValidator.identifierOf(poiType))
|
||||
.orElseThrow(() -> new ThrowStatement(poiType, Throwables.UNKNOWN_POI));
|
||||
condition = (tt) -> tt == type;
|
||||
}
|
||||
@@ -372,7 +373,7 @@ public class WorldAccess {
|
||||
return Value.TRUE;
|
||||
}
|
||||
String poiTypeString = poi.getString().toLowerCase(Locale.ROOT);
|
||||
PointOfInterestType type = Registry.POINT_OF_INTEREST_TYPE.getOrEmpty(new Identifier(poiTypeString))
|
||||
PointOfInterestType type = Registry.POINT_OF_INTEREST_TYPE.getOrEmpty(InputValidator.identifierOf(poiTypeString))
|
||||
.orElseThrow(() -> new ThrowStatement(poiTypeString, Throwables.UNKNOWN_POI));
|
||||
int occupancy = 0;
|
||||
if (locator.offset + 1 < lv.size())
|
||||
@@ -827,7 +828,7 @@ public class WorldAccess {
|
||||
{
|
||||
playerBreak = true;
|
||||
String itemString = val.getString();
|
||||
item = Registry.ITEM.getOrEmpty(new Identifier(itemString))
|
||||
item = Registry.ITEM.getOrEmpty(InputValidator.identifierOf(itemString))
|
||||
.orElseThrow(() -> new ThrowStatement(itemString, Throwables.UNKNOWN_ITEM));
|
||||
}
|
||||
}
|
||||
@@ -1136,7 +1137,7 @@ public class WorldAccess {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
TagManager tagManager = cc.s.getMinecraftServer().getTagManager();
|
||||
String tag = lv.get(0).getString();
|
||||
net.minecraft.tag.Tag<Block> blockTag = tagManager.getBlocks().getTag(new Identifier(tag));
|
||||
net.minecraft.tag.Tag<Block> blockTag = tagManager.getBlocks().getTag(InputValidator.identifierOf(tag));
|
||||
if (blockTag == null) return Value.NULL;
|
||||
return ListValue.wrap(blockTag.values().stream().map(b -> ValueConversions.of(Registry.BLOCK.getId(b))).collect(Collectors.toList()));
|
||||
});
|
||||
@@ -1154,7 +1155,7 @@ public class WorldAccess {
|
||||
return ListValue.wrap(tagManager.getBlocks().getTags().entrySet().stream().filter(e -> e.getValue().contains(target)).map(e -> ValueConversions.of(e.getKey())).collect(Collectors.toList()));
|
||||
}
|
||||
String tag = lv.get(blockLocator.offset).getString();
|
||||
net.minecraft.tag.Tag<Block> blockTag = tagManager.getBlocks().getTag(new Identifier(tag));
|
||||
net.minecraft.tag.Tag<Block> blockTag = tagManager.getBlocks().getTag(InputValidator.identifierOf(tag));
|
||||
if (blockTag == null) return Value.NULL;
|
||||
return BooleanValue.of(blockLocator.block.getBlockState().isIn(blockTag));
|
||||
});
|
||||
@@ -1170,7 +1171,7 @@ public class WorldAccess {
|
||||
Biome biome;
|
||||
if (locator.replacement != null)
|
||||
{
|
||||
biome = world.getRegistryManager().get(Registry.BIOME_KEY).get(new Identifier(locator.replacement));
|
||||
biome = world.getRegistryManager().get(Registry.BIOME_KEY).get(InputValidator.identifierOf(locator.replacement));
|
||||
if (biome == null) throw new ThrowStatement(locator.replacement, Throwables.UNKNOWN_BIOME) ;
|
||||
}
|
||||
else
|
||||
@@ -1198,7 +1199,7 @@ public class WorldAccess {
|
||||
throw new InternalExpressionException("'set_biome' needs a biome name as an argument");
|
||||
String biomeName = lv.get(locator.offset+0).getString();
|
||||
// from locatebiome command code
|
||||
Biome biome = cc.s.getMinecraftServer().getRegistryManager().get(Registry.BIOME_KEY).getOrEmpty(new Identifier(biomeName))
|
||||
Biome biome = cc.s.getMinecraftServer().getRegistryManager().get(Registry.BIOME_KEY).getOrEmpty(InputValidator.identifierOf(biomeName))
|
||||
.orElseThrow(() -> new ThrowStatement(biomeName, Throwables.UNKNOWN_BIOME));
|
||||
boolean doImmediateUpdate = true;
|
||||
if (lv.size() > locator.offset+1)
|
||||
@@ -1235,7 +1236,7 @@ public class WorldAccess {
|
||||
String simpleStructureName = lv.get(locator.offset).getString().toLowerCase(Locale.ROOT);
|
||||
//CarpetSettings.LOG.error(FeatureGenerator.featureToStructure.keySet().stream().collect(Collectors.joining(",")));
|
||||
//CarpetSettings.LOG.error(FeatureGenerator.featureToStructure.values().stream().collect(Collectors.joining(",")));
|
||||
StructureFeature<?> structureName = Registry.STRUCTURE_FEATURE.get(new Identifier(simpleStructureName));
|
||||
StructureFeature<?> structureName = Registry.STRUCTURE_FEATURE.get(InputValidator.identifierOf(simpleStructureName));
|
||||
if (structureName == null) return Value.NULL;
|
||||
LongSet structureReferences = references.get(structureName);
|
||||
if (structureReferences == null || structureReferences.isEmpty()) return ListValue.of();
|
||||
@@ -1264,7 +1265,7 @@ public class WorldAccess {
|
||||
if (!(requested instanceof NullValue))
|
||||
{
|
||||
String reqString = requested.getString();
|
||||
structure = Registry.STRUCTURE_FEATURE.getOrEmpty(new Identifier(reqString))
|
||||
structure = Registry.STRUCTURE_FEATURE.getOrEmpty(InputValidator.identifierOf(reqString))
|
||||
.orElseThrow(() -> new ThrowStatement(reqString, Throwables.UNKNOWN_STRUCTURE));
|
||||
}
|
||||
if (lv.size() > locator.offset+1)
|
||||
@@ -1325,7 +1326,7 @@ public class WorldAccess {
|
||||
return MapValue.wrap(structureList);
|
||||
}
|
||||
String structureName = lv.get(locator.offset).getString().toLowerCase(Locale.ROOT);
|
||||
return ValueConversions.of(structures.get(Registry.STRUCTURE_FEATURE.get(new Identifier(structureName))));
|
||||
return ValueConversions.of(structures.get(Registry.STRUCTURE_FEATURE.get(InputValidator.identifierOf(structureName))));
|
||||
});
|
||||
|
||||
expression.addContextFunction("set_structure", -1, (c, t, lv) ->
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package carpet.script.utils;
|
||||
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.InvalidIdentifierException;
|
||||
import org.lwjgl.system.CallbackI;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -13,4 +16,16 @@ public class InputValidator {
|
||||
return simplified;
|
||||
}
|
||||
|
||||
public static Identifier identifierOf(String string)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Identifier(string);
|
||||
}
|
||||
catch (InvalidIdentifierException iie)
|
||||
{
|
||||
throw new InternalExpressionException("Incorrect identifier format '"+string+"': "+iie.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import carpet.script.CarpetScriptServer;
|
||||
import carpet.script.EntityEventsGroup;
|
||||
import carpet.script.argument.Vector3Argument;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.utils.InputValidator;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
@@ -249,7 +250,7 @@ public class EntityValue extends Value
|
||||
positive = false;
|
||||
who = who.substring(1);
|
||||
}
|
||||
net.minecraft.tag.Tag<EntityType<?>> eTag = server.getTagManager().getEntityTypes().getTag(new Identifier(who));
|
||||
net.minecraft.tag.Tag<EntityType<?>> eTag = server.getTagManager().getEntityTypes().getTag(InputValidator.identifierOf(who));
|
||||
if (eTag == null) throw new InternalExpressionException(who+" is not a valid entity descriptor");
|
||||
if (positive)
|
||||
{
|
||||
@@ -444,7 +445,7 @@ public class EntityValue extends Value
|
||||
|
||||
put("has_scoreboard_tag", (e, a) -> BooleanValue.of(e.getScoreboardTags().contains(a.getString())));
|
||||
put("has_entity_tag", (e, a) -> {
|
||||
net.minecraft.tag.Tag<EntityType<?>> tag = e.getServer().getTagManager().getEntityTypes().getTag(new Identifier(a.getString()));
|
||||
net.minecraft.tag.Tag<EntityType<?>> tag = e.getServer().getTagManager().getEntityTypes().getTag(InputValidator.identifierOf(a.getString()));
|
||||
if (tag == null) return Value.NULL;
|
||||
return BooleanValue.of(e.getType().isIn(tag));
|
||||
});
|
||||
@@ -609,7 +610,7 @@ public class EntityValue extends Value
|
||||
|
||||
put("brain", (e, a) -> {
|
||||
String module = a.getString();
|
||||
MemoryModuleType<?> moduleType = Registry.MEMORY_MODULE_TYPE.get(new Identifier(module));
|
||||
MemoryModuleType<?> moduleType = Registry.MEMORY_MODULE_TYPE.get(InputValidator.identifierOf(module));
|
||||
if (moduleType == MemoryModuleType.DUMMY) return Value.NULL;
|
||||
if (e instanceof LivingEntity)
|
||||
{
|
||||
@@ -704,7 +705,7 @@ public class EntityValue extends Value
|
||||
return ListValue.wrap(effects);
|
||||
}
|
||||
String effectName = a.getString();
|
||||
StatusEffect potion = Registry.STATUS_EFFECT.get(new Identifier(effectName));
|
||||
StatusEffect potion = Registry.STATUS_EFFECT.get(InputValidator.identifierOf(effectName));
|
||||
if (potion == null)
|
||||
throw new InternalExpressionException("No such an effect: "+effectName);
|
||||
if (!((LivingEntity) e).hasStatusEffect(potion))
|
||||
@@ -848,7 +849,7 @@ public class EntityValue extends Value
|
||||
AttributeContainer container = el.getAttributes();
|
||||
return MapValue.wrap(Registry.ATTRIBUTE.stream().filter(container::hasAttribute).collect(Collectors.toMap(aa -> ValueConversions.of(Registry.ATTRIBUTE.getId(aa)), aa -> NumericValue.of(container.getValue(aa)))));
|
||||
}
|
||||
Identifier id = new Identifier(a.getString());
|
||||
Identifier id = InputValidator.identifierOf(a.getString());
|
||||
EntityAttribute attrib = Registry.ATTRIBUTE.getOrEmpty(id).orElseThrow(
|
||||
() -> new InternalExpressionException("Unknown attribute: "+a.getString())
|
||||
);
|
||||
@@ -1360,7 +1361,7 @@ public class EntityValue extends Value
|
||||
if (lv.size() >= 1 && lv.size() <= 6)
|
||||
{
|
||||
String effectName = lv.get(0).getString();
|
||||
StatusEffect effect = Registry.STATUS_EFFECT.get(new Identifier(effectName));
|
||||
StatusEffect effect = Registry.STATUS_EFFECT.get(InputValidator.identifierOf(effectName));
|
||||
if (effect == null)
|
||||
throw new InternalExpressionException("Wrong effect name: "+effectName);
|
||||
if (lv.size() == 1)
|
||||
@@ -1393,7 +1394,7 @@ public class EntityValue extends Value
|
||||
else
|
||||
{
|
||||
String effectName = v.getString();
|
||||
StatusEffect effect = Registry.STATUS_EFFECT.get(new Identifier(effectName));
|
||||
StatusEffect effect = Registry.STATUS_EFFECT.get(InputValidator.identifierOf(effectName));
|
||||
if (effect == null)
|
||||
throw new InternalExpressionException("Wrong effect name: "+effectName);
|
||||
le.removeStatusEffect(effect);
|
||||
|
||||
Reference in new issue
Block a user