Skip to content

Commit

Permalink
untested, but should work?
Browse files Browse the repository at this point in the history
  • Loading branch information
AnAwesomGuy committed Jun 22, 2024
1 parent 1e180b0 commit 3b0fe84
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public final class CarnivalFoodsClient implements ClientModInitializer {
private static final Map<Item, Identifier> HELD_ITEMS_PRIVATE = new WeakHashMap<>();
public static final Map<Item, Identifier> HELD_ITEM_MODELS = Collections.unmodifiableMap(HELD_ITEMS_PRIVATE);
public static final Identifier COTTON_CANDY_HELD = CarnivalFoods.id("item/cotton_candy_held");

public static void addHeldItemModel(Item item, Identifier modelIdentifier) {
HELD_ITEMS_PRIVATE.put(Objects.requireNonNull(item), modelIdentifier);
Expand All @@ -36,13 +37,13 @@ public void onInitializeClient() {
ColorProviderRegistry.ITEM.register((stack, tintIndex) -> {
DyedColorComponent color;
if (tintIndex == 1 && (color = stack.get(DataComponentTypes.DYED_COLOR)) != null)
return ColorHelper.Argb.withAlpha(99, color.rgb());
return ColorHelper.Argb.fullAlpha(color.rgb());
return -1;
}, CarnivalFoods.COTTON_CANDY);
ModelLoadingPlugin.register(pluginContext -> pluginContext.addModels(HELD_ITEMS_PRIVATE.values()));
}

static {
addHeldItemModel(CarnivalFoods.COTTON_CANDY, CarnivalFoods.id("item/cotton_candy_held"));
addHeldItemModel(CarnivalFoods.COTTON_CANDY, COTTON_CANDY_HELD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactory;
import net.minecraft.client.render.entity.model.EntityModelLayer;
import net.minecraft.client.render.entity.model.EntityModelPartNames;
import net.minecraft.client.texture.SpriteAtlasTexture;
import net.minecraft.client.util.SpriteIdentifier;
import net.minecraft.client.util.math.MatrixStack;
Expand All @@ -31,12 +32,12 @@ public class CottonCandyMachineRenderer implements BlockEntityRenderer<CottonCan
@SuppressWarnings("deprecation") // BLOCK_ATLAS_TEXTURE
public static final SpriteIdentifier TEXTURE = new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, CarnivalFoods.id("entity/cotton_candy_machine"));

private final ModelPart base;
private final ModelPart bone;
private final ModelPart spinning;

public CottonCandyMachineRenderer(BlockEntityRendererFactory.Context ctx) {
ModelPart part = ctx.getLayerModelPart(LAYER_LOCATION);
this.base = part.getChild("base");
this.bone = part.getChild("bone");
this.spinning = part.getChild("spinning");
}

Expand All @@ -51,36 +52,37 @@ public static TexturedModelData createBodyLayer() {
inner2 = create().cuboid(-4.2F, 0.1F, -1.35F, 1F, 5F, 3F, dilation2).mirrored(), // northeast, southeast
inner3 = create().cuboid(3.2F, 0.1F, -1.65F, 1F, 5F, 3F, dilation2); // northwest, southwest

ModelPartData base = data.addChild("base",
ModelPartData bone = data.addChild(EntityModelPartNames.BONE, create(), ModelTransform.NONE);
bone.addChild("bone",
create().uv(-14, 14).cuboid(-7F, 1F, -7F, 14F, 1F, 14F, new Dilation(0.01F))
.uv(19, 4).cuboid(-6F, 0F, -6F, 2F, 2F, 2F, dilation1)
.uv(19, 4).cuboid(-6F, 0F, 4F, 2F, 2F, 2F, dilation1)
.uv(19, 4).cuboid(4F, 0F, 4F, 2F, 2F, 2F, dilation1)
.uv(19, 4).cuboid(4F, 0F, -6F, 2F, 2F, 2F, dilation1),
ModelTransform.NONE);
base.addChild("outerN", outer, ModelTransform.rotation(PI / 180 * 7.8F, 0F, 0F));
base.addChild("outerS", create().uv(0, 8).cuboid(-7F, 2F, 5.8F, 14F, 5F, 1F), ModelTransform.rotation(-PI / 180 * 7.8F, 0F, 0F));
base.addChild("outerE", outer.mirrored(), ModelTransform.rotation(PI / 180 * 7.8F, HALF_PI, 0F));
base.addChild("outerW", outer, ModelTransform.rotation(PI / 180 * 7.8F, -HALF_PI, 0F));
base.addChild("innerE", inner1, ModelTransform.rotation(-PI / 1.125F, -PI / 36F * 11F, PI));
base.addChild("innerW", inner1, ModelTransform.rotation(PI / 9, PI / 36F * 11F, 0F));
base.addChild("innerN", inner1.mirrored(), ModelTransform.rotation(-PI / 1.125F, PI / 36F * 7F, PI));
base.addChild("innerS", inner1, ModelTransform.rotation(PI / 9, -PI / 36F * 7F, 0F));
base.addChild("innerNE", inner2, ModelTransform.rotation(-1.0601F, -1.2728F, 1.0791F));
base.addChild("innerSE", inner2, ModelTransform.rotation(0.0375F, 0.1391F, 0.2644F));
base.addChild("innerNW", inner3, ModelTransform.rotation(-0.0364F, 0.1348F, -0.2643F));
base.addChild("innerSW", inner3, ModelTransform.rotation(1.0601F, -1.2728F, -1.0791F));
ModelTransform.pivot(8, 0, 8));
bone.addChild("outerN", outer, ModelTransform.rotation(PI / 180 * 7.8F, 0F, 0F));
bone.addChild("outerS", create().uv(0, 8).cuboid(-7F, 2F, 5.8F, 14F, 5F, 1F), ModelTransform.rotation(-PI / 180 * 7.8F, 0F, 0F));
bone.addChild("outerE", outer.mirrored(), ModelTransform.rotation(PI / 180 * 7.8F, HALF_PI, 0F));
bone.addChild("outerW", outer, ModelTransform.rotation(PI / 180 * 7.8F, -HALF_PI, 0F));
bone.addChild("innerE", inner1, ModelTransform.rotation(-PI / 1.125F, -PI / 36F * 11F, PI));
bone.addChild("innerW", inner1, ModelTransform.rotation(PI / 9, PI / 36F * 11F, 0F));
bone.addChild("innerN", inner1.mirrored(), ModelTransform.rotation(-PI / 1.125F, PI / 36F * 7F, PI));
bone.addChild("innerS", inner1, ModelTransform.rotation(PI / 9, -PI / 36F * 7F, 0F));
bone.addChild("innerNE", inner2, ModelTransform.rotation(-1.0601F, -1.2728F, 1.0791F));
bone.addChild("innerSE", inner2, ModelTransform.rotation(0.0375F, 0.1391F, 0.2644F));
bone.addChild("innerNW", inner3, ModelTransform.rotation(-0.0364F, 0.1348F, -0.2643F));
bone.addChild("innerSW", inner3, ModelTransform.rotation(1.0601F, -1.2728F, -1.0791F));

data.addChild("spinning", create().uv(8, 0).cuboid(-1F, 1.3F, -0.5F, 2F, 4F, 1F), ModelTransform.rotation(0F, -PI / 7.9F, 0F));
data.addChild("spinning", create().uv(8, 0).cuboid(7F, 1.3F, 7.5F, 2F, 4F, 1F), ModelTransform.rotation(0F, -PI / 7.9F, 0F));

return TexturedModelData.of(modelData, 32, 32);
}

@Override
public void render(CottonCandyMachineBlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
spinning.yaw = Math.clamp(spinning.yaw + tickDelta, 0, PI);
spinning.yaw = (spinning.yaw + tickDelta) % PI;
VertexConsumer consumer = TEXTURE.getVertexConsumer(vertexConsumers, RenderLayer::getEntityCutout);
base.render(matrices, consumer, light, overlay);
bone.render(matrices, consumer, light, overlay);
spinning.render(matrices, consumer, light, overlay);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.anawesomguy.carnivalfoods.mixin;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.render.model.json.ModelElement;
import net.minecraft.util.Identifier;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.lang.reflect.Type;
import java.util.List;
import java.util.function.Function;

@Mixin(JsonUnbakedModel.class)
public abstract class JsonUnbakedModelMixin implements UnbakedModel {
@Shadow @Final
private List<ModelElement> elements;
@Unique
boolean inheritElements;

@SuppressWarnings({"LocalMayBeArgsOnly", "DataFlowIssue"}) // what? (neither warning makes any sense)
@Inject(
method = "setParents",
at = @At(value = "JUMP", opcode = Opcodes.GOTO, ordinal = 0),
slice = @Slice(from = @At(value = "FIELD", target = "Lnet/minecraft/client/render/model/json/JsonUnbakedModel;parent:Lnet/minecraft/client/render/model/json/JsonUnbakedModel;"))
) // targets right before the for loop loops over, after `parent` is set
private void addInheritedElements(Function<Identifier, UnbakedModel> modelLoader, CallbackInfo ci, @Local JsonUnbakedModel jsonModel, @Local UnbakedModel unbakedModel) {
JsonUnbakedModelMixin jsonModelMixin = (JsonUnbakedModelMixin)(Object)jsonModel;
if (jsonModelMixin.inheritElements && !jsonModelMixin.elements.isEmpty())
jsonModelMixin.elements.addAll(((JsonUnbakedModel)unbakedModel).getElements());
}

@Mixin(JsonUnbakedModel.Deserializer.class)
public static abstract class DeserializerMixin implements JsonDeserializer<JsonUnbakedModel> {
@SuppressWarnings("ReferenceToMixin") // it's in a mixin
@Inject(method = "deserialize(Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/minecraft/client/render/model/json/JsonUnbakedModel;", at = @At("RETURN"))
private void setInheritElements(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext, CallbackInfoReturnable<JsonUnbakedModel> cir) {
((JsonUnbakedModelMixin)(Object)cir.getReturnValue()).inheritElements =
((JsonObject)jsonElement).getAsJsonPrimitive("inherit_elements").getAsBoolean();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,11 @@
"translation": [0, 1, 3.2],
"scale": [0.8, 0.8, 0.8]
},
"thirdperson_lefthand": {
"translation": [0, 1, 3.2],
"scale": [0.8, 0.8, 0.8]
},
"firstperson_righthand": {
"rotation": [9, 0, 0],
"translation": [-0.5, 0, 0],
"scale": [0.8, 0.8, 0.8]
},
"firstperson_lefthand": {
"rotation": [9, 0, 0],
"translation": [-0.5, 0, 0],
"scale": [0.8, 0.8, 0.8]
},
"ground": {
"translation": [0, -1, 0]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"credit": "Made with Blockbench",
"parent": "carnival-foods:item/cotton_candy_held",
"inherit_elements": true,
"elements": [
{
"from": [2.5, 12, -1.5],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"credit": "Made with Blockbench",
"parent": "carnival-foods:item/cotton_candy_held",
"inherit_elements": true,
"elements": [
{
"from": [3.5, 12, -0.5],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"credit": "Made with Blockbench",
"parent": "carnival-foods:item/cotton_candy_held",
"inherit_elements": true,
"elements": [
{
"from": [4.5, 12.5, 0.5],
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/carnival-foods.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"defaultRequire": 1
},
"client": [
"ItemRendererMixin"
"ItemRendererMixin",
"JsonUnbakedModelMixin",
"JsonUnbakedModelMixin$DeserializerMixin"
]
}

0 comments on commit 3b0fe84

Please sign in to comment.