Add some jqwik arbitraries for Minecraft types

This requires supporting registries in our platform test
code. Thankfully this is mostly the same as what we can do in Fabric -
the duplication is unfortunate - but it's easy enough.
This commit is contained in:
Jonathan Coates
2023-08-23 10:05:56 +01:00
parent 5a7259e4c9
commit 5b2fdec6ca
2 changed files with 135 additions and 11 deletions
@@ -21,6 +21,7 @@ import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
@@ -50,6 +51,7 @@ import net.minecraft.world.phys.Vec3;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
@@ -68,30 +70,41 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
throw new UnsupportedOperationException("Cannot create config file inside tests");
}
@Override
public <T> RegistryWrappers.RegistryWrapper<T> wrap(ResourceKey<Registry<T>> registry) {
throw new UnsupportedOperationException("Cannot query registry inside tests");
}
@Override
public <T> RegistrationHelper<T> createRegistrationHelper(ResourceKey<Registry<T>> registry) {
throw new UnsupportedOperationException("Cannot query registry inside tests");
}
@Override
public <K> ResourceLocation getRegistryKey(ResourceKey<Registry<K>> registry, K object) {
throw new UnsupportedOperationException("Cannot query registry inside tests");
@SuppressWarnings("unchecked")
private static <T> Registry<T> getRegistry(ResourceKey<Registry<T>> id) {
var registry = (Registry<T>) BuiltInRegistries.REGISTRY.get(id.location());
if (registry == null) throw new IllegalArgumentException("Unknown registry " + id);
return registry;
}
@Override
public <K> K getRegistryObject(ResourceKey<Registry<K>> registry, ResourceLocation id) {
throw new UnsupportedOperationException("Cannot query registry inside tests");
public <T> ResourceLocation getRegistryKey(ResourceKey<Registry<T>> registry, T object) {
var key = getRegistry(registry).getKey(object);
if (key == null) throw new IllegalArgumentException(object + " was not registered in " + registry);
return key;
}
@Override
public <T> T getRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id) {
var value = getRegistry(registry).get(id);
if (value == null) throw new IllegalArgumentException(id + " was not registered in " + registry);
return value;
}
@Override
public <T> RegistryWrappers.RegistryWrapper<T> wrap(ResourceKey<Registry<T>> registry) {
return new RegistryWrapperImpl<>(registry.location(), getRegistry(registry));
}
@Nullable
@Override
public <T> T tryGetRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id) {
throw new UnsupportedOperationException("Cannot query registries");
return getRegistry(registry).get(id);
}
@Override
@@ -239,4 +252,48 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
public String getInstalledVersion() {
return "1.0";
}
private record RegistryWrapperImpl<T>(
ResourceLocation name, Registry<T> registry
) implements RegistryWrappers.RegistryWrapper<T> {
@Override
public int getId(T object) {
return registry.getId(object);
}
@Override
public ResourceLocation getKey(T object) {
var key = registry.getKey(object);
if (key == null) throw new IllegalArgumentException(object + " was not registered in " + name);
return key;
}
@Override
public T get(ResourceLocation location) {
var object = registry.get(location);
if (object == null) throw new IllegalArgumentException(location + " was not registered in " + name);
return object;
}
@Nullable
@Override
public T tryGet(ResourceLocation location) {
return registry.get(location);
}
@Override
public @Nullable T byId(int id) {
return registry.byId(id);
}
@Override
public int size() {
return registry.size();
}
@Override
public Iterator<T> iterator() {
return registry.iterator();
}
}
}