跳到主要内容

142D和3D模型

添加物品

ModItems中:

    public static final Item RUBY_STAFF = registerItem("ruby_staff",
new Item(new Item.Settings().maxCount(1)));

mixin包中

ExampleMixin

@Mixin(MinecraftServer.class)
public class ExampleMixin {
@Inject(at = @At("HEAD"), method = "loadWorld")
private void init(CallbackInfo info) {
// This code is injected into the start of MinecraftServer.loadWorld()V
}
}

ItemRendererAccessor

@Mixin(ItemRenderer.class)
public interface ItemRendererAccessor {
@Accessor("models")
ItemModels mccourse$getModels();
}

ItemRendererMixin

@Mixin(ItemRenderer.class)
public abstract class ItemRendererMixin {
@ModifyVariable(method = "renderItem", at = @At(value = "HEAD"), argsOnly = true)
public BakedModel useRubyStaffModel(BakedModel value, ItemStack stack, ModelTransformationMode renderMode, boolean leftHanded, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
if (stack.isOf(ModItems.RUBY_STAFF) && renderMode != ModelTransformationMode.GUI) {
return ((ItemRendererAccessor) this).mccourse$getModels().getModelManager().getModel(new ModelIdentifier(DemoMod1.MOD_ID, "ruby_staff_3d", "inventory"));
}
return value;
}
}

ModelLoaderMixin

@Mixin(ModelLoader.class)
public abstract class ModelLoaderMixin {
@Shadow
protected abstract void addModel(ModelIdentifier modelId);

@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/model/ModelLoader;addModel(Lnet/minecraft/client/util/ModelIdentifier;)V", ordinal = 3, shift = At.Shift.AFTER))
public void addRubyStaff(BlockColors blockColors, Profiler profiler, Map<Identifier, JsonUnbakedModel> jsonUnbakedModels, Map<Identifier, List<ModelLoader.SourceTrackedData>> blockStates, CallbackInfo ci) {
this.addModel(new ModelIdentifier(DemoMod1.MOD_ID, "ruby_staff_3d", "inventory"));
}
}

重点是添加3D模型文件json,在resources/assets/tutorialmod/models/item/ruby_staff_3d.json中进行添加

效果

物品栏为2D,手持为3D

image-20240824161916301