Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Split Shulkers] Consider using Mixin inheritance for improved mod compatibility #3

Open
RubixDev opened this issue Dec 7, 2023 · 1 comment

Comments

@RubixDev
Copy link

RubixDev commented Dec 7, 2023

Hi, I'm the author of EnchantedShulkers, and I got a request to make my mod compatible with yours RubixDev/EnchantedShulkers#21. While working on that, I learned about Mixin Inheritance, which is preferred over just overriding entire vanilla methods in mixins. I am mainly talking about the getUpdatePacket() and getUpdateTag() methods in the MixinShulkerBoxBlockEntity class:

@Override
public ClientboundBlockEntityDataPacket getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
public @NotNull CompoundTag getUpdateTag() {
var tag = super.getUpdateTag();
var color = this.getColor();
var color2 = this.splitshulkers_secondaryColor;
if (color != color2) {
secondaryColorToTag(color2, tag);
}
return tag;
}
}

You can see how I "override" the same two methods on my side here and here (note: I'm using yarn mappings so the names are different).

While a change on your side technically isn't necessary, it would still be good practice to do so and a step forward for better compatibility between your mod and other mods like Shulker+.


Additionally you should consider relying less on ci.cancel() calls. Mainly

@Inject(method = "renderByItem", cancellable = true, at = @At(value = "INVOKE", shift = At.Shift.BEFORE, target = "Lnet/minecraft/world/level/block/ShulkerBoxBlock;getColorFromItem(Lnet/minecraft/world/item/Item;)Lnet/minecraft/world/item/DyeColor;"))
private void onRenderByItem(ItemStack stack, ItemDisplayContext displayContext, PoseStack poseStack, MultiBufferSource bufferSource, int i, int j, CallbackInfo ci) {
var color1 = ShulkerBoxBlock.getColorFromItem(stack.getItem());
var tag = BlockItem.getBlockEntityData(stack);
var color2 = secondaryColorFromTag(tag, color1);
var index = 17 * (color1 == null ? 0 : color1.getId()+1) + (color2 == null ? 0 : color2.getId()+1);
this.blockEntityRenderDispatcher.renderItem(splitshulkers_AllShulkerBoxes[index], poseStack, bufferSource, i, j);
ci.cancel();
}
and
@Inject(
method = "render(Lnet/minecraft/world/level/block/entity/ShulkerBoxBlockEntity;FLcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;II)V",
at = @At(value = "INVOKE", shift = At.Shift.BEFORE, target = "Lnet/minecraft/client/resources/model/Material;buffer(Lnet/minecraft/client/renderer/MultiBufferSource;Ljava/util/function/Function;)Lcom/mojang/blaze3d/vertex/VertexConsumer;"),
cancellable = true,
locals = LocalCapture.CAPTURE_FAILHARD
)
private void onRender(ShulkerBoxBlockEntity shulkerBox, float f, PoseStack poseStack, MultiBufferSource multiBufferSource, int i, int j, CallbackInfo ci, Direction direction, Material material) {
// if (false) return; // TODO check for non-split shulkers
ci.cancel();
var color2 = ((SplitShulkerBoxBlockEntity) shulkerBox).splitshulkers_getSecondaryColor();
var model = (ShulkerModelGetter) this.model;
// Render base with new color
Material material2 = color2 == null ? Sheets.DEFAULT_SHULKER_TEXTURE_LOCATION : Sheets.SHULKER_TEXTURE_LOCATION.get(color2.ordinal());
VertexConsumer vertexConsumer2 = material2.buffer(multiBufferSource, RenderType::entityCutoutNoCull);
model.getBase().render(poseStack, vertexConsumer2, i, j, 1.0f, 1.0f, 1.0f, 1.0f);
// Render lid normally
VertexConsumer vertexConsumer = material.buffer(multiBufferSource, RenderType::entityCutoutNoCull);
model.getLid().render(poseStack, vertexConsumer, i, j, 1.0f, 1.0f, 1.0f, 1.0f);
poseStack.popPose();
}
caused some trouble. For ensuring compatibility between our mods I had to mixin into your mixins in these places to basically just apply the same modifications that I do to the vanilla game.

@CursedFlames
Copy link
Owner

I've rewritten the mixins to hopefully be less invasive in 1b7ee9a; let me know if there's anything else that could be changed to improve compat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants