diff --git a/common/src/main/java/ru/octol1ttle/knockdowns/common/KnockdownsClient.java b/common/src/main/java/ru/octol1ttle/knockdowns/common/KnockdownsClient.java new file mode 100644 index 0000000..1929320 --- /dev/null +++ b/common/src/main/java/ru/octol1ttle/knockdowns/common/KnockdownsClient.java @@ -0,0 +1,19 @@ +package ru.octol1ttle.knockdowns.common; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.util.math.Vec3d; +import ru.octol1ttle.knockdowns.common.events.KnockdownsClientEvents; +import ru.octol1ttle.knockdowns.common.registries.KnockdownsSoundEvents; +import ru.octol1ttle.knockdowns.common.registries.KnockedDownSoundInstance; + +public class KnockdownsClient { + public static void init() { + KnockdownsClientEvents.registerCallbacks(); + } + + public static void playKnockedDownSound(Vec3d pos) { + MinecraftClient.getInstance().getSoundManager().play( + new KnockedDownSoundInstance(KnockdownsSoundEvents.KNOCKED_DOWN.get(), pos) + ); + } +} diff --git a/common/src/main/java/ru/octol1ttle/knockdowns/common/KnockdownsCommon.java b/common/src/main/java/ru/octol1ttle/knockdowns/common/KnockdownsCommon.java index d7fea13..8514251 100644 --- a/common/src/main/java/ru/octol1ttle/knockdowns/common/KnockdownsCommon.java +++ b/common/src/main/java/ru/octol1ttle/knockdowns/common/KnockdownsCommon.java @@ -1,7 +1,7 @@ package ru.octol1ttle.knockdowns.common; -import ru.octol1ttle.knockdowns.common.events.KnockdownsClientEvents; import ru.octol1ttle.knockdowns.common.events.KnockdownsEvents; +import ru.octol1ttle.knockdowns.common.network.KnockdownsNetwork; import ru.octol1ttle.knockdowns.common.registries.KnockdownsSoundEvents; public class KnockdownsCommon { @@ -10,7 +10,6 @@ public class KnockdownsCommon { public static void init() { KnockdownsSoundEvents.register(); KnockdownsNetwork.registerPackets(); - KnockdownsClientEvents.registerCallbacks(); KnockdownsEvents.registerCallbacks(); } } diff --git a/common/src/main/java/ru/octol1ttle/knockdowns/common/events/KnockdownsClientEvents.java b/common/src/main/java/ru/octol1ttle/knockdowns/common/events/KnockdownsClientEvents.java index 459d763..08daac4 100644 --- a/common/src/main/java/ru/octol1ttle/knockdowns/common/events/KnockdownsClientEvents.java +++ b/common/src/main/java/ru/octol1ttle/knockdowns/common/events/KnockdownsClientEvents.java @@ -11,10 +11,10 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; import net.minecraft.util.hit.EntityHitResult; import net.minecraft.util.hit.HitResult; -import ru.octol1ttle.knockdowns.common.KnockdownsNetwork; +import ru.octol1ttle.knockdowns.common.network.KnockdownsNetwork; import ru.octol1ttle.knockdowns.common.api.IKnockableDown; -import ru.octol1ttle.knockdowns.common.packets.KnockedDownStatusPacket; -import ru.octol1ttle.knockdowns.common.packets.ReviveStatusPacket; +import ru.octol1ttle.knockdowns.common.network.packets.KnockedDownStatusPacket; +import ru.octol1ttle.knockdowns.common.network.packets.ReviveStatusPacket; public class KnockdownsClientEvents { private static final int REVIVAL_WAIT_TIME = 10 * SharedConstants.TICKS_PER_SECOND; @@ -22,13 +22,13 @@ public class KnockdownsClientEvents { private static int revivalTimer = -1; public static void registerCallbacks() { - registerOnEntityLoad(); + registerOnClientPlayerJoin(); registerOnEntityUse(); registerOnWorldTick(); registerOnHudRender(); } - private static void registerOnEntityLoad() { + private static void registerOnClientPlayerJoin() { ClientPlayerEvent.CLIENT_PLAYER_JOIN.register(player -> { UUID playerUuid = player.getUuid(); KnockdownsNetwork.sendToServer(new KnockedDownStatusPacket.RequestC2S(playerUuid)); @@ -38,7 +38,7 @@ public class KnockdownsClientEvents { private static void registerOnEntityUse() { InteractionEvent.INTERACT_ENTITY.register((player, entity, hand) -> { - if (!(entity instanceof IKnockableDown knockableEntity) || !knockableEntity.knockdowns$isKnockedDown() + if (!player.getWorld().isClient() || !(entity instanceof IKnockableDown knockableEntity) || !knockableEntity.knockdowns$isKnockedDown() || knockableEntity.knockdowns$isBeingRevived()) { return EventResult.pass(); } diff --git a/common/src/main/java/ru/octol1ttle/knockdowns/common/events/KnockdownsEvents.java b/common/src/main/java/ru/octol1ttle/knockdowns/common/events/KnockdownsEvents.java index a812469..429a3ce 100644 --- a/common/src/main/java/ru/octol1ttle/knockdowns/common/events/KnockdownsEvents.java +++ b/common/src/main/java/ru/octol1ttle/knockdowns/common/events/KnockdownsEvents.java @@ -11,10 +11,10 @@ import net.minecraft.text.Text; import net.minecraft.text.TranslatableTextContent; import net.minecraft.util.Hand; import net.minecraft.world.GameRules; -import ru.octol1ttle.knockdowns.common.KnockdownsNetwork; +import ru.octol1ttle.knockdowns.common.network.KnockdownsNetwork; import ru.octol1ttle.knockdowns.common.api.IKnockableDown; -import ru.octol1ttle.knockdowns.common.packets.KnockedDownStatusPacket; -import ru.octol1ttle.knockdowns.common.packets.PlayKnockedDownSoundS2CPacket; +import ru.octol1ttle.knockdowns.common.network.packets.KnockedDownStatusPacket; +import ru.octol1ttle.knockdowns.common.network.packets.PlayKnockedDownSoundS2CPacket; public class KnockdownsEvents { public static void registerCallbacks() { diff --git a/common/src/main/java/ru/octol1ttle/knockdowns/common/mixin/PlayerEntityMixin.java b/common/src/main/java/ru/octol1ttle/knockdowns/common/mixin/PlayerEntityMixin.java index 15ebee3..4d7ced9 100644 --- a/common/src/main/java/ru/octol1ttle/knockdowns/common/mixin/PlayerEntityMixin.java +++ b/common/src/main/java/ru/octol1ttle/knockdowns/common/mixin/PlayerEntityMixin.java @@ -15,9 +15,9 @@ import ru.octol1ttle.knockdowns.common.api.IKnockableDown; @Mixin(PlayerEntity.class) public abstract class PlayerEntityMixin implements IKnockableDown { @Unique - private boolean knockedDown; + private boolean knockdowns$knockedDown; @Unique - private boolean beingRevived; + private boolean knockdowns$beingRevived; @ModifyExpressionValue(method = "updatePose", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;isSwimming()Z")) private boolean enterSwimmingIfKnockedDown(boolean original) { @@ -36,32 +36,32 @@ public abstract class PlayerEntityMixin implements IKnockableDown { @Inject(method = "readCustomDataFromNbt", at = @At("TAIL")) public void readKnockedDownFromNbt(NbtCompound nbt, CallbackInfo ci) { - this.knockedDown = nbt.getBoolean("KnockedDown"); + this.knockdowns$knockedDown = nbt.getBoolean("KnockedDown"); } @Inject(method = "writeCustomDataToNbt", at = @At("TAIL")) public void writeKnockedDownToNbt(NbtCompound nbt, CallbackInfo ci) { - nbt.putBoolean("KnockedDown", this.knockedDown); + nbt.putBoolean("KnockedDown", this.knockdowns$knockedDown); } @Override public boolean knockdowns$isKnockedDown() { - return knockedDown; + return knockdowns$knockedDown; } @Override public void knockdowns$setKnockedDown(boolean knockedDown) { - this.knockedDown = knockedDown; + this.knockdowns$knockedDown = knockedDown; } @Override public boolean knockdowns$isBeingRevived() { - return beingRevived; + return knockdowns$beingRevived; } @Override public void knockdowns$setBeingRevived(boolean beingRevived) { - this.beingRevived = beingRevived; + this.knockdowns$beingRevived = beingRevived; } @Override diff --git a/common/src/main/java/ru/octol1ttle/knockdowns/common/KnockdownsNetwork.java b/common/src/main/java/ru/octol1ttle/knockdowns/common/network/KnockdownsNetwork.java similarity index 92% rename from common/src/main/java/ru/octol1ttle/knockdowns/common/KnockdownsNetwork.java rename to common/src/main/java/ru/octol1ttle/knockdowns/common/network/KnockdownsNetwork.java index 3e552bc..1d9af8e 100644 --- a/common/src/main/java/ru/octol1ttle/knockdowns/common/KnockdownsNetwork.java +++ b/common/src/main/java/ru/octol1ttle/knockdowns/common/network/KnockdownsNetwork.java @@ -1,4 +1,4 @@ -package ru.octol1ttle.knockdowns.common; +package ru.octol1ttle.knockdowns.common.network; import dev.architectury.networking.NetworkChannel; import dev.architectury.networking.NetworkManager; @@ -11,9 +11,10 @@ import net.minecraft.server.world.ServerChunkManager; import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ThreadedAnvilChunkStorage; import net.minecraft.util.Identifier; -import ru.octol1ttle.knockdowns.common.packets.KnockedDownStatusPacket; -import ru.octol1ttle.knockdowns.common.packets.PlayKnockedDownSoundS2CPacket; -import ru.octol1ttle.knockdowns.common.packets.ReviveStatusPacket; +import ru.octol1ttle.knockdowns.common.KnockdownsCommon; +import ru.octol1ttle.knockdowns.common.network.packets.KnockedDownStatusPacket; +import ru.octol1ttle.knockdowns.common.network.packets.PlayKnockedDownSoundS2CPacket; +import ru.octol1ttle.knockdowns.common.network.packets.ReviveStatusPacket; public class KnockdownsNetwork { private static final NetworkChannel CHANNEL = NetworkChannel.create(new Identifier(KnockdownsCommon.MOD_ID, "main")); diff --git a/common/src/main/java/ru/octol1ttle/knockdowns/common/packets/KnockdownsPacket.java b/common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/KnockdownsPacket.java similarity index 73% rename from common/src/main/java/ru/octol1ttle/knockdowns/common/packets/KnockdownsPacket.java rename to common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/KnockdownsPacket.java index 0267f16..f9288d7 100644 --- a/common/src/main/java/ru/octol1ttle/knockdowns/common/packets/KnockdownsPacket.java +++ b/common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/KnockdownsPacket.java @@ -1,14 +1,10 @@ -package ru.octol1ttle.knockdowns.common.packets; +package ru.octol1ttle.knockdowns.common.network.packets; import dev.architectury.networking.NetworkManager; import java.util.function.Supplier; import net.minecraft.network.PacketByteBuf; public abstract class KnockdownsPacket { - public KnockdownsPacket(PacketByteBuf buf) { - // Decode data into a message - } - public KnockdownsPacket(/* args here */) { // Message creation } diff --git a/common/src/main/java/ru/octol1ttle/knockdowns/common/packets/KnockedDownStatusPacket.java b/common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/KnockedDownStatusPacket.java similarity index 95% rename from common/src/main/java/ru/octol1ttle/knockdowns/common/packets/KnockedDownStatusPacket.java rename to common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/KnockedDownStatusPacket.java index fa0fc82..a86205c 100644 --- a/common/src/main/java/ru/octol1ttle/knockdowns/common/packets/KnockedDownStatusPacket.java +++ b/common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/KnockedDownStatusPacket.java @@ -1,10 +1,10 @@ -package ru.octol1ttle.knockdowns.common.packets; +package ru.octol1ttle.knockdowns.common.network.packets; import dev.architectury.networking.NetworkManager; import java.util.UUID; import java.util.function.Supplier; import net.minecraft.network.PacketByteBuf; -import ru.octol1ttle.knockdowns.common.KnockdownsNetwork; +import ru.octol1ttle.knockdowns.common.network.KnockdownsNetwork; import ru.octol1ttle.knockdowns.common.api.IKnockableDown; public class KnockedDownStatusPacket { diff --git a/common/src/main/java/ru/octol1ttle/knockdowns/common/packets/PlayKnockedDownSoundS2CPacket.java b/common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/PlayKnockedDownSoundS2CPacket.java similarity index 67% rename from common/src/main/java/ru/octol1ttle/knockdowns/common/packets/PlayKnockedDownSoundS2CPacket.java rename to common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/PlayKnockedDownSoundS2CPacket.java index 041d112..fa9f4fb 100644 --- a/common/src/main/java/ru/octol1ttle/knockdowns/common/packets/PlayKnockedDownSoundS2CPacket.java +++ b/common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/PlayKnockedDownSoundS2CPacket.java @@ -1,12 +1,10 @@ -package ru.octol1ttle.knockdowns.common.packets; +package ru.octol1ttle.knockdowns.common.network.packets; import dev.architectury.networking.NetworkManager; import java.util.function.Supplier; -import net.minecraft.client.MinecraftClient; import net.minecraft.network.PacketByteBuf; import net.minecraft.util.math.Vec3d; -import ru.octol1ttle.knockdowns.common.registries.KnockdownsSoundEvents; -import ru.octol1ttle.knockdowns.common.registries.KnockedDownSoundInstance; +import ru.octol1ttle.knockdowns.common.KnockdownsClient; public class PlayKnockedDownSoundS2CPacket extends KnockdownsPacket { private final double x; @@ -33,8 +31,6 @@ public class PlayKnockedDownSoundS2CPacket extends KnockdownsPacket { @Override public void apply(Supplier contextSupplier) { NetworkManager.PacketContext context = contextSupplier.get(); - context.queue(() -> MinecraftClient.getInstance().getSoundManager().play( - new KnockedDownSoundInstance(KnockdownsSoundEvents.KNOCKED_DOWN.get(), new Vec3d(this.x, this.y, this.z)) - )); + context.queue(() -> KnockdownsClient.playKnockedDownSound(new Vec3d(this.x, this.y, this.z))); } } diff --git a/common/src/main/java/ru/octol1ttle/knockdowns/common/packets/ReviveStatusPacket.java b/common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/ReviveStatusPacket.java similarity index 97% rename from common/src/main/java/ru/octol1ttle/knockdowns/common/packets/ReviveStatusPacket.java rename to common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/ReviveStatusPacket.java index e84b7da..e486e7d 100644 --- a/common/src/main/java/ru/octol1ttle/knockdowns/common/packets/ReviveStatusPacket.java +++ b/common/src/main/java/ru/octol1ttle/knockdowns/common/network/packets/ReviveStatusPacket.java @@ -1,11 +1,11 @@ -package ru.octol1ttle.knockdowns.common.packets; +package ru.octol1ttle.knockdowns.common.network.packets; import dev.architectury.networking.NetworkManager; import java.util.UUID; import java.util.function.Supplier; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; -import ru.octol1ttle.knockdowns.common.KnockdownsNetwork; +import ru.octol1ttle.knockdowns.common.network.KnockdownsNetwork; import ru.octol1ttle.knockdowns.common.api.IKnockableDown; public class ReviveStatusPacket { diff --git a/fabric/src/main/java/ru/octol1ttle/knockdowns/fabric/KnockdownsFabricClient.java b/fabric/src/main/java/ru/octol1ttle/knockdowns/fabric/KnockdownsFabricClient.java new file mode 100644 index 0000000..2a651c4 --- /dev/null +++ b/fabric/src/main/java/ru/octol1ttle/knockdowns/fabric/KnockdownsFabricClient.java @@ -0,0 +1,11 @@ +package ru.octol1ttle.knockdowns.fabric; + +import net.fabricmc.api.ClientModInitializer; +import ru.octol1ttle.knockdowns.common.KnockdownsClient; + +public class KnockdownsFabricClient implements ClientModInitializer { + @Override + public void onInitializeClient() { + KnockdownsClient.init(); + } +} diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index b0ce26e..b1f1a7c 100644 --- a/fabric/src/main/resources/fabric.mod.json +++ b/fabric/src/main/resources/fabric.mod.json @@ -17,6 +17,9 @@ "entrypoints": { "main": [ "ru.octol1ttle.knockdowns.fabric.KnockdownsFabric" + ], + "client": [ + "ru.octol1ttle.knockdowns.fabric.KnockdownsFabricClient" ] }, "mixins": [ diff --git a/forge/src/main/java/ru/octol1ttle/knockdowns/forge/KnockdownsForge.java b/forge/src/main/java/ru/octol1ttle/knockdowns/forge/KnockdownsForge.java index e71aa25..dd706c0 100644 --- a/forge/src/main/java/ru/octol1ttle/knockdowns/forge/KnockdownsForge.java +++ b/forge/src/main/java/ru/octol1ttle/knockdowns/forge/KnockdownsForge.java @@ -1,10 +1,14 @@ package ru.octol1ttle.knockdowns.forge; import dev.architectury.platform.forge.EventBuses; +import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import ru.octol1ttle.knockdowns.common.KnockdownsClient; import ru.octol1ttle.knockdowns.common.KnockdownsCommon; +@SuppressWarnings("unused") @Mod(KnockdownsCommon.MOD_ID) public class KnockdownsForge { public KnockdownsForge() { @@ -12,4 +16,9 @@ public class KnockdownsForge { EventBuses.registerModEventBus(KnockdownsCommon.MOD_ID, FMLJavaModLoadingContext.get().getModEventBus()); KnockdownsCommon.init(); } + + @SubscribeEvent + public void onInitializeClient(FMLClientSetupEvent event) { + KnockdownsClient.init(); + } }