Skip to content

Commit

Permalink
feat: 实现魔力值显示並修復幾個BUG,优化玩家魔力管理。
Browse files Browse the repository at this point in the history
  • Loading branch information
northgreen committed Apr 19, 2024
1 parent 6d1cf61 commit 0f49aed
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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();


}
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends LivingEntity> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,6 +23,10 @@ public float getMagicLevel() {
return magicLevel;
}

public void setMagicLevel(float magicLevel) {
this.magicLevel = magicLevel;
}

/**
* 生成魔法實體
* @param player 玩家
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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;
import net.minecraft.client.util.math.MatrixStack;
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");
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,5 +20,7 @@ public void onInitialize() {
AllItem.registerItems();
AllBlockEntity.register();
AllEntity.regEntity();

NetWorkReg.registerC2SPackets();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/main/resources/the_origin_of_magic.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"PlayerEntityMixin"
],
"client": [
"ClientPlayerMixin",
"PlayerHudMixin"
],
"injectors": {
Expand Down

0 comments on commit 0f49aed

Please sign in to comment.