From 0f49aed707ed4eb971b842adbda8029842b59938 Mon Sep 17 00:00:00 2001 From: ictye Date: Fri, 19 Apr 2024 21:22:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E9=AD=94=E5=8A=9B?= =?UTF-8?q?=E5=80=BC=E6=98=BE=E7=A4=BA=E4=B8=A6=E4=BF=AE=E5=BE=A9=E5=B9=BE?= =?UTF-8?q?=E5=80=8BBUG=EF=BC=8C=E4=BC=98=E5=8C=96=E7=8E=A9=E5=AE=B6?= =?UTF-8?q?=E9=AD=94=E5=8A=9B=E7=AE=A1=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../89b86ab0e66f527166d98df92ddbcf5416ed58f6 | 2 +- .../dc1d6e7286e7569a79007c10f809d49635ea1c49 | 2 +- .../foundation/mixin/ClientPlayerMixin.java | 23 +++++++++++ .../foundation/mixin/PlayerEntityMixin.java | 23 ++++++++--- .../foundation/mixin/SeriverEntityMixin.java | 39 ++++++++++++++++++ .../player/MagicAbilitiesManager.java | 14 +++++++ .../infrastructure/GUI/MagicLevelHud.java | 13 +++--- .../infrastructure/netWork/NetWorkReg.java | 12 ++++++ .../netWork/NetworkIDFinder.java | 11 +++++ .../netWork/SyncHUDS2CPacket.java | 20 +++++++++ .../the_origin_of_magic.java | 5 ++- .../the_origin_of_magic_client.java | 3 ++ .../utils/PlayerEntityMixinInterfaces.java | 2 +- .../textures/gui/magic_power_image.png | Bin 395 -> 310 bytes .../resources/the_origin_of_magic.mixins.json | 1 + 15 files changed, 156 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/ClientPlayerMixin.java create mode 100644 src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/SeriverEntityMixin.java create mode 100644 src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/NetWorkReg.java create mode 100644 src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/NetworkIDFinder.java create mode 100644 src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/SyncHUDS2CPacket.java diff --git a/src/main/generated/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 b/src/main/generated/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 index f6e63a9..63f9c83 100644 --- a/src/main/generated/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 +++ b/src/main/generated/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 @@ -1,2 +1,2 @@ -// 1.19.2 2024-04-19T10:01:41.7140308 Language +// 1.19.2 2024-04-19T19:43:43.0995063 Language 84f246a88b9c3dc4c5d0c98a56c4de5529eabe74 assets\the_origin_of_magic\lang\en_us.json diff --git a/src/main/generated/.cache/dc1d6e7286e7569a79007c10f809d49635ea1c49 b/src/main/generated/.cache/dc1d6e7286e7569a79007c10f809d49635ea1c49 index cde3b98..b55e8de 100644 --- a/src/main/generated/.cache/dc1d6e7286e7569a79007c10f809d49635ea1c49 +++ b/src/main/generated/.cache/dc1d6e7286e7569a79007c10f809d49635ea1c49 @@ -1,2 +1,2 @@ -// 1.19.2 2024-04-19T10:01:41.7110246 Block Loot Tables +// 1.19.2 2024-04-19T19:43:43.0964229 Block Loot Tables fe57f13449a11436bbede8c37e01bd0d1cd87191 data\the_origin_of_magic\loot_tables\blocks\magic_workbench.json diff --git a/src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/ClientPlayerMixin.java b/src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/ClientPlayerMixin.java new file mode 100644 index 0000000..68c509e --- /dev/null +++ b/src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/ClientPlayerMixin.java @@ -0,0 +1,23 @@ +package com.ictye.the_origin_of_magic.foundation.mixin; + +import com.ictye.the_origin_of_magic.foundation.player.MagicAbilitiesManager; +import com.ictye.the_origin_of_magic.utils.PlayerEntityMixinInterfaces; +import com.mojang.authlib.GameProfile; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.network.encryption.PlayerPublicKey; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Mixin; + +@Mixin(ClientPlayerEntity.class) +public abstract class ClientPlayerMixin extends PlayerEntity { + public ClientPlayerMixin(World world, BlockPos pos, float yaw, GameProfile gameProfile, @Nullable PlayerPublicKey publicKey) { + super(world, pos, yaw, gameProfile, publicKey); + } + + private final MagicAbilitiesManager magicAbilitiesManager = ((PlayerEntityMixinInterfaces)this).the_origin_of_magic$getMagicAbilitiesManager(); + + +} diff --git a/src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/PlayerEntityMixin.java b/src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/PlayerEntityMixin.java index 3f3af3f..337257e 100644 --- a/src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/PlayerEntityMixin.java +++ b/src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/PlayerEntityMixin.java @@ -2,31 +2,44 @@ import com.ictye.the_origin_of_magic.foundation.player.MagicAbilitiesManager; +import com.ictye.the_origin_of_magic.infrastructure.netWork.NetworkIDFinder; import com.ictye.the_origin_of_magic.utils.PlayerEntityMixinInterfaces; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; -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.callback.CallbackInfo; @Mixin(PlayerEntity.class) -public abstract class PlayerEntityMixin implements PlayerEntityMixinInterfaces { +public abstract class PlayerEntityMixin extends LivingEntity implements PlayerEntityMixinInterfaces { - @Unique - protected MagicAbilitiesManager magicAbilitiesManager = new MagicAbilitiesManager(); + protected PlayerEntityMixin(EntityType entityType, World world) { + super(entityType, world); + } + private final MagicAbilitiesManager magicAbilitiesManager = new MagicAbilitiesManager(); + @Override public MagicAbilitiesManager the_origin_of_magic$getMagicAbilitiesManager() { return this.magicAbilitiesManager; } - @Inject(method = "tick",at = @At("HEAD")) + @Inject(method = "tick()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/HungerManager;update(Lnet/minecraft/entity/player/PlayerEntity;)V", shift = At.Shift.AFTER)) @SuppressWarnings("ConstantValue") public void tick(CallbackInfo ci){ if((Object)this instanceof ServerPlayerEntity player){ + PacketByteBuf buffer = PacketByteBufs.create(); + buffer.writeFloat(this.magicAbilitiesManager.getMagicLevel()); + ServerPlayNetworking.send(player, NetworkIDFinder.SYNC_HUD_ID, buffer); + this.magicAbilitiesManager.update(player); } } diff --git a/src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/SeriverEntityMixin.java b/src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/SeriverEntityMixin.java new file mode 100644 index 0000000..e29db30 --- /dev/null +++ b/src/main/java/com/ictye/the_origin_of_magic/foundation/mixin/SeriverEntityMixin.java @@ -0,0 +1,39 @@ +package com.ictye.the_origin_of_magic.foundation.mixin; + +import com.ictye.the_origin_of_magic.foundation.player.MagicAbilitiesManager; +import com.ictye.the_origin_of_magic.infrastructure.netWork.NetworkIDFinder; +import com.ictye.the_origin_of_magic.utils.PlayerEntityMixinInterfaces; +import com.mojang.authlib.GameProfile; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.encryption.PlayerPublicKey; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.jetbrains.annotations.Nullable; +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; + +@Mixin(ServerPlayerEntity.class) +public abstract class SeriverEntityMixin extends PlayerEntity { + public SeriverEntityMixin(World world, BlockPos pos, float yaw, GameProfile gameProfile, @Nullable PlayerPublicKey publicKey) { + super(world, pos, yaw, gameProfile, publicKey); + } + + private final MagicAbilitiesManager magicAbilitiesManager = ((PlayerEntityMixinInterfaces)this).the_origin_of_magic$getMagicAbilitiesManager(); + + @Inject(method = "playerTick", at = @At(value = "HEAD")) + public void playerTick(CallbackInfo ci) { + // ServerPlayerEntity + ServerPlayerEntity serverPlayerEntity = (ServerPlayerEntity) (Object) this; + + PacketByteBuf buffer = PacketByteBufs.create(); + buffer.writeFloat(this.magicAbilitiesManager.getMagicLevel()); + ServerPlayNetworking.send(serverPlayerEntity, NetworkIDFinder.SYNC_HUD_ID, buffer); + } + +} diff --git a/src/main/java/com/ictye/the_origin_of_magic/foundation/player/MagicAbilitiesManager.java b/src/main/java/com/ictye/the_origin_of_magic/foundation/player/MagicAbilitiesManager.java index 3a092ed..3e9171e 100644 --- a/src/main/java/com/ictye/the_origin_of_magic/foundation/player/MagicAbilitiesManager.java +++ b/src/main/java/com/ictye/the_origin_of_magic/foundation/player/MagicAbilitiesManager.java @@ -1,9 +1,14 @@ package com.ictye.the_origin_of_magic.foundation.player; import com.ictye.the_origin_of_magic.foundation.Entitys.Magics.StdThrownMagic; +import com.ictye.the_origin_of_magic.infrastructure.netWork.NetworkIDFinder; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.world.GameRules; import net.minecraft.world.World; @@ -18,6 +23,10 @@ public float getMagicLevel() { return magicLevel; } + public void setMagicLevel(float magicLevel) { + this.magicLevel = magicLevel; + } + /** * 生成魔法實體 * @param player 玩家 @@ -32,6 +41,11 @@ public boolean cast(PlayerEntity player, StdThrownMagic magic , World world){ } if(magicLevel>neededMagic){ magicLevel -= neededMagic; + if(player instanceof ServerPlayerEntity serverPlayerEntity){ + PacketByteBuf buffer = PacketByteBufs.create(); + buffer.writeFloat(magicLevel); + ServerPlayNetworking.send(serverPlayerEntity, NetworkIDFinder.SYNC_HUD_ID, buffer); + } return world.spawnEntity(magic); }else { return false; diff --git a/src/main/java/com/ictye/the_origin_of_magic/infrastructure/GUI/MagicLevelHud.java b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/GUI/MagicLevelHud.java index 3cbff87..2685107 100644 --- a/src/main/java/com/ictye/the_origin_of_magic/infrastructure/GUI/MagicLevelHud.java +++ b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/GUI/MagicLevelHud.java @@ -3,6 +3,8 @@ import com.ictye.the_origin_of_magic.foundation.player.MagicAbilitiesManager; import com.ictye.the_origin_of_magic.utils.PlayerEntityMixinInterfaces; import com.mojang.blaze3d.systems.RenderSystem; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.render.GameRenderer; @@ -10,6 +12,7 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.Identifier; +@Environment(value = EnvType.CLIENT) public class MagicLevelHud { private static final Identifier magicTexture = new Identifier("the_origin_of_magic", "textures/gui/magic_power_image.png"); @@ -39,12 +42,12 @@ public static void renderThirstHud(MatrixStack matrixStack, MinecraftClient clie // 繪製半個魔力 - for (int i = 0; i < 20; i++) { + for (int i = 0; i <= 20; i++) { if(magicLevel!=0){ - if (((magicLevel + 1) / 2) > i){ + if (((((int)magicLevel) + 1) / 2) > i){ DrawableHelper.drawTexture( - matrixStack, - width + 82 - (i * 9) + i , (height - 49) - 1, + matrixStack + ,width + 82 - (i * 9) + i , (height - 49) - 1, 18, 0, 9, 9, 27, 9); @@ -54,7 +57,7 @@ public static void renderThirstHud(MatrixStack matrixStack, MinecraftClient clie for (int i = 0; i < 20; i++) { if(magicLevel!=0){ - if ((magicLevel / 2) > i){ + if ((((int)magicLevel) / 2) > i){ DrawableHelper.drawTexture( matrixStack, width + 82 - (i * 9) + i , (height - 49) - 1, diff --git a/src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/NetWorkReg.java b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/NetWorkReg.java new file mode 100644 index 0000000..479ba40 --- /dev/null +++ b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/NetWorkReg.java @@ -0,0 +1,12 @@ +package com.ictye.the_origin_of_magic.infrastructure.netWork; + +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; + +public class NetWorkReg { + public static void registerC2SPackets() { + } + + public static void registerS2CPackets() { + ClientPlayNetworking.registerGlobalReceiver(NetworkIDFinder.SYNC_HUD_ID, SyncHUDS2CPacket::receive); + } +} diff --git a/src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/NetworkIDFinder.java b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/NetworkIDFinder.java new file mode 100644 index 0000000..0c2c0a9 --- /dev/null +++ b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/NetworkIDFinder.java @@ -0,0 +1,11 @@ +package com.ictye.the_origin_of_magic.infrastructure.netWork; + +import com.ictye.the_origin_of_magic.the_origin_of_magic; +import net.minecraft.util.Identifier; + +public class NetworkIDFinder { + public static final Identifier DRINK_WATER_ID = new Identifier(the_origin_of_magic.Mod_Id, "drink_water"); + public static final Identifier SWING_HAND_ID = new Identifier(the_origin_of_magic.Mod_Id, "swing_hand"); + public static final Identifier SYNC_THIRST_ID = new Identifier(the_origin_of_magic.Mod_Id, "sync_thirst"); + public static final Identifier SYNC_HUD_ID = new Identifier(the_origin_of_magic.Mod_Id, "sync_hud"); +} diff --git a/src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/SyncHUDS2CPacket.java b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/SyncHUDS2CPacket.java new file mode 100644 index 0000000..2626a25 --- /dev/null +++ b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/netWork/SyncHUDS2CPacket.java @@ -0,0 +1,20 @@ +package com.ictye.the_origin_of_magic.infrastructure.netWork; + +import com.ictye.the_origin_of_magic.foundation.player.MagicAbilitiesManager; +import com.ictye.the_origin_of_magic.utils.PlayerEntityMixinInterfaces; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.network.PacketByteBuf; + +public class SyncHUDS2CPacket { + public static void receive(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buffer, PacketSender responseSender) { + PlayerEntity playerEntity = client.player; + MagicAbilitiesManager magicAbilitiesManager = null; + if (playerEntity != null) { + magicAbilitiesManager = ((PlayerEntityMixinInterfaces) playerEntity).the_origin_of_magic$getMagicAbilitiesManager(); + magicAbilitiesManager.setMagicLevel(buffer.readFloat()); + } + } +} diff --git a/src/main/java/com/ictye/the_origin_of_magic/the_origin_of_magic.java b/src/main/java/com/ictye/the_origin_of_magic/the_origin_of_magic.java index 095897b..cb26806 100644 --- a/src/main/java/com/ictye/the_origin_of_magic/the_origin_of_magic.java +++ b/src/main/java/com/ictye/the_origin_of_magic/the_origin_of_magic.java @@ -1,9 +1,10 @@ package com.ictye.the_origin_of_magic; import com.ictye.the_origin_of_magic.Contents.AllBlock; -import com.ictye.the_origin_of_magic.Contents.AllItem; import com.ictye.the_origin_of_magic.Contents.AllBlockEntity; import com.ictye.the_origin_of_magic.Contents.AllEntity; +import com.ictye.the_origin_of_magic.Contents.AllItem; +import com.ictye.the_origin_of_magic.infrastructure.netWork.NetWorkReg; import net.fabricmc.api.ModInitializer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,5 +20,7 @@ public void onInitialize() { AllItem.registerItems(); AllBlockEntity.register(); AllEntity.regEntity(); + + NetWorkReg.registerC2SPackets(); } } diff --git a/src/main/java/com/ictye/the_origin_of_magic/the_origin_of_magic_client.java b/src/main/java/com/ictye/the_origin_of_magic/the_origin_of_magic_client.java index 42197a6..8edc1cc 100644 --- a/src/main/java/com/ictye/the_origin_of_magic/the_origin_of_magic_client.java +++ b/src/main/java/com/ictye/the_origin_of_magic/the_origin_of_magic_client.java @@ -3,6 +3,7 @@ import com.ictye.the_origin_of_magic.Contents.AllEntity; import com.ictye.the_origin_of_magic.infrastructure.GUI.MagicWorkbench.MagicWorkbenchScreen; import com.ictye.the_origin_of_magic.infrastructure.GUI.MagicWorkbench.MagicWorkbenchScreenHandler; +import com.ictye.the_origin_of_magic.infrastructure.netWork.NetWorkReg; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -22,5 +23,7 @@ public class the_origin_of_magic_client implements ClientModInitializer { public void onInitializeClient() { HandledScreens.register(MAGIC_WORKSTATION_SCREEN_HANDLER_SCREEN_HANDLER_TYPE, MagicWorkbenchScreen::new); AllEntity.regEntityRunder(); + + NetWorkReg.registerS2CPackets(); } } diff --git a/src/main/java/com/ictye/the_origin_of_magic/utils/PlayerEntityMixinInterfaces.java b/src/main/java/com/ictye/the_origin_of_magic/utils/PlayerEntityMixinInterfaces.java index 4507c8b..3453659 100644 --- a/src/main/java/com/ictye/the_origin_of_magic/utils/PlayerEntityMixinInterfaces.java +++ b/src/main/java/com/ictye/the_origin_of_magic/utils/PlayerEntityMixinInterfaces.java @@ -3,5 +3,5 @@ import com.ictye.the_origin_of_magic.foundation.player.MagicAbilitiesManager; public interface PlayerEntityMixinInterfaces { - MagicAbilitiesManager the_origin_of_magic$getMagicAbilitiesManager(); + public MagicAbilitiesManager the_origin_of_magic$getMagicAbilitiesManager(); } diff --git a/src/main/resources/assets/the_origin_of_magic/textures/gui/magic_power_image.png b/src/main/resources/assets/the_origin_of_magic/textures/gui/magic_power_image.png index e10d196a94c4eeb3d2aab0bbd2a153c423cee9d9..45410f9696e3875d22bdd59355810d5128bf2145 100644 GIT binary patch delta 271 zcmV+q0r38d1GWN?Fn<8>NklD%PDHLkV1mP`c#Z%7 delta 356 zcmV-q0h|7|0*eEXFn<9>Nkl<$H)usM9TYmjAh27pyJV9MYj)nwm;b$g_m9XVc717>0TTfy4S&E! z0OZOL+a%HBI3`z0WK!y?uGy8Pz*~+173f4^BkVr2=V2o=fPWI>IA)t9cbTq^$fVy$ zYd8RNK-?y)>Q9_VQ51SGoN~P!Nz?S^5NI*qKdV6Jq1|Y2XWV0I1GYqd&%hagqA0X$ z=-f6;73f6G)g1s(A8Y&?Cc<6IU^q3*`zwB(D-|+5I+3x6wG8svxtrVlq}}A#%h50| zZ}@d4WytjCL|rB=Cu?;Lj4`|aW;L0VI!#ltT#R^nT(Mk?T5r~6`gDYeem9Na$2M1e zNi&Z8__u$JP{&4pBN~Toz2Hrq)FO0000