Skip to content

Commit

Permalink
让渲染手臂的监听器支持返回值
Browse files Browse the repository at this point in the history
  • Loading branch information
CSneko committed Oct 1, 2024
1 parent 9275f10 commit 7058d44
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import net.minecraft.world.item.ItemStack;
import org.ayamemc.ayame.client.event.RenderCustomArmEventHandler;
import org.ayamemc.ayame.fabric.client.event.AyameKeyMappingEventHandler;
import org.ayamemc.ayame.fabric.client.event.custom.RenderArmCallback;
import org.ayamemc.ayame.fabric.client.api.event.RenderArmCallback;
import org.ayamemc.ayame.util.TaskManager;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,30 @@
* along with Ayame. If not, see <https://www.gnu.org/licenses/>.
*/

package org.ayamemc.ayame.fabric.client.event.custom;
package org.ayamemc.ayame.fabric.client.api.event;

import com.mojang.blaze3d.vertex.PoseStack;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

/**
* 在以第一人称渲染玩家手臂之前触发。为代替NeoForge提供的 {@code RenderArmEvent} 而生(因为Fabric Api没给对应的事件),可用于替换玩家手臂的渲染
* <p>例如在手臂上渲染盔甲或直接将手臂替换为盔甲。
*/
@Environment(EnvType.CLIENT)
public interface RenderArmCallback {
Event<RenderArmCallback> EVENT = EventFactory.createArrayBacked(RenderArmCallback.class,
public class RenderArmCallback {
public static final Event<OnRenderArm> ON_RENDER_ARM = EventFactory.createArrayBacked(OnRenderArm.class,
(listeners) -> (hand, poseStack, multiBufferSource, packedLight, partialTick, interpolatedPitch, swingProgress, equipProgress, stack) -> {
for (RenderArmCallback event : listeners) {
event.onRenderArm(
for (OnRenderArm listener : listeners) {
InteractionResult result = listener.onRenderArm(
hand,
poseStack,
multiBufferSource,
Expand All @@ -49,31 +52,39 @@ public interface RenderArmCallback {
equipProgress,
stack
);
if (result != InteractionResult.PASS) return result;
}
return InteractionResult.PASS;
});

/**
* 渲染玩家手之前调用
*
* @param hand 正在渲染的手
* @param poseStack 用于渲染的姿势堆栈
* @param multiBufferSource 渲染缓冲区的来源
* @param packedLight 用于渲染的压缩(天空和方块)光量
* @param partialTick Partial Tick
* @param interpolatedPitch 玩家实体的插值音高
* @param swingProgress 正在渲染的手牌的挥动进度
* @param equipProgress 装备动画的进度,从 { 0.0} 到 { 1.0}
* @param stack 要渲染的物品组
*/
void onRenderArm(
InteractionHand hand,
PoseStack poseStack,
MultiBufferSource multiBufferSource,
int packedLight,
float partialTick,
float interpolatedPitch,
float swingProgress,
float equipProgress,
ItemStack stack
);

public interface OnRenderArm {

/**
* 渲染玩家手之前调用
*
* @param hand 正在渲染的手
* @param poseStack 用于渲染的姿势堆栈
* @param multiBufferSource 渲染缓冲区的来源
* @param packedLight 用于渲染的压缩(天空和方块)光量
* @param partialTick Partial Tick
* @param interpolatedPitch 玩家实体的插值音高
* @param swingProgress 正在渲染的手牌的挥动进度
* @param equipProgress 装备动画的进度,从 { 0.0} 到 { 1.0}
* @param stack 要渲染的物品组
* @param player 玩家实体
*/
InteractionResult onRenderArm(
InteractionHand hand,
PoseStack poseStack,
MultiBufferSource multiBufferSource,
int packedLight,
float partialTick,
float interpolatedPitch,
float swingProgress,
float equipProgress,
ItemStack stack,
LocalPlayer player
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.ItemStack;
import org.ayamemc.ayame.fabric.client.event.custom.RenderArmCallback;
import org.ayamemc.ayame.fabric.client.api.event.RenderArmCallback;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand Down Expand Up @@ -68,9 +69,8 @@ private void renderArmWithItem(float partialTicks, PoseStack poseStack, MultiBuf
if (handsToRender == null) {
return;
}

if (handsToRender.renderMainHand) {
RenderArmCallback.EVENT.invoker().onRenderArm(
InteractionResult result = RenderArmCallback.ON_RENDER_ARM.invoker().onRenderArm(
InteractionHand.MAIN_HAND,
poseStack,
buffer,
Expand All @@ -79,12 +79,17 @@ private void renderArmWithItem(float partialTicks, PoseStack poseStack, MultiBuf
pitch,
interactionHand == InteractionHand.MAIN_HAND ? f : 0.0F,
1.0F - Mth.lerp(partialTicks, this.oMainHandHeight, this.mainHandHeight),
this.mainHandItem
this.mainHandItem,
playerEntity
);
if (result != InteractionResult.PASS) {
ci.cancel();
return;
}
}

if (handsToRender.renderOffHand) {
RenderArmCallback.EVENT.invoker().onRenderArm(
InteractionResult result = RenderArmCallback.ON_RENDER_ARM.invoker().onRenderArm(
InteractionHand.OFF_HAND,
poseStack,
buffer,
Expand All @@ -93,13 +98,18 @@ private void renderArmWithItem(float partialTicks, PoseStack poseStack, MultiBuf
pitch,
interactionHand == InteractionHand.OFF_HAND ? f : 0.0F,
1.0F - Mth.lerp(partialTicks, this.oOffHandHeight, this.offHandHeight),
this.offHandItem
this.offHandItem,
playerEntity
);
if (result != InteractionResult.PASS) {
ci.cancel();
return;
}
}
}

@Inject(method = "renderArmWithItem", at = @At("HEAD"), cancellable = true)
private void CancelRenderArmWithItem(AbstractClientPlayer player, float partialTicks, float pitch, InteractionHand hand, float swingProgress, ItemStack stack, float equippedProgress, PoseStack poseStack, MultiBufferSource buffer, int combinedLight, CallbackInfo ci) {
ci.cancel(); // 取消渲染默认手臂,与Neo那边一个性质
}
// @Inject(method = "renderArmWithItem", at = @At("HEAD"), cancellable = true)
// private void CancelRenderArmWithItem(AbstractClientPlayer player, float partialTicks, float pitch, InteractionHand hand, float swingProgress, ItemStack stack, float equippedProgress, PoseStack poseStack, MultiBufferSource buffer, int combinedLight, CallbackInfo ci) {
// ci.cancel(); // 取消渲染默认手臂,与Neo那边一个性质
// }
}

0 comments on commit 7058d44

Please sign in to comment.