Testmod for many blocks/items test

This commit is contained in:
embeddedt 2023-08-09 21:45:21 -04:00
parent fed0392ddc
commit 365eb80a28
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
9 changed files with 243 additions and 1 deletions

View File

@ -1,5 +1,9 @@
apply plugin: "dev.architectury.loom"
loom {
accessWidenerPath = project(":common").loom.accessWidenerPath
}
dependencies {
minecraft "com.mojang:minecraft:${rootProject.minecraft_version}"
mappings loom.layered() {
@ -11,6 +15,9 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
modImplementation(fabricApi.module("fabric-resource-loader-v0", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
modImplementation(fabricApi.module("fabric-models-v0", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
modImplementation(fabricApi.module("fabric-renderer-api-v1", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
modRuntimeOnly(fabricApi.module("fabric-renderer-indigo", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
implementation project(path: ":common", configuration: "namedElements")
implementation project(path: ":fabric", configuration: "namedElements")

View File

@ -0,0 +1,13 @@
package org.embeddedt.modernfix.testmod;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
public class TestBlock extends Block {
private static final BlockBehaviour.Properties PROPERTIES = BlockBehaviour.Properties.copy(Blocks.STONE);
public TestBlock() {
super(PROPERTIES);
}
}

View File

@ -0,0 +1,13 @@
package org.embeddedt.modernfix.testmod;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
public class TestBlockItem extends BlockItem {
private static final Item.Properties PROPERTIES = new Item.Properties().tab(CreativeModeTab.TAB_BUILDING_BLOCKS);
public TestBlockItem(TestBlock block) {
super(block, PROPERTIES);
}
}

View File

@ -1,10 +1,40 @@
package org.embeddedt.modernfix.testmod;
import com.google.common.base.Stopwatch;
import net.fabricmc.api.ModInitializer;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class TestMod implements ModInitializer {
public static final String ID = "mfix_testmod";
public static final Logger LOGGER = LogManager.getLogger("ModernFix TestMod");
public static final int NUM_COLORS = 32;
public static final int MAX_COLOR = NUM_COLORS - 1;
@Override
public void onInitialize() {
System.out.println("Hello ModernFix world!");
// Register 1 million blocks & items
Stopwatch watch = Stopwatch.createStarted();
int totalToRegister = NUM_COLORS * NUM_COLORS * NUM_COLORS;
int progressReport = totalToRegister / 20;
int numRegistered = 0;
for(int r = 0; r < NUM_COLORS; r++) {
for(int g = 0; g < NUM_COLORS; g++) {
for(int b = 0; b < NUM_COLORS; b++) {
ResourceLocation name = new ResourceLocation(ID, "wool_" + r + "_" + g + "_" + b);
TestBlock block = Registry.register(Registry.BLOCK, name, new TestBlock());
Registry.register(Registry.ITEM, name, new TestBlockItem(block));
numRegistered++;
if((numRegistered % progressReport) == 0) {
LOGGER.info(String.format("Registering... %.02f%%", ((float)numRegistered)/totalToRegister * 100));
}
}
}
}
watch.stop();
LOGGER.info("Registered {} registry entries in {}", totalToRegister, watch);
}
}

View File

@ -0,0 +1,142 @@
package org.embeddedt.modernfix.testmod.client;
import com.google.common.collect.ImmutableList;
import com.mojang.datafixers.util.Pair;
import net.fabricmc.fabric.api.renderer.v1.Renderer;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.fabricmc.fabric.api.renderer.v1.mesh.Mesh;
import net.fabricmc.fabric.api.renderer.v1.mesh.MeshBuilder;
import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView;
import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.*;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;
import org.embeddedt.modernfix.testmod.TestMod;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
public class TestModBlockModel implements UnbakedModel, BakedModel, FabricBakedModel {
private static final Material BASE_WOOL = new Material(TextureAtlas.LOCATION_BLOCKS, new ResourceLocation(TestMod.ID, "block/base_wool"));
private Mesh mesh;
private TextureAtlasSprite texture;
private final int r, g, b;
public TestModBlockModel(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
@Override
public boolean isVanillaAdapter() {
return false;
}
@Override
public void emitBlockQuads(BlockAndTintGetter blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) {
context.meshConsumer().accept(mesh);
}
@Override
public void emitItemQuads(ItemStack stack, Supplier<Random> randomSupplier, RenderContext context) {
context.meshConsumer().accept(mesh);
}
@Override
public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) {
return Collections.emptyList();
}
@Override
public boolean useAmbientOcclusion() {
return true;
}
@Override
public boolean isGui3d() {
return true;
}
@Override
public boolean usesBlockLight() {
return true;
}
@Override
public boolean isCustomRenderer() {
return false;
}
@Override
public TextureAtlasSprite getParticleIcon() {
return texture;
}
@Override
public ItemTransforms getTransforms() {
return ModelHelper.MODEL_TRANSFORM_BLOCK;
}
@Override
public ItemOverrides getOverrides() {
return ItemOverrides.EMPTY;
}
@Override
public Collection<ResourceLocation> getDependencies() {
return Collections.emptyList();
}
@Override
public Collection<Material> getMaterials(Function<ResourceLocation, UnbakedModel> modelGetter, Set<Pair<String, String>> missingTextureErrors) {
return ImmutableList.of(BASE_WOOL);
}
private static int scaleColor(int c) {
return c * 255 / TestMod.MAX_COLOR;
}
@Nullable
@Override
public BakedModel bake(ModelBakery modelBakery, Function<Material, TextureAtlasSprite> spriteGetter, ModelState transform, ResourceLocation location) {
// Build the mesh using the Renderer API
Renderer renderer = RendererAccess.INSTANCE.getRenderer();
MeshBuilder builder = renderer.meshBuilder();
QuadEmitter emitter = builder.getEmitter();
texture = spriteGetter.apply(BASE_WOOL);
for(Direction direction : Direction.values()) {
// Add a new face to the mesh
emitter.square(direction, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);
// Set the sprite of the face, must be called after .square()
// We haven't specified any UV coordinates, so we want to use the whole texture. BAKE_LOCK_UV does exactly that.
emitter.spriteBake(0, texture, MutableQuadView.BAKE_LOCK_UV);
int color = (255 << 24) | (scaleColor(r) << 16) | (scaleColor(g) << 8) | scaleColor(b);
// Enable texture usage
emitter.spriteColor(0, color, color, color, color);
// Add the quad to the mesh
emitter.emit();
}
mesh = builder.build();
return this;
}
}

View File

@ -0,0 +1,27 @@
package org.embeddedt.modernfix.testmod.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry;
import org.embeddedt.modernfix.testmod.TestMod;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestModClient implements ClientModInitializer {
private static final Pattern RGB_PATTERN = Pattern.compile("^wool_([0-9]+)_([0-9]+)_([0-9]+)$");
@Override
public void onInitializeClient() {
ModelLoadingRegistry.INSTANCE.registerVariantProvider(resourceManager -> (modelId, context) -> {
if(modelId.getNamespace().equals(TestMod.ID)) {
Matcher matcher = RGB_PATTERN.matcher(modelId.getPath());
if(matcher.matches()) {
int r = Integer.parseInt(matcher.group(1));
int g = Integer.parseInt(matcher.group(2));
int b = Integer.parseInt(matcher.group(3));
return new TestModBlockModel(r, g, b);
}
}
return null;
});
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 560 B

View File

@ -17,6 +17,9 @@
"entrypoints": {
"main": [
"org.embeddedt.modernfix.testmod.TestMod"
],
"client": [
"org.embeddedt.modernfix.testmod.client.TestModClient"
]
}
}

View File

@ -0,0 +1,7 @@
{
"pack": {
"description": "testmod resources",
"pack_format": 6,
"_comment": "A pack_format of 6 requires json lang files and some texture changes from 1.16.2. Note: we require v6 pack meta for all mods."
}
}