autogen door item renderer

This commit is contained in:
CreepyCre 2021-04-16 20:49:41 +02:00
parent 66d2150b4a
commit 11dbe0d582
5 changed files with 139 additions and 10 deletions

View file

@ -27,7 +27,7 @@ import java.util.ArrayList;
import java.util.function.BiFunction;
public class DimensionalDoorBlockRegistrar {
private static final String PREFIX = "autogen_dimensional_";
private static final String PREFIX = "block_ag_dim_";
private final Registry<Block> registry;
private final DimensionalDoorItemRegistrar itemRegistrar;

View file

@ -8,16 +8,24 @@ import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.util.Identifier;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.block.door.DimensionalDoorBlockRegistrar;
import org.dimdev.dimdoors.item.DimensionalDoorItemRegistrar;
import org.jetbrains.annotations.Nullable;
public class DimensionalDoorModelVariantProvider implements ModelVariantProvider {
private static final Identifier childItem = new Identifier("dimdoors:item/child_item");
@Override
public @Nullable UnbakedModel loadModelVariant(ModelIdentifier modelId, ModelProviderContext context) throws ModelProviderException {
Identifier identifier = new Identifier(modelId.getNamespace(), modelId.getPath());
DimensionalDoorBlockRegistrar registrar = DimensionalDoorsInitializer.getDimensionalDoorBlockRegistrar();
if (!registrar.isMapped(identifier)) return null;
Identifier mapped = registrar.get(identifier);
ModelIdentifier newId = new ModelIdentifier(mapped, modelId.getVariant());
return context.loadModel(newId);
DimensionalDoorBlockRegistrar blockRegistrar = DimensionalDoorsInitializer.getDimensionalDoorBlockRegistrar();
if (blockRegistrar.isMapped(identifier)) {
Identifier mapped = blockRegistrar.get(identifier);
ModelIdentifier newId = new ModelIdentifier(mapped, modelId.getVariant());
return context.loadModel(newId);
} else if (identifier.getPath().startsWith(DimensionalDoorItemRegistrar.PREFIX)) {
return context.loadModel(childItem);
}
return null;
}
}

View file

@ -0,0 +1,51 @@
package org.dimdev.dimdoors.client;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.item.ItemRenderer;
import net.minecraft.client.render.model.json.ModelTransformation;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import org.dimdev.dimdoors.item.DimensionalDoorItemRegistrar;
public class UnderlaidChildItemRenderer implements BuiltinItemRendererRegistry.DynamicItemRenderer {
private final ItemStack underlay;
public UnderlaidChildItemRenderer(Item underlay) {
this.underlay = new ItemStack(underlay);
}
public UnderlaidChildItemRenderer(ItemStack underlay) {
this.underlay = underlay;
}
@Override
public void render(ItemStack stack, ModelTransformation.Mode mode, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
if (!(stack.getItem() instanceof DimensionalDoorItemRegistrar.ChildItem)) throw new UnsupportedOperationException("Can only use UnderlaidChildItemRenderer for ChildItems");
DimensionalDoorItemRegistrar.ChildItem childItem = (DimensionalDoorItemRegistrar.ChildItem) stack.getItem();
matrices.push();
matrices.translate(0.5D, 0.5D, 0.5D);
ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
matrices.push();
matrices.scale(1, 1, 0.5f);
itemRenderer.renderItem(underlay, ModelTransformation.Mode.NONE, light, overlay, matrices, vertexConsumers, 0);
matrices.pop();
ItemStack originalItemStack = new ItemStack(
childItem.getOriginalItem(),
stack.getCount());
originalItemStack.setTag(stack.getTag());
matrices.push();
childItem.transform(matrices);
itemRenderer.renderItem(originalItemStack, ModelTransformation.Mode.NONE, light, overlay, matrices, vertexConsumers, 0);
matrices.pop();
matrices.pop();
}
}

View file

@ -1,17 +1,22 @@
package org.dimdev.dimdoors.item;
import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;
import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Block;
import net.minecraft.block.DoorBlock;
import net.minecraft.block.TrapdoorBlock;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.*;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;
import net.minecraft.util.math.Quaternion;
import net.minecraft.util.math.Vec3f;
import net.minecraft.util.registry.Registry;
import org.apache.commons.lang3.tuple.ImmutableTriple;
@ -22,6 +27,7 @@ import org.dimdev.dimdoors.block.door.DimensionalDoorBlock;
import org.dimdev.dimdoors.block.door.DimensionalTrapdoorBlock;
import org.dimdev.dimdoors.block.door.data.DoorData;
import org.dimdev.dimdoors.block.entity.EntranceRiftBlockEntity;
import org.dimdev.dimdoors.client.UnderlaidChildItemRenderer;
import org.dimdev.dimdoors.listener.ItemRegistryEntryAddedListener;
import org.dimdev.dimdoors.rift.targets.PublicPocketTarget;
@ -32,7 +38,8 @@ import java.util.function.Consumer;
import java.util.function.Function;
public class DimensionalDoorItemRegistrar {
private static final String PREFIX = "autogen_dimensional_";
public static final String PREFIX = "item_ag_dim_";
private static final UnderlaidChildItemRenderer renderer = new UnderlaidChildItemRenderer(Items.ENDER_PEARL);
private final Registry<Item> registry;
@ -110,9 +117,12 @@ public class DimensionalDoorItemRegistrar {
Registry.register(registry, gennedId, dimItem);
}
placementFunctions.put(original, dimItem::place);
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
BuiltinItemRendererRegistry.INSTANCE.register(dimItem, renderer);
}
}
private static class AutoGenDimensionalDoorItem extends DimensionalDoorItem {
private static class AutoGenDimensionalDoorItem extends DimensionalDoorItem implements ChildItem {
private final Item originalItem;
public AutoGenDimensionalDoorItem(Block block, Settings settings, Consumer<? super EntranceRiftBlockEntity> setupFunction, Item originalItem) {
@ -129,9 +139,20 @@ public class DimensionalDoorItemRegistrar {
public Text getName() {
return new TranslatableText("dimdoors.autogen_item_prefix", I18n.translate(originalItem.getTranslationKey()));
}
@Override
public Item getOriginalItem() {
return originalItem;
}
@Override
public void transform(MatrixStack matrices) {
matrices.scale(0.68f, 0.68f, 1);
matrices.translate(0.05, 0.02, 0);
}
}
private static class AutoGenDimensionalTrapdoorItem extends DimensionalTrapdoorItem {
private static class AutoGenDimensionalTrapdoorItem extends DimensionalTrapdoorItem implements ChildItem {
private final Item originalItem;
public AutoGenDimensionalTrapdoorItem(Block block, Settings settings, Consumer<? super EntranceRiftBlockEntity> setupFunction, Item originalItem) {
@ -148,5 +169,24 @@ public class DimensionalDoorItemRegistrar {
public Text getName() {
return new TranslatableText("dimdoors.autogen_item_prefix", I18n.translate(originalItem.getTranslationKey()));
}
@Override
public Item getOriginalItem() {
return originalItem;
}
@Override
public void transform(MatrixStack matrices) {
matrices.scale(0.55f, 0.55f, 0.6f);
matrices.translate(0.05, -0.05, 0.41);
matrices.multiply(Quaternion.method_35823(new Vec3f(90, 0, 0)));
}
}
public interface ChildItem {
Item getOriginalItem();
default void transform(MatrixStack matrices) {
}
}
}

View file

@ -0,0 +1,30 @@
{
"parent": "minecraft:builtin/entity",
"gui_light": "front",
"display": {
"ground": {
"rotation": [ 0, 0, 0 ],
"translation": [ 0, 2, 0],
"scale":[ 0.5, 0.5, 0.5 ]
},
"head": {
"rotation": [ 0, 180, 0 ],
"translation": [ 0, 13, 7],
"scale":[ 1, 1, 1]
},
"thirdperson_righthand": {
"rotation": [ 0, 0, 0 ],
"translation": [ 0, 3, 1 ],
"scale": [ 0.55, 0.55, 0.55 ]
},
"firstperson_righthand": {
"rotation": [ 0, -90, 25 ],
"translation": [ 1.13, 3.2, 1.13],
"scale": [ 0.68, 0.68, 0.68 ]
},
"fixed": {
"rotation": [ 0, 180, 0 ],
"scale": [ 1, 1, 1 ]
}
}
}