From 05c11a381e972c35f74f7ee3df506772114db94d Mon Sep 17 00:00:00 2001 From: Eli Orona Date: Tue, 2 Aug 2022 18:58:38 -0700 Subject: [PATCH] Spear/Trident API go brrr --- library/item/item_extension/build.gradle | 1 + .../ProjectileModifyingTridentItem.java | 10 ++++++ .../api/trident/TridentExtensions.java | 13 ++++++++ .../TridentThrownProjectileEvents.java | 4 +++ .../trident/TridentClientModInitializer.java | 24 ++++++++++++++ .../mixin/trident/TridentEntityAccessor.java | 16 +++++++++ .../mixin/trident/TridentEntityMixin.java | 33 +++++++++++++++++++ .../client/ClientPlayNetworkHandlerMixin.java | 24 ++++++++++++++ .../client/TridentEntityRendererMixin.java | 22 +++++++++++++ .../quilt_item_extension.mixins.json | 8 +++-- 10 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/ProjectileModifyingTridentItem.java create mode 100644 library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/TridentExtensions.java create mode 100644 library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/TridentThrownProjectileEvents.java create mode 100644 library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/impl/trident/TridentClientModInitializer.java create mode 100644 library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/TridentEntityAccessor.java create mode 100644 library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/TridentEntityMixin.java create mode 100644 library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/client/ClientPlayNetworkHandlerMixin.java create mode 100644 library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/client/TridentEntityRendererMixin.java diff --git a/library/item/item_extension/build.gradle b/library/item/item_extension/build.gradle index 778fc30d15..c1e3e11d35 100644 --- a/library/item/item_extension/build.gradle +++ b/library/item/item_extension/build.gradle @@ -11,6 +11,7 @@ qslModule { moduleDependencies { core { api("qsl_base") + impl("networking") } } injectedInterface("net/minecraft/class_1753") { diff --git a/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/ProjectileModifyingTridentItem.java b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/ProjectileModifyingTridentItem.java new file mode 100644 index 0000000000..ffe55354a1 --- /dev/null +++ b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/ProjectileModifyingTridentItem.java @@ -0,0 +1,10 @@ +package org.quiltmc.qsl.item.extension.api.trident; + +import net.minecraft.item.Item; +import net.minecraft.item.TridentItem; + +public class ProjectileModifyingTridentItem extends TridentItem { + public ProjectileModifyingTridentItem(Item.Settings settings) { + super(settings); + } +} diff --git a/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/TridentExtensions.java b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/TridentExtensions.java new file mode 100644 index 0000000000..0da1b72815 --- /dev/null +++ b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/TridentExtensions.java @@ -0,0 +1,13 @@ +package org.quiltmc.qsl.item.extension.api.trident; + +import org.quiltmc.qsl.base.api.util.InjectedInterface; + +import net.minecraft.item.TridentItem; +import net.minecraft.util.Identifier; + +@InjectedInterface(TridentItem.class) +public interface TridentExtensions { + boolean useVanillaRenderer(); + Identifier getRenderTexture(); + +} diff --git a/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/TridentThrownProjectileEvents.java b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/TridentThrownProjectileEvents.java new file mode 100644 index 0000000000..7e17045c4a --- /dev/null +++ b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/api/trident/TridentThrownProjectileEvents.java @@ -0,0 +1,4 @@ +package org.quiltmc.qsl.item.extension.api.trident; + +public class TridentThrownProjectileEvents { +} diff --git a/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/impl/trident/TridentClientModInitializer.java b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/impl/trident/TridentClientModInitializer.java new file mode 100644 index 0000000000..85c25fa225 --- /dev/null +++ b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/impl/trident/TridentClientModInitializer.java @@ -0,0 +1,24 @@ +package org.quiltmc.qsl.item.extension.impl.trident; + +import java.util.ArrayDeque; +import java.util.Deque; + +import org.quiltmc.loader.api.ModContainer; +import org.quiltmc.qsl.base.api.entrypoint.client.ClientModInitializer; +import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking; + +import net.minecraft.item.ItemStack; +import net.minecraft.util.Identifier; + +public class TridentClientModInitializer implements ClientModInitializer { + public static final Deque TRIDENT_QUEUE = new ArrayDeque<>(); + + public static final Identifier TRIDENT_SPAWN_PACKET_ID = new Identifier("quilt_item_extensions", "trident_spawn_stack"); + + @Override + public void onInitializeClient(ModContainer mod) { + ClientPlayNetworking.registerGlobalReceiver(TRIDENT_SPAWN_PACKET_ID, (client, handler, buf, responseSender) -> { + TRIDENT_QUEUE.add(buf.readItemStack()); + }); + } +} diff --git a/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/TridentEntityAccessor.java b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/TridentEntityAccessor.java new file mode 100644 index 0000000000..73db10f8bb --- /dev/null +++ b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/TridentEntityAccessor.java @@ -0,0 +1,16 @@ +package org.quiltmc.qsl.item.extension.mixin.trident; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import net.minecraft.entity.projectile.TridentEntity; +import net.minecraft.item.ItemStack; + +@Mixin(TridentEntity.class) +public interface TridentEntityAccessor { + @Accessor("tridentStack") + ItemStack getTridentStack(); + + @Accessor("tridentStack") + void setTridentStack(ItemStack stack); +} diff --git a/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/TridentEntityMixin.java b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/TridentEntityMixin.java new file mode 100644 index 0000000000..4db1f23db8 --- /dev/null +++ b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/TridentEntityMixin.java @@ -0,0 +1,33 @@ +package org.quiltmc.qsl.item.extension.mixin.trident; + +import org.quiltmc.qsl.item.extension.impl.trident.TridentClientModInitializer; +import org.quiltmc.qsl.networking.api.PacketByteBufs; +import org.quiltmc.qsl.networking.api.ServerPlayNetworking; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.projectile.ProjectileEntity; +import net.minecraft.entity.projectile.TridentEntity; +import net.minecraft.network.Packet; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.world.World; + +@Mixin(ProjectileEntity.class) +public abstract class TridentEntityMixin extends Entity { + public TridentEntityMixin(EntityType entityType, World world) { + super(entityType, world); + } + + @Inject(method = "createSpawnPacket", at = @At("HEAD")) + public void sendTridentStackBeforeSpawnPacket(CallbackInfoReturnable> cir) { + if ((Object) this instanceof TridentEntity trident) { + PacketByteBuf passedData = PacketByteBufs.create(); + passedData.writeItemStack(((TridentEntityAccessor) trident).getTridentStack()); + ServerPlayNetworking.send(this.world.getServer().getPlayerManager().getPlayerList(), TridentClientModInitializer.TRIDENT_SPAWN_PACKET_ID, passedData); + } + } +} diff --git a/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/client/ClientPlayNetworkHandlerMixin.java b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/client/ClientPlayNetworkHandlerMixin.java new file mode 100644 index 0000000000..493bceaad2 --- /dev/null +++ b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/client/ClientPlayNetworkHandlerMixin.java @@ -0,0 +1,24 @@ +package org.quiltmc.qsl.item.extension.mixin.trident.client; + +import org.quiltmc.qsl.item.extension.impl.trident.TridentClientModInitializer; +import org.quiltmc.qsl.item.extension.mixin.trident.TridentEntityAccessor; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket; + +@Mixin(ClientPlayNetworkHandler.class) +public class ClientPlayNetworkHandlerMixin { + @Inject(method = "onEntitySpawn", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;onSpawnPacket(Lnet/minecraft/network/packet/s2c/play/EntitySpawnS2CPacket;)V"), locals = LocalCapture.CAPTURE_FAILHARD) + public void onEntitySpawnMixin(EntitySpawnS2CPacket packet, CallbackInfo ci, EntityType entityType, Entity entity) { + if (entityType == EntityType.TRIDENT) { + ((TridentEntityAccessor) entity).setTridentStack(TridentClientModInitializer.TRIDENT_QUEUE.remove()); + } + } +} diff --git a/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/client/TridentEntityRendererMixin.java b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/client/TridentEntityRendererMixin.java new file mode 100644 index 0000000000..59bdc22964 --- /dev/null +++ b/library/item/item_extension/src/main/java/org/quiltmc/qsl/item/extension/mixin/trident/client/TridentEntityRendererMixin.java @@ -0,0 +1,22 @@ +package org.quiltmc.qsl.item.extension.mixin.trident.client; + +import org.quiltmc.qsl.item.extension.api.trident.TridentExtensions; +import org.quiltmc.qsl.item.extension.mixin.trident.TridentEntityAccessor; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.client.render.entity.TridentEntityRenderer; +import net.minecraft.entity.projectile.TridentEntity; +import net.minecraft.util.Identifier; + +@Mixin(TridentEntityRenderer.class) +public class TridentEntityRendererMixin { + @Inject(method = "getTexture(Lnet/minecraft/entity/projectile/TridentEntity;)Lnet/minecraft/util/Identifier;", at = @At(value = "HEAD"), cancellable = true) + public void getTextureMixin(TridentEntity entity, CallbackInfoReturnable cir) { + if(((TridentEntityAccessor) entity).getTridentStack().getItem() instanceof TridentExtensions tridentItem) { + cir.setReturnValue(tridentItem.getRenderTexture()); + } + } +} diff --git a/library/item/item_extension/src/main/resources/quilt_item_extension.mixins.json b/library/item/item_extension/src/main/resources/quilt_item_extension.mixins.json index e40f82a717..bc83734a1f 100644 --- a/library/item/item_extension/src/main/resources/quilt_item_extension.mixins.json +++ b/library/item/item_extension/src/main/resources/quilt_item_extension.mixins.json @@ -11,12 +11,16 @@ "bow.EntityUseBowMixin", "crossbow.CrossbowItemAccessor", "crossbow.CrossbowItemMixin", - "crossbow.EntityUseCrossbowMixin" + "crossbow.EntityUseCrossbowMixin", + "trident.TridentEntityAccessor", + "trident.TridentEntityMixin" ], "client": [ "bow.client.SkeletonEntityModelMixin", "client.HeldItemRendererMixin", - "crossbow.client.PlayerEntityRendererMixin" + "crossbow.client.PlayerEntityRendererMixin", + "trident.client.ClientPlayNetworkHandlerMixin", + "trident.client.TridentEntityRendererMixin" ], "injectors": { "defaultRequire": 1