diff --git a/src/main/java/knightminer/inspirations/common/network/InspirationsNetwork.java b/src/main/java/knightminer/inspirations/common/network/InspirationsNetwork.java index f228bfc7..151e6a5c 100644 --- a/src/main/java/knightminer/inspirations/common/network/InspirationsNetwork.java +++ b/src/main/java/knightminer/inspirations/common/network/InspirationsNetwork.java @@ -1,7 +1,9 @@ package knightminer.inspirations.common.network; import knightminer.inspirations.Inspirations; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.network.IPacket; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; @@ -28,20 +30,52 @@ public void setup() { registerPacket(MilkablePacket.class, MilkablePacket::encode, MilkablePacket::decode, MilkablePacket::handle); } + /** + * Sends a packet to all players on the network + * @param packet Packet + */ public static void sendToAll(AbstractPacket packet) { instance.network.send(PacketDistributor.ALL.noArg(), packet); } - + /** + * + * Sends a packet to a specific player + * @param packet Packet + * @param player Player receiving packet + */ public static void sendTo(AbstractPacket packet, ServerPlayerEntity player) { instance.network.send(PacketDistributor.PLAYER.with(() -> player), packet); } + /** + * Sends a vanilla packet to a player + * @param player Player receiving packet + * @param packet Packet + */ + public static void sendPacket(Entity player, IPacket packet) { + if(player instanceof ServerPlayerEntity && ((ServerPlayerEntity) player).connection != null) { + ((ServerPlayerEntity) player).connection.sendPacket(packet); + } + } + + /** + * Sends a packet to all clients near a location + * @param world World, does nothing if not a WorldServer + * @param pos Players too far from this position will not receive the packet + * @param packet Packet + */ public static void sendToClients(World world, BlockPos pos, AbstractPacket packet) { if(world instanceof ServerWorld) { sendToClients((ServerWorld)world, pos, packet); } } + /** + * Sends a packet to all clients near a location + * @param world World + * @param pos Players too far from this position will not receive the packet + * @param packet Packet + */ public static void sendToClients(ServerWorld world, BlockPos pos, AbstractPacket packet) { Chunk chunk = world.getChunkAt(pos); instance.network.send(PacketDistributor.TRACKING_CHUNK.with(() -> chunk), packet); diff --git a/src/main/java/knightminer/inspirations/tools/ToolsEvents.java b/src/main/java/knightminer/inspirations/tools/ToolsEvents.java index f4f73ee3..d029811a 100644 --- a/src/main/java/knightminer/inspirations/tools/ToolsEvents.java +++ b/src/main/java/knightminer/inspirations/tools/ToolsEvents.java @@ -1,6 +1,7 @@ package knightminer.inspirations.tools; import knightminer.inspirations.common.Config; +import knightminer.inspirations.common.network.InspirationsNetwork; import knightminer.inspirations.library.InspirationsRegistry; import knightminer.inspirations.library.Util; import knightminer.inspirations.tools.item.WaypointCompassItem; @@ -14,11 +15,13 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.item.DyeColor; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.item.ShearsItem; +import net.minecraft.network.play.server.SEntityVelocityPacket; import net.minecraft.tileentity.LockableTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResultType; @@ -284,6 +287,9 @@ public static void onShieldHit(LivingAttackEvent event) { if (knockback > 0) { if (attacker instanceof LivingEntity) { ((LivingEntity)attacker).knockBack(target, knockback * 0.5F, MathHelper.sin(target.rotationYaw * 0.017453292F), -MathHelper.cos(target.rotationYaw * 0.017453292F)); + if (attacker instanceof ServerPlayerEntity) { + InspirationsNetwork.sendPacket(attacker, new SEntityVelocityPacket(attacker)); + } } else { attacker.addVelocity(-MathHelper.sin(target.rotationYaw * 0.017453292F) * knockback * 0.5f, 0.1D, MathHelper.cos(target.rotationYaw * 0.017453292F) * knockback * 0.5f); }