Skip to content

Commit

Permalink
完成 aquaculture 模组的兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Oct 9, 2024
1 parent 145556d commit 7823556
Show file tree
Hide file tree
Showing 14 changed files with 559 additions and 46 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ dependencies {

implementation fg.deobf("maven.modrinth:torohealth-damage-indicators-updated:1.20.1-forge-1")

runtimeOnly fg.deobf("curse.maven:aquaculture-60028:5585290")
implementation fg.deobf("curse.maven:aquaculture-60028:5585290")

annotationProcessor 'org.spongepowered:mixin:0.8.5:processor'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.tartaricacid.touhoulittlemaid;

import com.github.tartaricacid.touhoulittlemaid.api.ILittleMaid;
import com.github.tartaricacid.touhoulittlemaid.compat.aquaculture.AquacultureCompat;
import com.github.tartaricacid.touhoulittlemaid.config.GeneralConfig;
import com.github.tartaricacid.touhoulittlemaid.config.ServerConfig;
import com.github.tartaricacid.touhoulittlemaid.entity.chatbubble.ChatBubbleManger;
Expand Down Expand Up @@ -28,6 +29,8 @@ public TouhouLittleMaid() {
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, GeneralConfig.init());
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, ServerConfig.init());
ChatBubbleManger.initDefaultChat();

AquacultureCompat.init();
}

private static void initRegister(IEventBus eventBus) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.tartaricacid.touhoulittlemaid.api.entity.fishing;

import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.github.tartaricacid.touhoulittlemaid.entity.projectile.MaidFishingHook;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;

public interface IFishingType {
boolean isFishingRod(ItemStack itemStack);

MaidFishingHook getFishingHook(EntityMaid maid, Level level, ItemStack itemStack, Vec3 pos);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.joml.Matrix4f;

@OnlyIn(Dist.CLIENT)
public class MaidFishingHookRenderer extends EntityRenderer<MaidFishingHook> {
public class MaidFishingHookRenderer<T extends MaidFishingHook> extends EntityRenderer<T> {
private static final ResourceLocation TEXTURE_LOCATION = new ResourceLocation("textures/entity/fishing_hook.png");
private static final RenderType RENDER_TYPE = RenderType.entityCutout(TEXTURE_LOCATION);

Expand All @@ -28,19 +28,42 @@ public MaidFishingHookRenderer(EntityRendererProvider.Context context) {
}

@Override
public void render(MaidFishingHook fishingHook, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int packedLight) {
public void render(T fishingHook, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int packedLight) {
EntityMaid maid = fishingHook.getMaidOwner();
if (maid == null) {
return;
}
poseStack.pushPose();
this.renderFishFloat(poseStack, buffer, packedLight);
this.renderFishLine(fishingHook, partialTicks, poseStack, buffer, maid);
this.renderBobber(fishingHook, poseStack, buffer, packedLight);
this.renderFishingLine(fishingHook, partialTicks, poseStack, buffer, maid);
poseStack.popPose();
super.render(fishingHook, entityYaw, partialTicks, poseStack, buffer, packedLight);
}

private void renderFishLine(MaidFishingHook fishingHook, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, EntityMaid maid) {
protected void renderBobber(T fishingHook, PoseStack poseStack, MultiBufferSource buffer, int packedLight) {
poseStack.pushPose();
poseStack.scale(0.5F, 0.5F, 0.5F);
poseStack.mulPose(this.entityRenderDispatcher.cameraOrientation());
poseStack.mulPose(Axis.YP.rotationDegrees(180.0F));

PoseStack.Pose lasted = poseStack.last();
Matrix4f lastedPose = lasted.pose();
Matrix3f lastedNormal = lasted.normal();

VertexConsumer consumer = buffer.getBuffer(RENDER_TYPE);
vertex(consumer, lastedPose, lastedNormal, packedLight, 0.0F, 0, 0, 1);
vertex(consumer, lastedPose, lastedNormal, packedLight, 1.0F, 0, 1, 1);
vertex(consumer, lastedPose, lastedNormal, packedLight, 1.0F, 1, 1, 0);
vertex(consumer, lastedPose, lastedNormal, packedLight, 0.0F, 1, 0, 0);

poseStack.popPose();
}

protected float[] getLineColor(T fishingHook) {
return new float[]{0f, 0f, 0f};
}

protected void renderFishingLine(T fishingHook, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, EntityMaid maid) {
float lerpBodyRot;
if (maid.getVehicle() instanceof LivingEntity vehicle) {
lerpBodyRot = Mth.lerp(partialTicks, vehicle.yBodyRotO, vehicle.yBodyRot) * ((float) Math.PI / 180F);
Expand All @@ -62,37 +85,20 @@ private void renderFishLine(MaidFishingHook fishingHook, float partialTicks, Pos
float y = (float) (y1 - y2) - 0.1875F;
float z = (float) (z1 - z2);

float[] colors = getLineColor(fishingHook);

VertexConsumer lineConsumer = buffer.getBuffer(RenderType.lineStrip());
PoseStack.Pose lasted = poseStack.last();
for (int i = 0; i <= 16; ++i) {
stringVertex(x, y, z, lineConsumer, lasted, fraction(i), fraction(i + 1));
stringVertex(x, y, z, lineConsumer, lasted, fraction(i), fraction(i + 1), colors[0], colors[1], colors[2]);
}
}

private void renderFishFloat(PoseStack poseStack, MultiBufferSource buffer, int packedLight) {
poseStack.pushPose();
poseStack.scale(0.5F, 0.5F, 0.5F);
poseStack.mulPose(this.entityRenderDispatcher.cameraOrientation());
poseStack.mulPose(Axis.YP.rotationDegrees(180.0F));

PoseStack.Pose lasted = poseStack.last();
Matrix4f lastedPose = lasted.pose();
Matrix3f lastedNormal = lasted.normal();

VertexConsumer consumer = buffer.getBuffer(RENDER_TYPE);
vertex(consumer, lastedPose, lastedNormal, packedLight, 0.0F, 0, 0, 1);
vertex(consumer, lastedPose, lastedNormal, packedLight, 1.0F, 0, 1, 1);
vertex(consumer, lastedPose, lastedNormal, packedLight, 1.0F, 1, 1, 0);
vertex(consumer, lastedPose, lastedNormal, packedLight, 0.0F, 1, 0, 0);

poseStack.popPose();
}

private float fraction(int numerator) {
protected float fraction(int numerator) {
return (float) numerator / (float) 16;
}

private void vertex(VertexConsumer consumer, Matrix4f pose, Matrix3f normal, int lightMapUV, float pX, int pY, int pU, int pV) {
protected void vertex(VertexConsumer consumer, Matrix4f pose, Matrix3f normal, int lightMapUV, float pX, int pY, int pU, int pV) {
consumer.vertex(pose, pX - 0.5F, pY - 0.5F, 0.0F)
.color(255, 255, 255, 255)
.uv((float) pU, (float) pV)
Expand All @@ -102,7 +108,25 @@ private void vertex(VertexConsumer consumer, Matrix4f pose, Matrix3f normal, int
.endVertex();
}

private void stringVertex(float pX, float pY, float pZ, VertexConsumer consumer, PoseStack.Pose pose, float fraction1, float fraction2) {
protected static void renderPosTexture(VertexConsumer builder, Matrix4f matrix4f, Matrix3f matrix3f, int lightMapUV, float x, int y, int u, int v) {
builder.vertex(matrix4f, x - 0.5F, (float) y - 0.5F, 0.0F)
.color(255, 255, 255, 255)
.uv((float) u, (float) v)
.overlayCoords(OverlayTexture.NO_OVERLAY).uv2(lightMapUV)
.normal(matrix3f, 0.0F, 1.0F, 0.0F)
.endVertex();
}

protected static void vertex(VertexConsumer builder, Matrix4f matrix4f, Matrix3f matrix3f, int lightMapUV, float x, int y, int u, int v, float r, float g, float b) {
builder.vertex(matrix4f, x - 0.5F, (float) y - 0.5F, 0.0F)
.color(r, g, b, 1.0F)
.uv((float) u, (float) v)
.overlayCoords(OverlayTexture.NO_OVERLAY)
.uv2(lightMapUV).normal(matrix3f, 0.0F, 1.0F, 0.0F)
.endVertex();
}

protected void stringVertex(float pX, float pY, float pZ, VertexConsumer consumer, PoseStack.Pose pose, float fraction1, float fraction2, float r, float g, float b) {
float x = pX * fraction1;
float y = pY * (fraction1 * fraction1 + fraction1) * 0.5F + 0.25F;
float z = pZ * fraction1;
Expand All @@ -117,13 +141,13 @@ private void stringVertex(float pX, float pY, float pZ, VertexConsumer consumer,
nz /= sqrt;

consumer.vertex(pose.pose(), x, y, z)
.color(0, 0, 0, 255)
.color(r, g, b, 1.0F)
.normal(pose.normal(), nx, ny, nz)
.endVertex();
}

@Override
public ResourceLocation getTextureLocation(MaidFishingHook pEntity) {
public ResourceLocation getTextureLocation(T pEntity) {
return TEXTURE_LOCATION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.tartaricacid.touhoulittlemaid.compat.aquaculture;

import com.github.tartaricacid.touhoulittlemaid.compat.aquaculture.client.AquacultureClientRegister;
import com.github.tartaricacid.touhoulittlemaid.compat.aquaculture.entity.AquacultureFishingHook;
import com.github.tartaricacid.touhoulittlemaid.compat.aquaculture.entity.AquacultureFishingType;
import com.github.tartaricacid.touhoulittlemaid.entity.ai.fishing.FishingTypeManager;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.fml.loading.LoadingModList;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegisterEvent;

public class AquacultureCompat {
private static final String MOD_ID = "aquaculture";
private static boolean INSTALLED;

public static void init() {
INSTALLED = LoadingModList.get().getModFileById(MOD_ID) != null;
if (INSTALLED) {
registerAll();
}
}

public static void registerFishingType(FishingTypeManager manager) {
if (INSTALLED) {
manager.addFishingType(new AquacultureFishingType());
}
}

private static void registerAll() {
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
modEventBus.register(new AquacultureCompat());
if (FMLEnvironment.dist == Dist.CLIENT) {
modEventBus.register(new AquacultureClientRegister());
}
}

@SubscribeEvent
public void register(RegisterEvent event) {
event.register(ForgeRegistries.Keys.ENTITY_TYPES, helper -> helper.register("aquaculture_fishing_hook", AquacultureFishingHook.TYPE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.tartaricacid.touhoulittlemaid.compat.aquaculture.client;

import com.github.tartaricacid.touhoulittlemaid.compat.aquaculture.entity.AquacultureFishingHook;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

@OnlyIn(Dist.CLIENT)
public class AquacultureClientRegister {
@SubscribeEvent
public void onEntityRenderers(EntityRenderersEvent.RegisterRenderers event) {
event.registerEntityRenderer(AquacultureFishingHook.TYPE, AquacultureFishingHookRenderer::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.github.tartaricacid.touhoulittlemaid.compat.aquaculture.client;

import com.github.tartaricacid.touhoulittlemaid.client.renderer.entity.MaidFishingHookRenderer;
import com.github.tartaricacid.touhoulittlemaid.compat.aquaculture.entity.AquacultureFishingHook;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Axis;
import com.teammetallurgy.aquaculture.Aquaculture;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.DyeableLeatherItem;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.joml.Matrix3f;
import org.joml.Matrix4f;

import javax.annotation.Nonnull;

@OnlyIn(Dist.CLIENT)
public class AquacultureFishingHookRenderer extends MaidFishingHookRenderer<AquacultureFishingHook> {
private static final ResourceLocation BOBBER = new ResourceLocation(Aquaculture.MOD_ID, "textures/entity/rod/bobber/bobber.png");
private static final ResourceLocation BOBBER_OVERLAY = new ResourceLocation(Aquaculture.MOD_ID, "textures/entity/rod/bobber/bobber_overlay.png");
private static final ResourceLocation BOBBER_VANILLA = new ResourceLocation(Aquaculture.MOD_ID, "textures/entity/rod/bobber/bobber_vanilla.png");
private static final ResourceLocation HOOK = new ResourceLocation(Aquaculture.MOD_ID, "textures/entity/rod/hook/hook.png");
private static final RenderType BOBBER_RENDER = RenderType.entityCutout(BOBBER);
private static final RenderType BOBBER_OVERLAY_RENDER = RenderType.entityCutout(BOBBER_OVERLAY);
private static final RenderType BOBBER_VANILLA_RENDER = RenderType.entityCutout(BOBBER_VANILLA);
private static final RenderType HOOK_RENDER = RenderType.entityCutout(HOOK);

public AquacultureFishingHookRenderer(EntityRendererProvider.Context context) {
super(context);
}

@Override
protected void renderBobber(AquacultureFishingHook fishingHook, PoseStack poseStack, MultiBufferSource buffer, int packedLight) {
poseStack.pushPose();
poseStack.scale(0.5F, 0.5F, 0.5F);
poseStack.mulPose(this.entityRenderDispatcher.cameraOrientation());
poseStack.mulPose(Axis.YP.rotationDegrees(180.0F));

PoseStack.Pose lasted = poseStack.last();
Matrix4f lastedPose = lasted.pose();
Matrix3f lastedNormal = lasted.normal();

VertexConsumer consumer = fishingHook.hasBobber() ? buffer.getBuffer(BOBBER_OVERLAY_RENDER) : buffer.getBuffer(BOBBER_VANILLA_RENDER);

// Bobber Overlay
ItemStack bobberStack = fishingHook.getBobber();
float bobberR = 1.0F;
float bobberG = 1.0F;
float bobberB = 1.0F;
if (!bobberStack.isEmpty()) {
if (bobberStack.getItem() instanceof DyeableLeatherItem) {
int colorInt = ((DyeableLeatherItem) bobberStack.getItem()).getColor(bobberStack);
bobberR = (float) (colorInt >> 16 & 255) / 255.0F;
bobberG = (float) (colorInt >> 8 & 255) / 255.0F;
bobberB = (float) (colorInt & 255) / 255.0F;
}
}
vertex(consumer, lastedPose, lastedNormal, packedLight, 0.0F, 0, 0, 1, bobberR, bobberG, bobberB);
vertex(consumer, lastedPose, lastedNormal, packedLight, 1.0F, 0, 1, 1, bobberR, bobberG, bobberB);
vertex(consumer, lastedPose, lastedNormal, packedLight, 1.0F, 1, 1, 0, bobberR, bobberG, bobberB);
vertex(consumer, lastedPose, lastedNormal, packedLight, 0.0F, 1, 0, 0, bobberR, bobberG, bobberB);

// Bobber Background
if (fishingHook.hasBobber()) {
VertexConsumer bobberVertex = buffer.getBuffer(BOBBER_RENDER);
renderPosTexture(bobberVertex, lastedPose, lastedNormal, packedLight, 0.0F, 0, 0, 1);
renderPosTexture(bobberVertex, lastedPose, lastedNormal, packedLight, 1.0F, 0, 1, 1);
renderPosTexture(bobberVertex, lastedPose, lastedNormal, packedLight, 1.0F, 1, 1, 0);
renderPosTexture(bobberVertex, lastedPose, lastedNormal, packedLight, 0.0F, 1, 0, 0);
}

// Hook
RenderType renderType = RenderType.entityCutout(fishingHook.getHook().getTexture());
VertexConsumer hookVertex = fishingHook.hasHook() ? buffer.getBuffer(renderType) : buffer.getBuffer(HOOK_RENDER);
renderPosTexture(hookVertex, lastedPose, lastedNormal, packedLight, 0.0F, 0, 0, 1);
renderPosTexture(hookVertex, lastedPose, lastedNormal, packedLight, 1.0F, 0, 1, 1);
renderPosTexture(hookVertex, lastedPose, lastedNormal, packedLight, 1.0F, 1, 1, 0);
renderPosTexture(hookVertex, lastedPose, lastedNormal, packedLight, 0.0F, 1, 0, 0);

poseStack.popPose();
}

@Override
protected float[] getLineColor(AquacultureFishingHook fishingHook) {
// Line color
ItemStack line = fishingHook.getFishingLine();
float r = 0;
float g = 0;
float b = 0;
if (!line.isEmpty()) {
DyeableLeatherItem lineItem = (DyeableLeatherItem) line.getItem();
if (lineItem.hasCustomColor(line)) {
int colorInt = lineItem.getColor(line);
r = (float) (colorInt >> 16 & 255) / 255.0F;
g = (float) (colorInt >> 8 & 255) / 255.0F;
b = (float) (colorInt & 255) / 255.0F;
}
}
return new float[]{r, g, b};
}

@Override
@Nonnull
public ResourceLocation getTextureLocation(@Nonnull AquacultureFishingHook fishHook) {
return BOBBER_VANILLA;
}
}
Loading

0 comments on commit 7823556

Please sign in to comment.